mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-20092. Use __index__ in constructors of int, float and complex. (GH-13108)
This commit is contained in:
parent
1a4d9ffa1a
commit
bdbad71b9d
15 changed files with 181 additions and 23 deletions
|
@ -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));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue