rewrite this function, which was still accounting for classic classes

This commit is contained in:
Benjamin Peterson 2012-03-21 14:38:11 -04:00
parent 65e32d1f1a
commit d614e707ca

View file

@ -1271,34 +1271,22 @@ PyNumber_AsSsize_t(PyObject *item, PyObject *err)
PyObject * PyObject *
_PyNumber_ConvertIntegralToInt(PyObject *integral, const char* error_format) _PyNumber_ConvertIntegralToInt(PyObject *integral, const char* error_format)
{ {
static PyObject *int_name = NULL; PyNumberMethods *nb;
if (int_name == NULL) { if (PyLong_Check(integral))
int_name = PyUnicode_InternFromString("__int__"); return integral;
if (int_name == NULL) nb = Py_TYPE(integral)->tp_as_number;
return NULL; if (nb->nb_int) {
} PyObject *as_int = nb->nb_int(integral);
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); Py_DECREF(integral);
integral = PyEval_CallObject(int_func, NULL); if (!as_int)
Py_DECREF(int_func); return NULL;
if (integral && !PyLong_Check(integral)) { if (PyLong_Check(as_int))
goto non_integral_error; return as_int;
} Py_DECREF(as_int);
} }
return integral;
non_integral_error:
PyErr_Format(PyExc_TypeError, error_format, Py_TYPE(integral)->tp_name); PyErr_Format(PyExc_TypeError, error_format, Py_TYPE(integral)->tp_name);
Py_DECREF(integral); Py_DECREF(integral);
return NULL; return NULL;
} }