]> granicus.if.org Git - python/commitdiff
Issue 9732: __class__ no longer checked on objects by getattr_static
authorMichael Foord <fuzzyman@voidspace.org.uk>
Sat, 20 Nov 2010 16:58:30 +0000 (16:58 +0000)
committerMichael Foord <fuzzyman@voidspace.org.uk>
Sat, 20 Nov 2010 16:58:30 +0000 (16:58 +0000)
Lib/inspect.py
Lib/test/test_inspect.py

index 2f05829b3c93e8fdc9335a5ca250cd5799112d1d..241cd08bffd0d02c4d928e70f89bc93d24178c51 100644 (file)
@@ -1080,6 +1080,13 @@ def _check_class(klass, attr):
             pass
     return _sentinel
 
+def _is_type(obj):
+    try:
+        _static_getmro(obj)
+    except TypeError:
+        return False
+    return True
+
 
 def getattr_static(obj, attr, default=_sentinel):
     """Retrieve attributes without triggering dynamic lookup via the
@@ -1093,7 +1100,7 @@ def getattr_static(obj, attr, default=_sentinel):
        documentation for details.
     """
     instance_result = _sentinel
-    if not isinstance(obj, type):
+    if not _is_type(obj):
         instance_result = _check_instance(obj, attr)
         klass = type(obj)
     else:
index b3e131ca102e651f8ec4d9a771057f7b4d01b314..df480b8b9dd23060410179c3193b774a85c4f28d 100644 (file)
@@ -860,11 +860,15 @@ class TestGetattrStatic(unittest.TestCase):
             foo = 3
 
         class Something(Base):
+            executed = False
             @property
             def __class__(self):
+                self.executed = True
                 return object
 
-        self.assertEqual(inspect.getattr_static(Something(), 'foo'), 3)
+        instance = Something()
+        self.assertEqual(inspect.getattr_static(instance, 'foo'), 3)
+        self.assertFalse(instance.executed)
         self.assertEqual(inspect.getattr_static(Something, 'foo'), 3)
 
     def test_mro_as_property(self):