Skip to content

bpo-29894: Deprecate returning an instance of complex subclass from __complex__ #798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Lib/test/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,9 @@ class complex2(complex):
def __complex__(self):
return None

self.assertAlmostEqual(complex(complex0(1j)), 42j)
self.assertAlmostEqual(complex(complex1(1j)), 2j)
self.assertEqual(complex(complex0(1j)), 42j)
with self.assertWarns(DeprecationWarning):
self.assertEqual(complex(complex1(1j)), 2j)
self.assertRaises(TypeError, complex, complex2(1j))

@support.requires_IEEE_754
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_getargs2.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ def test_D(self):
self.assertEqual(getargs_D(ComplexSubclass(7.5+0.25j)), 7.5+0.25j)
self.assertEqual(getargs_D(ComplexSubclass2(7.5+0.25j)), 7.5+0.25j)
self.assertRaises(TypeError, getargs_D, BadComplex())
self.assertEqual(getargs_D(BadComplex2()), 4.25+0.5j)
with self.assertWarns(DeprecationWarning):
self.assertEqual(getargs_D(BadComplex2()), 4.25+0.5j)
self.assertEqual(getargs_D(BadComplex3(7.5+0.25j)), 7.5+0.25j)

for x in (DBL_MIN, -DBL_MIN, DBL_MAX, -DBL_MAX, INF, -INF):
Expand Down
4 changes: 4 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ What's New in Python 3.7.0 alpha 1?
Core and Builtins
-----------------

- bpo-29894: The deprecation warning is emitted if __complex__ returns an
instance of a strict subclass of complex. In a future versions of Python
this can be an error.

- bpo-29859: Show correct error messages when any of the pthread_* calls in
thread_pthread.h fails.

Expand Down
29 changes: 19 additions & 10 deletions Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,17 +274,31 @@ PyComplex_ImagAsDouble(PyObject *op)
}

static PyObject *
try_complex_special_method(PyObject *op) {
try_complex_special_method(PyObject *op)
{
PyObject *f;
_Py_IDENTIFIER(__complex__);

f = _PyObject_LookupSpecial(op, &PyId___complex__);
if (f) {
PyObject *res = _PyObject_CallNoArg(f);
Py_DECREF(f);
if (res != NULL && !PyComplex_Check(res)) {
PyErr_SetString(PyExc_TypeError,
"__complex__ should return a complex object");
if (!res || PyComplex_CheckExact(res)) {
return res;
}
if (!PyComplex_Check(res)) {
PyErr_Format(PyExc_TypeError,
"__complex__ returned non-complex (type %.200s)",
res->ob_type->tp_name);
Py_DECREF(res);
return NULL;
}
/* Issue #29894: warn if 'res' not of exact type complex. */
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
"__complex__ returned non-complex (type %.200s). "
"The ability to return an instance of a strict subclass of complex "
"is deprecated, and may be removed in a future version of Python.",
res->ob_type->tp_name)) {
Py_DECREF(res);
return NULL;
}
Expand Down Expand Up @@ -1030,12 +1044,7 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
}
if (tmp == NULL)
return NULL;
if (!PyFloat_Check(tmp)) {
PyErr_SetString(PyExc_TypeError,
"float(r) didn't return a float");
Py_DECREF(tmp);
return NULL;
}
assert(PyFloat_Check(tmp));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intend to include this change in this PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be included in bpo-26983, but it was not. This PR is good opportunity to make this change since it is related to complex.__new__.

Do you suggest to exclude this change from the PR and make a separate PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the change is fine. I just wasn't sure whether you'd intended to include it as part of this PR or not.

cr.real = PyFloat_AsDouble(tmp);
cr.imag = 0.0;
Py_DECREF(tmp);
Expand Down