Skip to content

Commit ac81fd6

Browse files
committed
Optimize the performance of __getattr__
This is the corrected version of pythonGH-102248
1 parent af53046 commit ac81fd6

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

Objects/typeobject.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8257,17 +8257,22 @@ _Py_slot_tp_getattr_hook(PyObject *self, PyObject *name)
82578257
if (getattribute == NULL ||
82588258
(Py_IS_TYPE(getattribute, &PyWrapperDescr_Type) &&
82598259
((PyWrapperDescrObject *)getattribute)->d_wrapped ==
8260-
(void *)PyObject_GenericGetAttr))
8261-
res = PyObject_GenericGetAttr(self, name);
8262-
else {
8260+
(void *)PyObject_GenericGetAttr)) {
8261+
res = _PyObject_GenericGetAttrWithDict(self, name, NULL, 1);
8262+
/* if no error has occureed, then it must be suppressed by us */
8263+
if (res == NULL && !PyErr_Occurred()) {
8264+
res = call_attribute(self, getattr, name);
8265+
}
8266+
} else {
82638267
Py_INCREF(getattribute);
82648268
res = call_attribute(self, getattribute, name);
82658269
Py_DECREF(getattribute);
8270+
if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
8271+
PyErr_Clear();
8272+
res = call_attribute(self, getattr, name);
8273+
}
82668274
}
8267-
if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
8268-
PyErr_Clear();
8269-
res = call_attribute(self, getattr, name);
8270-
}
8275+
82718276
Py_DECREF(getattr);
82728277
return res;
82738278
}

0 commit comments

Comments
 (0)