mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
convert some more special methods to use _PyObject_LookupSpecial
This commit is contained in:
parent
f9b01fe692
commit
af1692a266
3 changed files with 30 additions and 28 deletions
|
@ -93,7 +93,7 @@ Py_ssize_t
|
|||
_PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
|
||||
{
|
||||
static PyObject *hintstrobj = NULL;
|
||||
PyObject *ro;
|
||||
PyObject *ro, *hintmeth;
|
||||
Py_ssize_t rv;
|
||||
|
||||
/* try o.__len__() */
|
||||
|
@ -107,20 +107,15 @@ _PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
|
|||
PyErr_Clear();
|
||||
}
|
||||
|
||||
/* cache a hashed version of the attribute string */
|
||||
if (hintstrobj == NULL) {
|
||||
hintstrobj = PyString_InternFromString("__length_hint__");
|
||||
if (hintstrobj == NULL)
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* try o.__length_hint__() */
|
||||
ro = PyObject_CallMethodObjArgs(o, hintstrobj, NULL);
|
||||
hintmeth = _PyObject_LookupSpecial(o, "__length_hint__", &hintstrobj);
|
||||
if (hintmeth == NULL)
|
||||
return defaultvalue;
|
||||
ro = PyObject_CallFunctionObjArgs(hintmeth, NULL);
|
||||
Py_DECREF(hintmeth);
|
||||
if (ro == NULL) {
|
||||
if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
|
||||
!PyErr_ExceptionMatches(PyExc_AttributeError))
|
||||
return -1;
|
||||
PyErr_Clear();
|
||||
if (!PyErr_ExceptionMatches(PyExc_TypeError))
|
||||
return -1;
|
||||
return defaultvalue;
|
||||
}
|
||||
rv = PyLong_Check(ro) ? PyLong_AsSsize_t(ro) : defaultvalue;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue