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

@ -1373,6 +1373,13 @@ PyNumber_Long(PyObject *o)
}
return result;
}
if (m && m->nb_index) {
result = _PyLong_FromNbIndexOrNbInt(o);
if (result != NULL && !PyLong_CheckExact(result)) {
Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
}
return result;
}
trunc_func = _PyObject_LookupSpecial(o, &PyId___trunc__);
if (trunc_func) {
result = _PyObject_CallNoArg(trunc_func);
@ -1480,6 +1487,18 @@ PyNumber_Float(PyObject *o)
Py_DECREF(res);
return PyFloat_FromDouble(val);
}
if (m && m->nb_index) {
PyObject *res = PyNumber_Index(o);
if (!res) {
return NULL;
}
double val = PyLong_AsDouble(res);
Py_DECREF(res);
if (val == -1.0 && PyErr_Occurred()) {
return NULL;
}
return PyFloat_FromDouble(val);
}
if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */
return PyFloat_FromDouble(PyFloat_AS_DOUBLE(o));
}