Skip to content
Merged
Changes from 1 commit
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
Next Next commit
Improve performance for hasattr() with unassigned slots.
  • Loading branch information
ZeroIntensity committed Sep 20, 2026
commit 086cb91a043bf3ab89d02ddeeaf166418aa7aac6
16 changes: 16 additions & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1924,6 +1924,22 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
if (descr != NULL) {
f = Py_TYPE(descr)->tp_descr_get;
if (f != NULL && PyDescr_IsData(descr)) {
// gh-157840: When using hasattr(), slot members without assigned values
// need to go through __get__(), which involves creating an AttributeError
// object (which is really slow). There's no way to supply "suppress"
// to the tp_descr_get() function, so we just special-case member
// descriptors here to improve performance.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please simplify the comment, leaving the substantial part?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (suppress && Py_IS_TYPE(descr, &PyMemberDescr_Type)) {
PyMemberDef *member = ((PyMemberDescrObject *)descr)->d_member;
if (member->type == Py_T_OBJECT_EX
&& !(member->flags & Py_AUDIT_READ) &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent formatting. && after and before line split.

PyObject_TypeCheck(obj, PyDescr_TYPE(descr))) {
PyObject **addr = _PyMember_GetOffset(obj, member);
if (FT_ATOMIC_LOAD_PTR(*addr) == NULL) {
goto done;
}
}
}
res = f(descr, obj, (PyObject *)Py_TYPE(obj));
if (res == NULL && suppress &&
PyErr_ExceptionMatches(PyExc_AttributeError)) {
Expand Down