summaryrefslogtreecommitdiffstats
path: root/Objects/abstract.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-06-01 21:05:48 (GMT)
committerGitHub <noreply@github.com>2019-06-01 21:05:48 (GMT)
commitbdbad71b9def0b86433de12cecca022eee91bd9f (patch)
tree147c8a3e08454a95e0e4900388d88f7b65430f63 /Objects/abstract.c
parent1a4d9ffa1aecd7e750195f2be06d3d16c7a3a88f (diff)
downloadcpython-bdbad71b9def0b86433de12cecca022eee91bd9f.zip
cpython-bdbad71b9def0b86433de12cecca022eee91bd9f.tar.gz
cpython-bdbad71b9def0b86433de12cecca022eee91bd9f.tar.bz2
bpo-20092. Use __index__ in constructors of int, float and complex. (GH-13108)
Diffstat (limited to 'Objects/abstract.c')
-rw-r--r--Objects/abstract.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c
index 68d06ed..77d0914 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -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));
}