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-103193: Use LBYL idioms rather than EAFP in inspect.getattr_static #103318

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Changes from all 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
25 changes: 10 additions & 15 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1787,11 +1787,8 @@ def _check_instance(obj, attr):

def _check_class(klass, attr):
for entry in _static_getmro(klass):
if _shadowed_dict(type(entry)) is _sentinel:
try:
return entry.__dict__[attr]
except KeyError:
pass
if _shadowed_dict(type(entry)) is _sentinel and attr in entry.__dict__:
return entry.__dict__[attr]
return _sentinel

def _is_type(obj):
Expand All @@ -1803,11 +1800,9 @@ def _is_type(obj):

def _shadowed_dict(klass):
for entry in _static_getmro(klass):
try:
class_dict = _get_dunder_dict_of_class(entry)["__dict__"]
except KeyError:
pass
else:
dunder_dict = _get_dunder_dict_of_class(entry)
if '__dict__' in dunder_dict:
class_dict = dunder_dict['__dict__']
if not (type(class_dict) is types.GetSetDescriptorType and
class_dict.__name__ == "__dict__" and
class_dict.__objclass__ is entry):
Expand Down Expand Up @@ -1850,11 +1845,11 @@ def getattr_static(obj, attr, default=_sentinel):
if obj is klass:
# for types we check the metaclass too
for entry in _static_getmro(type(klass)):
if _shadowed_dict(type(entry)) is _sentinel:
try:
return entry.__dict__[attr]
except KeyError:
pass
if (
_shadowed_dict(type(entry)) is _sentinel
and attr in entry.__dict__
):
return entry.__dict__[attr]
if default is not _sentinel:
return default
raise AttributeError(attr)
Expand Down