summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-03-24 19:28:43 (GMT)
committerGitHub <noreply@github.com>2017-03-24 19:28:43 (GMT)
commit671079ef6063fe227460a6c3114625fb6282bbd0 (patch)
tree1b43f7ca1513741277932d064bcd9367dff88443 /Objects
parentaf7b9ec5c855366feef4c67dc492d64b3baf84ca (diff)
downloadcpython-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.c29
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);