]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix inspect.getattr_static to work on modules (again).
authorMichael Foord <michael@voidspace.org.uk>
Sun, 18 Dec 2011 22:01:40 +0000 (22:01 +0000)
committerMichael Foord <michael@voidspace.org.uk>
Sun, 18 Dec 2011 22:01:40 +0000 (22:01 +0000)
Closes issue 11813.

Lib/inspect.py
Lib/test/test_inspect.py
Misc/NEWS

index 4e17f1b591cd8035ec378c5661b237c8c5b3c069..521d2a67d4421b46105fd1822072e75c5bf0a0ce 100644 (file)
@@ -1084,7 +1084,7 @@ def _check_instance(obj, attr):
 
 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:
@@ -1109,8 +1109,8 @@ def _shadowed_dict(klass):
             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
@@ -1126,7 +1126,9 @@ def getattr_static(obj, attr, default=_sentinel):
     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
index 7666fe43a7339b4904fe73461949fc4e4fd81f70..abbdc9f7c97069db5cc7559630938736ff3e525a 100644 (file)
@@ -1004,6 +1004,11 @@ class TestGetattrStatic(unittest.TestCase):
         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):
index 95c814748b87eb6f1e9630ab92675c7742a93b28..346326babecd516358a69b9c3554ee322652aecf 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -97,6 +97,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #11813: Fix inspect.getattr_static for modules. Patch by Andreas 
+  Stührk.
+
 - Issue #7502: Fix equality comparison for DocTestCase instances.  Patch by
   Cédric Krier.