Repository navigation
gh-157840: Special-case member descriptors when using hasattr
#157866
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
Merged
ZeroIntensity
merged 5 commits into
python:main
from
ZeroIntensity:performance/hasattr-unassigned-slots
Sep 20, 2026
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
086cb91
Improve performance for hasattr() with unassigned slots.
ZeroIntensity 74c0828
Add blurb.
ZeroIntensity cc752b9
Simplify comment.
ZeroIntensity 9878c2b
Adjust formatting.
ZeroIntensity 8e74138
Mention getattr() in the blurb.
ZeroIntensity File filter
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
commit 086cb91a043bf3ab89d02ddeeaf166418aa7aac6
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
| 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) && | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent formatting. |
||
| 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)) { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done