diff --git a/Lib/inspect.py b/Lib/inspect.py --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1080,7 +1080,7 @@ def _check_class(klass, attr): for entry in _static_getmro(klass): - if not _shadowed_dict(type(entry)): + if _shadowed_dict(type(entry)) is _sentinel: try: return entry.__dict__[attr] except KeyError: @@ -1105,8 +1105,8 @@ if not (type(class_dict) is types.GetSetDescriptorType and class_dict.__name__ == "__dict__" and class_dict.__objclass__ is entry): - return True - return False + return class_dict + return _sentinel def getattr_static(obj, attr, default=_sentinel): """Retrieve attributes without triggering dynamic lookup via the @@ -1122,7 +1122,9 @@ instance_result = _sentinel if not _is_type(obj): klass = type(obj) - if not _shadowed_dict(klass): + dict_attr = _shadowed_dict(klass) + if (dict_attr is _sentinel or + type(dict_attr) is types.MemberDescriptorType): instance_result = _check_instance(obj, attr) else: klass = obj diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -987,6 +987,11 @@ self.assertEqual(inspect.getattr_static(instance, "spam"), 42) self.assertFalse(Thing.executed) + def test_module(self): + sentinel = object() + self.assertIsNot(inspect.getattr_static(sys, "version", sentinel), + sentinel) + class TestGetGeneratorState(unittest.TestCase): def setUp(self):