blob: 37d735b3ba5bfb64206e129f683b4db885dfca5c [file] [log] [blame]
Neil Schemenauerfd288c72001-01-02 16:30:31 +00001import copy
2import sys
Tim Petersf022a4d2002-04-15 23:52:04 +00003import warnings
Neil Schemenauerfd288c72001-01-02 16:30:31 +00004
5# Fake a number that implements numeric methods through __coerce__
6class CoerceNumber:
7 def __init__(self, arg):
8 self.arg = arg
9
10 def __repr__(self):
11 return '<CoerceNumber %s>' % repr(self.arg)
12
13 def __coerce__(self, other):
14 if isinstance(other, CoerceNumber):
15 return self.arg, other.arg
16 else:
17 return (self.arg, other)
18
19
20# Fake a number that implements numeric ops through methods.
21class MethodNumber:
22
23 def __init__(self,arg):
24 self.arg = arg
25
26 def __repr__(self):
27 return '<MethodNumber %s>' % repr(self.arg)
28
29 def __add__(self,other):
30 return self.arg + other
31
32 def __radd__(self,other):
33 return other + self.arg
34
35 def __sub__(self,other):
36 return self.arg - other
37
38 def __rsub__(self,other):
39 return other - self.arg
40
41 def __mul__(self,other):
42 return self.arg * other
43
44 def __rmul__(self,other):
45 return other * self.arg
46
47 def __div__(self,other):
48 return self.arg / other
49
50 def __rdiv__(self,other):
51 return other / self.arg
52
53 def __pow__(self,other):
54 return self.arg ** other
55
56 def __rpow__(self,other):
57 return other ** self.arg
58
59 def __mod__(self,other):
60 return self.arg % other
61
62 def __rmod__(self,other):
63 return other % self.arg
64
65 def __cmp__(self, other):
66 return cmp(self.arg, other)
67
68
Neil Schemenauer38796d02001-01-03 01:52:11 +000069candidates = [ 2, 4.0, 2L, 2+0j, [1], (2,), None,
Neil Schemenauer6cbba502004-03-10 17:30:03 +000070 MethodNumber(2), CoerceNumber(2)]
Neil Schemenauerfd288c72001-01-02 16:30:31 +000071
72infix_binops = [ '+', '-', '*', '/', '**', '%' ]
73prefix_binops = [ 'divmod' ]
74
Neil Schemenauer6cbba502004-03-10 17:30:03 +000075def format_float(value):
76 if abs(value) < 0.01:
77 return '0.0'
78 else:
79 return '%.1f' % value
80
81# avoid testing platform fp quirks
82def format_result(value):
83 if isinstance(value, complex):
84 return '(%s + %sj)' % (format_float(value.real),
85 format_float(value.imag))
86 elif isinstance(value, float):
87 return format_float(value)
88 return str(value)
89
Neil Schemenauerfd288c72001-01-02 16:30:31 +000090def do_infix_binops():
91 for a in candidates:
92 for b in candidates:
93 for op in infix_binops:
94 print '%s %s %s' % (a, op, b),
95 try:
96 x = eval('a %s b' % op)
97 except:
98 error = sys.exc_info()[:2]
99 print '... %s' % error[0]
100 else:
Neil Schemenauer6cbba502004-03-10 17:30:03 +0000101 print '=', format_result(x)
Neil Schemenauerfd288c72001-01-02 16:30:31 +0000102 try:
103 z = copy.copy(a)
104 except copy.Error:
105 z = a # assume it has no inplace ops
106 print '%s %s= %s' % (a, op, b),
107 try:
108 exec('z %s= b' % op)
109 except:
110 error = sys.exc_info()[:2]
111 print '... %s' % error[0]
112 else:
Neil Schemenauer6cbba502004-03-10 17:30:03 +0000113 print '=>', format_result(z)
Neil Schemenauerfd288c72001-01-02 16:30:31 +0000114
115def do_prefix_binops():
116 for a in candidates:
117 for b in candidates:
118 for op in prefix_binops:
119 print '%s(%s, %s)' % (op, a, b),
120 try:
121 x = eval('%s(a, b)' % op)
122 except:
123 error = sys.exc_info()[:2]
124 print '... %s' % error[0]
125 else:
Neil Schemenauer6cbba502004-03-10 17:30:03 +0000126 print '=', format_result(x)
Neil Schemenauerfd288c72001-01-02 16:30:31 +0000127
Armin Rigoa1748132004-12-23 22:13:13 +0000128# New-style class version of CoerceNumber
129class CoerceTo(object):
130 def __init__(self, arg):
131 self.arg = arg
132 def __coerce__(self, other):
133 if isinstance(other, CoerceTo):
134 return self.arg, other.arg
135 else:
136 return self.arg, other
137
138def assert_(expr, msg=None):
139 if not expr:
140 raise AssertionError, msg
141
142def do_cmptypes():
143 # Built-in tp_compare slots expect their arguments to have the
144 # same type, but a user-defined __coerce__ doesn't have to obey.
145 # SF #980352
146 evil_coercer = CoerceTo(42)
147 # Make sure these don't crash any more
148 assert_(cmp(u'fish', evil_coercer) != 0)
149 assert_(cmp(slice(1), evil_coercer) != 0)
150 # ...but that this still works
151 class WackyComparer(object):
152 def __cmp__(self, other):
153 assert_(other == 42, 'expected evil_coercer, got %r' % other)
154 return 0
155 assert_(cmp(WackyComparer(), evil_coercer) == 0)
156 # ...and classic classes too, since that code path is a little different
157 class ClassicWackyComparer:
158 def __cmp__(self, other):
159 assert_(other == 42, 'expected evil_coercer, got %r' % other)
160 return 0
161 assert_(cmp(ClassicWackyComparer(), evil_coercer) == 0)
162
Tim Petersf022a4d2002-04-15 23:52:04 +0000163warnings.filterwarnings("ignore",
164 r'complex divmod\(\), // and % are deprecated',
Tim Petersd3925062002-04-16 01:27:44 +0000165 DeprecationWarning,
Barry Warsaw408b6d32002-07-30 23:27:12 +0000166 r'test.test_coercion$')
Tim Peters7d799482002-04-16 00:01:09 +0000167do_infix_binops()
168do_prefix_binops()
Armin Rigoa1748132004-12-23 22:13:13 +0000169do_cmptypes()