mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
gh-103193: Use LBYL idioms rather than EAFP in inspect.getattr_static
(#103318)
This commit is contained in:
parent
1724553e6e
commit
affedee8bf
1 changed files with 10 additions and 15 deletions
|
@ -1787,11 +1787,8 @@ def _check_instance(obj, attr):
|
||||||
|
|
||||||
def _check_class(klass, attr):
|
def _check_class(klass, attr):
|
||||||
for entry in _static_getmro(klass):
|
for entry in _static_getmro(klass):
|
||||||
if _shadowed_dict(type(entry)) is _sentinel:
|
if _shadowed_dict(type(entry)) is _sentinel and attr in entry.__dict__:
|
||||||
try:
|
|
||||||
return entry.__dict__[attr]
|
return entry.__dict__[attr]
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
return _sentinel
|
return _sentinel
|
||||||
|
|
||||||
def _is_type(obj):
|
def _is_type(obj):
|
||||||
|
@ -1803,11 +1800,9 @@ def _is_type(obj):
|
||||||
|
|
||||||
def _shadowed_dict(klass):
|
def _shadowed_dict(klass):
|
||||||
for entry in _static_getmro(klass):
|
for entry in _static_getmro(klass):
|
||||||
try:
|
dunder_dict = _get_dunder_dict_of_class(entry)
|
||||||
class_dict = _get_dunder_dict_of_class(entry)["__dict__"]
|
if '__dict__' in dunder_dict:
|
||||||
except KeyError:
|
class_dict = dunder_dict['__dict__']
|
||||||
pass
|
|
||||||
else:
|
|
||||||
if not (type(class_dict) is types.GetSetDescriptorType and
|
if not (type(class_dict) is types.GetSetDescriptorType and
|
||||||
class_dict.__name__ == "__dict__" and
|
class_dict.__name__ == "__dict__" and
|
||||||
class_dict.__objclass__ is entry):
|
class_dict.__objclass__ is entry):
|
||||||
|
@ -1850,11 +1845,11 @@ def getattr_static(obj, attr, default=_sentinel):
|
||||||
if obj is klass:
|
if obj is klass:
|
||||||
# for types we check the metaclass too
|
# for types we check the metaclass too
|
||||||
for entry in _static_getmro(type(klass)):
|
for entry in _static_getmro(type(klass)):
|
||||||
if _shadowed_dict(type(entry)) is _sentinel:
|
if (
|
||||||
try:
|
_shadowed_dict(type(entry)) is _sentinel
|
||||||
|
and attr in entry.__dict__
|
||||||
|
):
|
||||||
return entry.__dict__[attr]
|
return entry.__dict__[attr]
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
if default is not _sentinel:
|
if default is not _sentinel:
|
||||||
return default
|
return default
|
||||||
raise AttributeError(attr)
|
raise AttributeError(attr)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue