Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-102213: Optimizing the Performance of __getattr__ #102213 #102248

Merged
merged 3 commits into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *);
PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) PyObject_GenericTryGetAttr(PyObject *, PyObject *);
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved
PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *, PyObject *, PyObject *);
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(int) PyObject_GenericSetDict(PyObject *, PyObject *, void *);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix performance loss when accessing an object's attributes with `__getattr__` defined.
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,12 @@ PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0);
}

PyObject *
PyObject_GenericTryGetAttr(PyObject *obj, PyObject *name)
{
return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 1);
}

Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved
int
_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
PyObject *value, PyObject *dict)
Expand Down
8 changes: 5 additions & 3 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -8248,14 +8248,16 @@ _Py_slot_tp_getattr_hook(PyObject *self, PyObject *name)
(Py_IS_TYPE(getattribute, &PyWrapperDescr_Type) &&
((PyWrapperDescrObject *)getattribute)->d_wrapped ==
(void *)PyObject_GenericGetAttr))
res = PyObject_GenericGetAttr(self, name);
/* finding nothing is reasonable when __getattr__ is defined */
res = PyObject_GenericTryGetAttr(self, name);
else {
Py_INCREF(getattribute);
res = call_attribute(self, getattribute, name);
Py_DECREF(getattribute);
}
if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
if (res == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_Clear();
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved
res = call_attribute(self, getattr, name);
}
Py_DECREF(getattr);
Expand Down