0001 import copy 0002 import sys 0003 import warnings 0004 0005 # Fake a number that implements numeric methods through __coerce__ 0006 class CoerceNumber: 0007 def __init__(self, arg): 0008 self.arg = arg 0009 0010 def __repr__(self): 0011 return '<CoerceNumber %s>' % repr(self.arg) 0012 0013 def __coerce__(self, other): 0014 if isinstance(other, CoerceNumber): 0015 return self.arg, other.arg 0016 else: 0017 return (self.arg, other) 0018 0019 0020 # Fake a number that implements numeric ops through methods. 0021 class MethodNumber: 0022 0023 def __init__(self,arg): 0024 self.arg = arg 0025 0026 def __repr__(self): 0027 return '<MethodNumber %s>' % repr(self.arg) 0028 0029 def __add__(self,other): 0030 return self.arg + other 0031 0032 def __radd__(self,other): 0033 return other + self.arg 0034 0035 def __sub__(self,other): 0036 return self.arg - other 0037 0038 def __rsub__(self,other): 0039 return other - self.arg 0040 0041 def __mul__(self,other): 0042 return self.arg * other 0043 0044 def __rmul__(self,other): 0045 return other * self.arg 0046 0047 def __div__(self,other): 0048 return self.arg / other 0049 0050 def __rdiv__(self,other): 0051 return other / self.arg 0052 0053 def __pow__(self,other): 0054 return self.arg ** other 0055 0056 def __rpow__(self,other): 0057 return other ** self.arg 0058 0059 def __mod__(self,other): 0060 return self.arg % other 0061 0062 def __rmod__(self,other): 0063 return other % self.arg 0064 0065 def __cmp__(self, other): 0066 return cmp(self.arg, other) 0067 0068 0069 candidates = [ 2, 4.0, 2L, 2+0j, [1], (2,), None, 0070 MethodNumber(2), CoerceNumber(2)] 0071 0072 infix_binops = [ '+', '-', '*', '/', '**', '%' ] 0073 prefix_binops = [ 'divmod' ] 0074 0075 def format_float(value): 0076 if abs(value) < 0.01: 0077 return '0.0' 0078 else: 0079 return '%.1f' % value 0080 0081 # avoid testing platform fp quirks 0082 def format_result(value): 0083 if isinstance(value, complex): 0084 return '(%s + %sj)' % (format_float(value.real), 0085 format_float(value.imag)) 0086 elif isinstance(value, float): 0087 return format_float(value) 0088 return str(value) 0089 0090 def do_infix_binops(): 0091 for a in candidates: 0092 for b in candidates: 0093 for op in infix_binops: 0094 print '%s %s %s' % (a, op, b), 0095 try: 0096 x = eval('a %s b' % op) 0097 except: 0098 error = sys.exc_info()[:2] 0099 print '... %s' % error[0] 0100 else: 0101 print '=', format_result(x) 0102 try: 0103 z = copy.copy(a) 0104 except copy.Error: 0105 z = a # assume it has no inplace ops 0106 print '%s %s= %s' % (a, op, b), 0107 try: 0108 exec('z %s= b' % op) 0109 except: 0110 error = sys.exc_info()[:2] 0111 print '... %s' % error[0] 0112 else: 0113 print '=>', format_result(z) 0114 0115 def do_prefix_binops(): 0116 for a in candidates: 0117 for b in candidates: 0118 for op in prefix_binops: 0119 print '%s(%s, %s)' % (op, a, b), 0120 try: 0121 x = eval('%s(a, b)' % op) 0122 except: 0123 error = sys.exc_info()[:2] 0124 print '... %s' % error[0] 0125 else: 0126 print '=', format_result(x) 0127 0128 warnings.filterwarnings("ignore", 0129 r'complex divmod\(\), // and % are deprecated', 0130 DeprecationWarning, 0131 r'test.test_coercion$') 0132 do_infix_binops() 0133 do_prefix_binops() 0134
Generated by PyXR 0.9.4