]> granicus.if.org Git - python/commitdiff
Issue #16290: __complex__ must now always return an instance of complex.
authorMark Dickinson <mdickinson@enthought.com>
Wed, 14 Nov 2012 17:08:31 +0000 (17:08 +0000)
committerMark Dickinson <mdickinson@enthought.com>
Wed, 14 Nov 2012 17:08:31 +0000 (17:08 +0000)
Lib/test/test_complex.py
Misc/NEWS
Objects/complexobject.c

index 6b34ddc0a68a6813110b0790841e6a1409d52c90..2a85bf44c487c4e3877812100361b8f58e33d4de 100644 (file)
@@ -221,6 +221,8 @@ class ComplexTest(unittest.TestCase):
         self.assertRaises(TypeError, complex, OS(None))
         self.assertRaises(TypeError, complex, NS(None))
         self.assertRaises(TypeError, complex, {})
+        self.assertRaises(TypeError, complex, NS(1.5))
+        self.assertRaises(TypeError, complex, NS(1))
 
         self.assertAlmostEqual(complex("1+10j"), 1+10j)
         self.assertAlmostEqual(complex(10), 10+0j)
index 593773d3990d1613ecbf94f68621f381817a2a6b..3ac1d3566e5230e0ae086f4c84a447a212acbca2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.4.0 Alpha 1?
 Core and Builtins
 -----------------
 
+- Issue #16290: A float return value from the __complex__ special method is no
+  longer accepted in the complex() constructor.
+
 - Issue #16416: On Mac OS X, operating system data are now always
   encoded/decoded to/from UTF-8/surrogateescape, instead of the locale encoding
   (which may be ASCII if no locale environment variable is set), to avoid
index 403c60c917d7a1910de0e3992588573846ea91b6..355b063f287dcc70d9efb49052b0bd05b2b9f263 100644 (file)
@@ -271,6 +271,12 @@ try_complex_special_method(PyObject *op) {
     if (f) {
         PyObject *res = PyObject_CallFunctionObjArgs(f, NULL);
         Py_DECREF(f);
+        if (res != NULL && !PyComplex_Check(res)) {
+            PyErr_SetString(PyExc_TypeError,
+                "__complex__ should return a complex object");
+            Py_DECREF(res);
+            return NULL;
+        }
         return res;
     }
     return NULL;
@@ -296,12 +302,6 @@ PyComplex_AsCComplex(PyObject *op)
     newop = try_complex_special_method(op);
 
     if (newop) {
-        if (!PyComplex_Check(newop)) {
-            PyErr_SetString(PyExc_TypeError,
-                "__complex__ should return a complex object");
-            Py_DECREF(newop);
-            return cv;
-        }
         cv = ((PyComplexObject *)newop)->cval;
         Py_DECREF(newop);
         return cv;