diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-03-24 19:28:43 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-24 19:28:43 (GMT) |
commit | 671079ef6063fe227460a6c3114625fb6282bbd0 (patch) | |
tree | 1b43f7ca1513741277932d064bcd9367dff88443 /Objects | |
parent | af7b9ec5c855366feef4c67dc492d64b3baf84ca (diff) | |
download | cpython-671079ef6063fe227460a6c3114625fb6282bbd0.zip cpython-671079ef6063fe227460a6c3114625fb6282bbd0.tar.gz cpython-671079ef6063fe227460a6c3114625fb6282bbd0.tar.bz2 |
bpo-29894: Deprecate returning an instance of complex subclass from __complex__. (#798)
In a future versions of Python this can be an error.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/complexobject.c | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/Objects/complexobject.c b/Objects/complexobject.c index 773ddb3..5ebb504 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -274,7 +274,8 @@ PyComplex_ImagAsDouble(PyObject *op) } static PyObject * -try_complex_special_method(PyObject *op) { +try_complex_special_method(PyObject *op) +{ PyObject *f; _Py_IDENTIFIER(__complex__); @@ -282,9 +283,22 @@ try_complex_special_method(PyObject *op) { 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; } @@ -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)); cr.real = PyFloat_AsDouble(tmp); cr.imag = 0.0; Py_DECREF(tmp); |