summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_complex.py
diff options
context:
space:
mode:
authorBrett Cannon <bcannon@gmail.com>2005-04-26 03:45:26 (GMT)
committerBrett Cannon <bcannon@gmail.com>2005-04-26 03:45:26 (GMT)
commitc3647ac93e2a38762de8a23b1d94a6380e9ad468 (patch)
treea7e00a7e8f70ee226fdeb3d229b9734d5b17a344 /Lib/test/test_complex.py
parentd7c795e72966f7c72b94b919f3539be66495e6c3 (diff)
downloadcpython-c3647ac93e2a38762de8a23b1d94a6380e9ad468.zip
cpython-c3647ac93e2a38762de8a23b1d94a6380e9ad468.tar.gz
cpython-c3647ac93e2a38762de8a23b1d94a6380e9ad468.tar.bz2
Make subclasses of int, long, complex, float, and unicode perform type
conversion using the proper magic slot (e.g., __int__()). Also move conversion code out of PyNumber_*() functions in the C API into the nb_* function. Applied patch #1109424. Thanks Walter Doewald.
Diffstat (limited to 'Lib/test/test_complex.py')
-rw-r--r--Lib/test/test_complex.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py
index 15f4b65..70e91c1 100644
--- a/Lib/test/test_complex.py
+++ b/Lib/test/test_complex.py
@@ -273,6 +273,28 @@ class ComplexTest(unittest.TestCase):
self.assertAlmostEqual(complex(real=float2(17.), imag=float2(23.)), 17+23j)
self.assertRaises(TypeError, complex, float2(None))
+ class complex0(complex):
+ """Test usage of __complex__() when inheriting from 'complex'"""
+ def __complex__(self):
+ return 42j
+
+ class complex1(complex):
+ """Test usage of __complex__() with a __new__() method"""
+ def __new__(self, value=0j):
+ return complex.__new__(self, 2*value)
+ def __complex__(self):
+ return self
+
+ class complex2(complex):
+ """Make sure that __complex__() calls fail if anything other than a
+ complex is returned"""
+ def __complex__(self):
+ return None
+
+ self.assertAlmostEqual(complex(complex0(1j)), 42j)
+ self.assertAlmostEqual(complex(complex1(1j)), 2j)
+ self.assertRaises(TypeError, complex, complex2(1j))
+
def test_hash(self):
for x in xrange(-30, 30):
self.assertEqual(hash(x), hash(complex(x, 0)))