diff -r 22c8e6d71529 Lib/test/test_complex.py --- a/Lib/test/test_complex.py Sun Oct 28 10:23:08 2012 +0000 +++ b/Lib/test/test_complex.py Sun Oct 28 11:44:44 2012 +0000 @@ -221,6 +221,8 @@ 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) diff -r 22c8e6d71529 Misc/NEWS --- a/Misc/NEWS Sun Oct 28 10:23:08 2012 +0000 +++ b/Misc/NEWS Sun Oct 28 11:44:44 2012 +0000 @@ -10,6 +10,9 @@ Core and Builtins ----------------- +- Issue #16290: A float return value from the __complex__ special method is no + longer accepted in the complex() constructor. + - Issue #16241: Document -X faulthandler command line option. Patch by Marek Ć uppa. diff -r 22c8e6d71529 Objects/complexobject.c --- a/Objects/complexobject.c Sun Oct 28 10:23:08 2012 +0000 +++ b/Objects/complexobject.c Sun Oct 28 11:44:44 2012 +0000 @@ -271,6 +271,12 @@ 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 @@ 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;