diff options
Diffstat (limited to 'Objects/abstract.c')
-rw-r--r-- | Objects/abstract.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index 755cb0e..b50b43e 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -1242,6 +1242,40 @@ PyNumber_AsSsize_t(PyObject *item, PyObject *err) } +PyObject * +_PyNumber_ConvertIntegralToInt(PyObject *integral, const char* error_format) +{ + static PyObject *int_name = NULL; + if (int_name == NULL) { + int_name = PyUnicode_InternFromString("__int__"); + if (int_name == NULL) + return NULL; + } + + if (integral && !PyLong_Check(integral)) { + /* Don't go through tp_as_number->nb_int to avoid + hitting the classic class fallback to __trunc__. */ + PyObject *int_func = PyObject_GetAttr(integral, int_name); + if (int_func == NULL) { + PyErr_Clear(); /* Raise a different error. */ + goto non_integral_error; + } + Py_DECREF(integral); + integral = PyEval_CallObject(int_func, NULL); + Py_DECREF(int_func); + if (integral && !PyLong_Check(integral)) { + goto non_integral_error; + } + } + return integral; + +non_integral_error: + PyErr_Format(PyExc_TypeError, error_format, Py_TYPE(integral)->tp_name); + Py_DECREF(integral); + return NULL; +} + + /* Add a check for embedded NULL-bytes in the argument. */ static PyObject * long_from_string(const char *s, Py_ssize_t len) @@ -1265,9 +1299,17 @@ PyObject * PyNumber_Long(PyObject *o) { PyNumberMethods *m; + static PyObject *trunc_name = NULL; + PyObject *trunc_func; const char *buffer; Py_ssize_t buffer_len; + if (trunc_name == NULL) { + trunc_name = PyUnicode_InternFromString("__trunc__"); + if (trunc_name == NULL) + return NULL; + } + if (o == NULL) return null_error(); if (PyLong_CheckExact(o)) { @@ -1287,6 +1329,7 @@ PyNumber_Long(PyObject *o) return res; } if (m && m->nb_long) { /* This should include subclasses of long */ + /* Classic classes always take this branch. */ PyObject *res = m->nb_long(o); if (res && !PyLong_Check(res)) { PyErr_Format(PyExc_TypeError, @@ -1299,6 +1342,27 @@ PyNumber_Long(PyObject *o) } if (PyLong_Check(o)) /* A long subclass without nb_long */ return _PyLong_Copy((PyLongObject *)o); + trunc_func = PyObject_GetAttr(o, trunc_name); + if (trunc_func) { + PyObject *truncated = PyEval_CallObject(trunc_func, NULL); + PyObject *int_instance; + Py_DECREF(trunc_func); + /* __trunc__ is specified to return an Integral type, + but long() needs to return a long. */ + int_instance = _PyNumber_ConvertIntegralToInt( + truncated, + "__trunc__ returned non-Integral (type %.200s)"); + return int_instance; + } + PyErr_Clear(); /* It's not an error if o.__trunc__ doesn't exist. */ + + if (PyString_Check(o)) + /* need to do extra error checking that PyLong_FromString() + * doesn't do. In particular long('9.5') must raise an + * exception, not truncate the float. + */ + return long_from_string(PyString_AS_STRING(o), + PyString_GET_SIZE(o)); if (PyUnicode_Check(o)) /* The above check is done in PyLong_FromUnicode(). */ return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o), |