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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | Lib/test/test_coercion.py
import copy import unittest from test.test_support import run_unittest, TestFailed, check_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) # New-style class version of CoerceNumber class CoerceTo(object): def __init__(self, arg): self.arg = arg def __coerce__(self, other): if isinstance(other, CoerceTo): 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 __truediv__(self,other): return self.arg / other def __rtruediv__(self,other): return other / self.arg def __floordiv__(self,other): return self.arg // other def __rfloordiv__(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, 2L, 4.0, 2+0j, [1], (2,), None, MethodNumber(2), CoerceNumber(2)] infix_binops = [ '+', '-', '*', '**', '%', '//', '/' ] TE = TypeError # b = both normal and augmented give same result list # s = single result lists for normal and augmented # e = equals other results # result lists: ['+', '-', '*', '**', '%', '//', ('classic /', 'new /')] # ^^^^^^^^^^^^^^^^^^^^^^ # 2-tuple if results differ # else only one value infix_results = { # 2 (0,0): ('b', [4, 0, 4, 4, 0, 1, (1, 1.0)]), (0,1): ('e', (0,0)), (0,2): ('b', [6.0, -2.0, 8.0, 16.0, 2.0, 0.0, 0.5]), (0,3): ('b', [4+0j, 0+0j, 4+0j, 4+0j, 0+0j, 1+0j, 1+0j]), (0,4): ('b', [TE, TE, [1, 1], TE, TE, TE, TE]), (0,5): ('b', [TE, TE, (2, 2), TE, TE, TE, TE]), (0,6): ('b', [TE, TE, TE, TE, TE, TE, TE]), (0,7): ('e', (0,0)), (0,8): ('e', (0,0)), # 2L (1,0): ('e', (0,0)), (1,1): ('e', (0,1)), (1,2): ('e', (0,2)), (1,3): ('e', (0,3)), (1,4): ('e', (0,4)), (1,5): ('e', (0,5)), (1,6): ('e', (0,6)), (1,7): ('e', (0,7)), (1,8): ('e', (0,8)), # 4.0 (2,0): ('b', [6.0, 2.0, 8.0, 16.0, 0.0, 2.0, 2.0]), (2,1): ('e', (2,0)), (2,2): ('b', [8.0, 0.0, 16.0, 256.0, 0.0, 1.0, 1.0]), (2,3): ('b', [6+0j, 2+0j, 8+0j, 16+0j, 0+0j, 2+0j, 2+0j]), (2,4): ('b', [TE, TE, TE, TE, TE, TE, TE]), (2,5): ('e', (2,4)), (2,6): ('e', (2,4)), (2,7): ('e', (2,0)), (2,8): ('e', (2,0)), # (2+0j) (3,0): ('b', [4+0j, 0+0j, 4+0j, 4+0j, 0+0j, 1+0j, 1+0j]), (3,1): ('e', (3,0)), (3,2): ('b', [6+0j, -2+0j, 8+0j, 16+0j, 2+0j, 0+0j, 0.5+0j]), (3,3): ('b', [4+0j, 0+0j, 4+0j, 4+0j, 0+0j, 1+0j, 1+0j]), (3,4): ('b', [TE, TE, TE, TE, TE, TE, TE]), (3,5): ('e', (3,4)), (3,6): ('e', (3,4)), (3,7): ('e', (3,0)), (3,8): ('e', (3,0)), # [1] (4,0): ('b', [TE, TE, [1, 1], TE, TE, TE, TE]), (4,1): ('e', (4,0)), (4,2): ('b', [TE, TE, TE, TE, TE, TE, TE]), (4,3): ('b', [TE, TE, TE, TE, TE, TE, TE]), (4,4): ('b', [[1, 1], TE, TE, TE, TE, TE, TE]), (4,5): ('s', [TE, TE, TE, TE, TE, TE, TE], [[1, 2], TE, TE, TE, TE, TE, TE]), (4,6): ('b', [TE, TE, TE, TE, TE, TE, TE]), (4,7): ('e', (4,0)), (4,8): ('e', (4,0)), # (2,) (5,0): ('b', [TE, TE, (2, 2), TE, TE, TE, TE]), (5,1): ('e', (5,0)), (5,2): ('b', [TE, TE, TE, TE, TE, TE, TE]), (5,3): ('e', (5,2)), (5,4): ('e', (5,2)), (5,5): ('b', [(2, 2), TE, TE, TE, TE, TE, TE]), (5,6): ('b', [TE, TE, TE, TE, TE, TE, TE]), (5,7): ('e', (5,0)), (5,8): ('e', (5,0)), # None (6,0): ('b', [TE, TE, TE, TE, TE, TE, TE]), (6,1): ('e', (6,0)), (6,2): ('e', (6,0)), (6,3): ('e', (6,0)), (6,4): ('e', (6,0)), (6,5): ('e', (6,0)), (6,6): ('e', (6,0)), (6,7): ('e', (6,0)), (6,8): ('e', (6,0)), # MethodNumber(2) (7,0): ('e', (0,0)), (7,1): ('e', (0,1)), (7,2): ('e', (0,2)), (7,3): ('e', (0,3)), (7,4): ('e', (0,4)), (7,5): ('e', (0,5)), (7,6): ('e', (0,6)), (7,7): ('e', (0,7)), (7,8): ('e', (0,8)), # CoerceNumber(2) (8,0): ('e', (0,0)), (8,1): ('e', (0,1)), (8,2): ('e', (0,2)), (8,3): ('e', (0,3)), (8,4): ('e', (0,4)), (8,5): ('e', (0,5)), (8,6): ('e', (0,6)), (8,7): ('e', (0,7)), (8,8): ('e', (0,8)), } def process_infix_results(): for key in sorted(infix_results): val = infix_results[key] if val[0] == 'e': infix_results[key] = infix_results[val[1]] else: if val[0] == 's': res = (val[1], val[2]) elif val[0] == 'b': res = (val[1], val[1]) for i in range(1): if isinstance(res[i][6], tuple): if 1/2 == 0: # testing with classic (floor) division res[i][6] = res[i][6][0] else: # testing with -Qnew res[i][6] = res[i][6][1] infix_results[key] = res with check_warnings(("classic (int|long) division", DeprecationWarning), quiet=True): process_infix_results() # now infix_results has two lists of results for every pairing. prefix_binops = [ 'divmod' ] prefix_results = [ [(1,0), (1L,0L), (0.0,2.0), ((1+0j),0j), TE, TE, TE, TE, (1,0)], [(1L,0L), (1L,0L), (0.0,2.0), ((1+0j),0j), TE, TE, TE, TE, (1L,0L)], [(2.0,0.0), (2.0,0.0), (1.0,0.0), ((2+0j),0j), TE, TE, TE, TE, (2.0,0.0)], [((1+0j),0j), ((1+0j),0j), (0j,(2+0j)), ((1+0j),0j), TE, TE, TE, TE, ((1+0j),0j)], [TE, TE, TE, TE, TE, TE, TE, TE, TE], [TE, TE, TE, TE, TE, TE, TE, TE, TE], [TE, TE, TE, TE, TE, TE, TE, TE, TE], [TE, TE, TE, TE, TE, TE, TE, TE, TE], [(1,0), (1L,0L), (0.0,2.0), ((1+0j),0j), TE, TE, TE, TE, (1,0)] ] def format_float(value): if abs(value) < 0.01: return '0.0' else: return '%.1f' % value # avoid testing platform fp quirks def format_result(value): if isinstance(value, complex): return '(%s + %sj)' % (format_float(value.real), format_float(value.imag)) elif isinstance(value, float): return format_float(value) return str(value) class CoercionTest(unittest.TestCase): def test_infix_binops(self): for ia, a in enumerate(candidates): for ib, b in enumerate(candidates): results = infix_results[(ia, ib)] for op, res, ires in zip(infix_binops, results[0], results[1]): if res is TE: self.assertRaises(TypeError, eval, 'a %s b' % op, {'a': a, 'b': b}) else: self.assertEqual(format_result(res), format_result(eval('a %s b' % op)), '%s %s %s == %s failed' % (a, op, b, res)) try: z = copy.copy(a) except copy.Error: z = a # assume it has no inplace ops if ires is TE: try: exec 'z %s= b' % op except TypeError: pass else: self.fail("TypeError not raised") else: exec('z %s= b' % op) self.assertEqual(ires, z) def test_prefix_binops(self): for ia, a in enumerate(candidates): for ib, b in enumerate(candidates): for op in prefix_binops: res = prefix_results[ia][ib] if res is TE: self.assertRaises(TypeError, eval, '%s(a, b)' % op, {'a': a, 'b': b}) else: self.assertEqual(format_result(res), format_result(eval('%s(a, b)' % op)), '%s(%s, %s) == %s failed' % (op, a, b, res)) def test_cmptypes(self): # Built-in tp_compare slots expect their arguments to have the # same type, but a user-defined __coerce__ doesn't have to obey. # SF #980352 evil_coercer = CoerceTo(42) # Make sure these don't crash any more self.assertNotEqual(cmp(u'fish', evil_coercer), 0) self.assertNotEqual(cmp(slice(1), evil_coercer), 0) # ...but that this still works class WackyComparer(object): def __cmp__(slf, other): self.assertTrue(other == 42, 'expected evil_coercer, got %r' % other) return 0 __hash__ = None # Invalid cmp makes this unhashable self.assertEqual(cmp(WackyComparer(), evil_coercer), 0) # ...and classic classes too, since that code path is a little different class ClassicWackyComparer: def __cmp__(slf, other): self.assertTrue(other == 42, 'expected evil_coercer, got %r' % other) return 0 self.assertEqual(cmp(ClassicWackyComparer(), evil_coercer), 0) def test_infinite_rec_classic_classes(self): # if __coerce__() returns its arguments reversed it causes an infinite # recursion for classic classes. class Tester: def __coerce__(self, other): return other, self exc = TestFailed("__coerce__() returning its arguments reverse " "should raise RuntimeError") try: Tester() + 1 except (RuntimeError, TypeError): return except: raise exc else: raise exc def test_main(): with check_warnings(("complex divmod.., // and % are deprecated", DeprecationWarning), ("classic (int|long) division", DeprecationWarning), quiet=True): run_unittest(CoercionTest) if __name__ == "__main__": test_main() |