Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-103193: cache calls to inspect._shadowed_dict in inspect.getattr_static #104267

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
merged 4 commits into from
May 7, 2023
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
Prev Previous commit
Next Next commit
Use mro tuple as the cache key
  • Loading branch information
AlexWaygood committed May 7, 2023
commit c26587c462935935eb68172dc179d1d9298e3864
7 changes: 5 additions & 2 deletions 7 Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1795,8 +1795,8 @@ def _check_class(klass, attr):
return _sentinel

@functools.lru_cache()
def _shadowed_dict(klass):
for entry in _static_getmro(klass):
def _shadowed_dict_from_mro_tuple(mro):
for entry in mro:
dunder_dict = _get_dunder_dict_of_class(entry)
if '__dict__' in dunder_dict:
class_dict = dunder_dict['__dict__']
Expand All @@ -1806,6 +1806,9 @@ def _shadowed_dict(klass):
return class_dict
return _sentinel

def _shadowed_dict(klass):
return _shadowed_dict_from_mro_tuple(_static_getmro(klass))
Comment on lines +1809 to +1810
Copy link
Member Author

@AlexWaygood AlexWaygood May 7, 2023

Choose a reason for hiding this comment

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

I considered inlining the calls to _static_getmro, but I thought this made it more readable. It also preserves git blame better, and having to pass through this function doesn't seem to have much impact on performance


def getattr_static(obj, attr, default=_sentinel):
"""Retrieve attributes without triggering dynamic lookup via the
descriptor protocol, __getattr__ or __getattribute__.
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.