diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2010-02-21 12:57:35 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2010-02-21 12:57:35 (GMT) |
commit | 82b34c5dbebdd4c39dd310056b20e89d2bed63ed (patch) | |
tree | 7a53145072bdd13dbc35fea511dda329b6e0836c /Lib/test | |
parent | 51f1204590ebe00554ad30d6abf0e723e2ee1b65 (diff) | |
download | cpython-82b34c5dbebdd4c39dd310056b20e89d2bed63ed.zip cpython-82b34c5dbebdd4c39dd310056b20e89d2bed63ed.tar.gz cpython-82b34c5dbebdd4c39dd310056b20e89d2bed63ed.tar.bz2 |
Issue #5211: Fix complex type to avoid implicit calls to
complex.__coerce__. Thanks Meador Inge for the patch.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_complex.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index 199caf0..2cf9a9e 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -358,6 +358,61 @@ class ComplexTest(unittest.TestCase): self.assertAlmostEqual(complex(complex1(1j)), 2j) self.assertRaises(TypeError, complex, complex2(1j)) + def test_subclass(self): + class xcomplex(complex): + def __add__(self,other): + return xcomplex(complex(self) + other) + __radd__ = __add__ + + def __sub__(self,other): + return xcomplex(complex(self) + other) + __rsub__ = __sub__ + + def __mul__(self,other): + return xcomplex(complex(self) * other) + __rmul__ = __mul__ + + def __div__(self,other): + return xcomplex(complex(self) / other) + + def __rdiv__(self,other): + return xcomplex(other / complex(self)) + + __truediv__ = __div__ + __rtruediv__ = __rdiv__ + + def __floordiv__(self,other): + return xcomplex(complex(self) // other) + + def __rfloordiv__(self,other): + return xcomplex(other // complex(self)) + + def __pow__(self,other): + return xcomplex(complex(self) ** other) + + def __rpow__(self,other): + return xcomplex(other ** complex(self) ) + + def __mod__(self,other): + return xcomplex(complex(self) % other) + + def __rmod__(self,other): + return xcomplex(other % complex(self)) + + infix_binops = ('+', '-', '*', '**', '%', '//', '/') + xcomplex_values = (xcomplex(1), xcomplex(123.0), + xcomplex(-10+2j), xcomplex(3+187j), + xcomplex(3-78j)) + test_values = (1, 123.0, 10-19j, xcomplex(1+2j), + xcomplex(1+87j), xcomplex(10+90j)) + + for op in infix_binops: + for x in xcomplex_values: + for y in test_values: + a = 'x %s y' % op + b = 'y %s x' % op + self.assertTrue(type(eval(a)) is type(eval(b)) is xcomplex) + def test_hash(self): for x in xrange(-30, 30): self.assertEqual(hash(x), hash(complex(x, 0))) |