mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-32544: Speed up hasattr() and getattr() (GH-5173)
AttributeError was raised always when attribute is not found. This commit skip raising AttributeError when `tp_getattro` is `PyObject_GenericGetAttr`. It makes hasattr() and getattr() about 4x faster when attribute is not found.
This commit is contained in:
parent
b44c5169f6
commit
378edee0a3
5 changed files with 71 additions and 19 deletions
|
@ -925,13 +925,15 @@ local_getattro(localobject *self, PyObject *name)
|
|||
|
||||
if (Py_TYPE(self) != &localtype)
|
||||
/* use generic lookup for subtypes */
|
||||
return _PyObject_GenericGetAttrWithDict((PyObject *)self, name, ldict);
|
||||
return _PyObject_GenericGetAttrWithDict(
|
||||
(PyObject *)self, name, ldict, 0);
|
||||
|
||||
/* Optimization: just look in dict ourselves */
|
||||
value = PyDict_GetItem(ldict, name);
|
||||
if (value == NULL)
|
||||
/* Fall back on generic to get __class__ and __dict__ */
|
||||
return _PyObject_GenericGetAttrWithDict((PyObject *)self, name, ldict);
|
||||
return _PyObject_GenericGetAttrWithDict(
|
||||
(PyObject *)self, name, ldict, 0);
|
||||
|
||||
Py_INCREF(value);
|
||||
return value;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue