Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | import copy |
2 | import sys | |
3 | import warnings | |
4 | ||
5 | # Fake a number that implements numeric methods through __coerce__ | |
6 | class 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. | |
21 | class 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 | ||
69 | candidates = [ 2, 4.0, 2L, 2+0j, [1], (2,), None, | |
70 | MethodNumber(2), CoerceNumber(2)] | |
71 | ||
72 | infix_binops = [ '+', '-', '*', '/', '**', '%' ] | |
73 | prefix_binops = [ 'divmod' ] | |
74 | ||
75 | def 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 | |
82 | def 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 | ||
90 | def 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: | |
101 | print '=', format_result(x) | |
102 | 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: | |
113 | print '=>', format_result(z) | |
114 | ||
115 | def 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: | |
126 | print '=', format_result(x) | |
127 | ||
128 | warnings.filterwarnings("ignore", | |
129 | r'complex divmod\(\), // and % are deprecated', | |
130 | DeprecationWarning, | |
131 | r'test.test_coercion$') | |
132 | do_infix_binops() | |
133 | do_prefix_binops() |