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

@ -246,6 +246,15 @@ PyFloat_AsDouble(PyObject *op)
nb = Py_TYPE(op)->tp_as_number;
if (nb == NULL || nb->nb_float == NULL) {
if (nb && nb->nb_index) {
PyObject *res = PyNumber_Index(op);
if (!res) {
return -1;
}
double val = PyLong_AsDouble(res);
Py_DECREF(res);
return val;
}
PyErr_Format(PyExc_TypeError, "must be real number, not %.50s",
op->ob_type->tp_name);
return -1;