From: Benjamin Peterson Date: Wed, 21 Mar 2012 18:38:11 +0000 (-0400) Subject: rewrite this function, which was still accounting for classic classes X-Git-Tag: v3.3.0a2~97^2~3 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d614e707caafc87bd644c1da51ea817c3df54d7b;p=python rewrite this function, which was still accounting for classic classes --- diff --git a/Objects/abstract.c b/Objects/abstract.c index 924ccd1ffe..345b96acce 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -1271,34 +1271,22 @@ 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; - } + PyNumberMethods *nb; + if (PyLong_Check(integral)) + return integral; + nb = Py_TYPE(integral)->tp_as_number; + if (nb->nb_int) { + PyObject *as_int = nb->nb_int(integral); Py_DECREF(integral); - integral = PyEval_CallObject(int_func, NULL); - Py_DECREF(int_func); - if (integral && !PyLong_Check(integral)) { - goto non_integral_error; - } + if (!as_int) + return NULL; + if (PyLong_Check(as_int)) + return as_int; + Py_DECREF(as_int); } - return integral; - -non_integral_error: PyErr_Format(PyExc_TypeError, error_format, Py_TYPE(integral)->tp_name); Py_DECREF(integral); - return NULL; + return NULL; }