summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_coercion.py
diff options
context:
space:
mode:
authorNeil Schemenauer <nascheme@enme.ucalgary.ca>2001-01-02 16:30:31 (GMT)
committerNeil Schemenauer <nascheme@enme.ucalgary.ca>2001-01-02 16:30:31 (GMT)
commitfd288c7cd59cffcdb110d04dca1ba2e821aabac7 (patch)
treebd3453fa24800393d252b69ecf37233208ea5538 /Lib/test/test_coercion.py
parent10e31cf82e5fa325e779013619d9e11bf8b56c28 (diff)
downloadcpython-fd288c7cd59cffcdb110d04dca1ba2e821aabac7.zip
cpython-fd288c7cd59cffcdb110d04dca1ba2e821aabac7.tar.gz
cpython-fd288c7cd59cffcdb110d04dca1ba2e821aabac7.tar.bz2
Add more tests for compare and coercion in preparation for the coercion
overhaul. Closes SF patch #102878.
Diffstat (limited to 'Lib/test/test_coercion.py')
-rw-r--r--Lib/test/test_coercion.py113
1 files changed, 113 insertions, 0 deletions
diff --git a/Lib/test/test_coercion.py b/Lib/test/test_coercion.py
new file mode 100644
index 0000000..03d6b9b
--- /dev/null
+++ b/Lib/test/test_coercion.py
@@ -0,0 +1,113 @@
+import copy
+import sys
+
+# Fake a number that implements numeric methods through __coerce__
+class CoerceNumber:
+ def __init__(self, arg):
+ self.arg = arg
+
+ def __repr__(self):
+ return '<CoerceNumber %s>' % repr(self.arg)
+
+ def __coerce__(self, other):
+ if isinstance(other, CoerceNumber):
+ return self.arg, other.arg
+ else:
+ return (self.arg, other)
+
+
+# Fake a number that implements numeric ops through methods.
+class MethodNumber:
+
+ def __init__(self,arg):
+ self.arg = arg
+
+ def __repr__(self):
+ return '<MethodNumber %s>' % repr(self.arg)
+
+ def __add__(self,other):
+ return self.arg + other
+
+ def __radd__(self,other):
+ return other + self.arg
+
+ def __sub__(self,other):
+ return self.arg - other
+
+ def __rsub__(self,other):
+ return other - self.arg
+
+ def __mul__(self,other):
+ return self.arg * other
+
+ def __rmul__(self,other):
+ return other * self.arg
+
+ def __div__(self,other):
+ return self.arg / other
+
+ def __rdiv__(self,other):
+ return other / self.arg
+
+ def __pow__(self,other):
+ return self.arg ** other
+
+ def __rpow__(self,other):
+ return other ** self.arg
+
+ def __mod__(self,other):
+ return self.arg % other
+
+ def __rmod__(self,other):
+ return other % self.arg
+
+ def __cmp__(self, other):
+ return cmp(self.arg, other)
+
+
+candidates = [ 2, 2.2, 2L, 2+4j, [1], (2,), None,
+ MethodNumber(1), CoerceNumber(8)]
+
+infix_binops = [ '+', '-', '*', '/', '**', '%' ]
+prefix_binops = [ 'divmod' ]
+
+def do_infix_binops():
+ for a in candidates:
+ for b in candidates:
+ for op in infix_binops:
+ print '%s %s %s' % (a, op, b),
+ try:
+ x = eval('a %s b' % op)
+ except:
+ error = sys.exc_info()[:2]
+ print '... %s' % error[0]
+ else:
+ print '=', x
+ try:
+ z = copy.copy(a)
+ except copy.Error:
+ z = a # assume it has no inplace ops
+ print '%s %s= %s' % (a, op, b),
+ try:
+ exec('z %s= b' % op)
+ except:
+ error = sys.exc_info()[:2]
+ print '... %s' % error[0]
+ else:
+ print '=>', z
+
+def do_prefix_binops():
+ for a in candidates:
+ for b in candidates:
+ for op in prefix_binops:
+ print '%s(%s, %s)' % (op, a, b),
+ try:
+ x = eval('%s(a, b)' % op)
+ except:
+ error = sys.exc_info()[:2]
+ print '... %s' % error[0]
+ else:
+ print '=', x
+
+do_infix_binops()
+do_prefix_binops()