bpo-20092. Use __index__ in constructors of int, float and complex. (GH-13108)

This commit is contained in:
Serhiy Storchaka 2019-06-02 00:05:48 +03:00 committed by GitHub
parent 1a4d9ffa1a
commit bdbad71b9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 181 additions and 23 deletions

View file

@ -984,7 +984,7 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
}
nbr = r->ob_type->tp_as_number;
if (nbr == NULL || nbr->nb_float == NULL) {
if (nbr == NULL || (nbr->nb_float == NULL && nbr->nb_index == NULL)) {
PyErr_Format(PyExc_TypeError,
"complex() first argument must be a string or a number, "
"not '%.200s'",
@ -996,7 +996,7 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
}
if (i != NULL) {
nbi = i->ob_type->tp_as_number;
if (nbi == NULL || nbi->nb_float == NULL) {
if (nbi == NULL || (nbi->nb_float == NULL && nbi->nb_index == NULL)) {
PyErr_Format(PyExc_TypeError,
"complex() second argument must be a number, "
"not '%.200s'",
@ -1052,7 +1052,7 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
/* The "imag" part really is entirely imaginary, and
contributes nothing in the real direction.
Just treat it as a double. */
tmp = (*nbi->nb_float)(i);
tmp = PyNumber_Float(i);
if (tmp == NULL)
return NULL;
ci.real = PyFloat_AsDouble(tmp);