1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
import copy
import sys
import warnings
# 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, 4.0, 2L, 2+0j, [1], (2,), None,
MethodNumber(1), CoerceNumber(2)]
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
warnings.filterwarnings("ignore",
r'complex divmod\(\), // and % are deprecated',
DeprecationWarning,
r'test_coercion$')
do_infix_binops()
do_prefix_binops()
|