Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a51623b

Browse files
committed
Fix inspect.getattr_static to work on modules (again).
Closes issue 11813.
1 parent bd206e2 commit a51623b

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

Lib/inspect.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ def _check_instance(obj, attr):
10841084

10851085
def _check_class(klass, attr):
10861086
for entry in _static_getmro(klass):
1087-
if not _shadowed_dict(type(entry)):
1087+
if _shadowed_dict(type(entry)) is _sentinel:
10881088
try:
10891089
return entry.__dict__[attr]
10901090
except KeyError:
@@ -1109,8 +1109,8 @@ def _shadowed_dict(klass):
11091109
if not (type(class_dict) is types.GetSetDescriptorType and
11101110
class_dict.__name__ == "__dict__" and
11111111
class_dict.__objclass__ is entry):
1112-
return True
1113-
return False
1112+
return class_dict
1113+
return _sentinel
11141114

11151115
def getattr_static(obj, attr, default=_sentinel):
11161116
"""Retrieve attributes without triggering dynamic lookup via the
@@ -1126,7 +1126,9 @@ def getattr_static(obj, attr, default=_sentinel):
11261126
instance_result = _sentinel
11271127
if not _is_type(obj):
11281128
klass = type(obj)
1129-
if not _shadowed_dict(klass):
1129+
dict_attr = _shadowed_dict(klass)
1130+
if (dict_attr is _sentinel or
1131+
type(dict_attr) is types.MemberDescriptorType):
11301132
instance_result = _check_instance(obj, attr)
11311133
else:
11321134
klass = obj

Lib/test/test_inspect.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,11 @@ def __init__(self):
10041004
self.assertEqual(inspect.getattr_static(instance, "spam"), 42)
10051005
self.assertFalse(Thing.executed)
10061006

1007+
def test_module(self):
1008+
sentinel = object()
1009+
self.assertIsNot(inspect.getattr_static(sys, "version", sentinel),
1010+
sentinel)
1011+
10071012
class TestGetGeneratorState(unittest.TestCase):
10081013

10091014
def setUp(self):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ Core and Builtins
9797
Library
9898
-------
9999

100+
- Issue #11813: Fix inspect.getattr_static for modules. Patch by Andreas
101+
Stührk.
102+
100103
- Issue #7502: Fix equality comparison for DocTestCase instances. Patch by
101104
Cédric Krier.
102105

0 commit comments

Comments
 (0)