]> granicus.if.org Git - python/commitdiff
Issue 9732: remove use of __class__ in inspect.getattr_static and note the mro except...
authorMichael Foord <fuzzyman@voidspace.org.uk>
Sat, 20 Nov 2010 16:20:16 +0000 (16:20 +0000)
committerMichael Foord <fuzzyman@voidspace.org.uk>
Sat, 20 Nov 2010 16:20:16 +0000 (16:20 +0000)
Doc/library/inspect.rst
Lib/inspect.py
Lib/test/test_inspect.py

index 2f0934840f09ceba5755c3fbb61229eebe4576a9..32e56e5917a61458ad7b5c70a6e9529644765076 100644 (file)
@@ -598,11 +598,13 @@ any of these then you deserve to have everything break anyway):
   member deleted from the class, or a fake `__slots__` attribute
   attached to the instance, or any other monkeying with
   `__slots__`
-* objects that lie about their type by having `__class__` as a
-  descriptor (`getattr_static` traverses the :term:`MRO` of whatever type
-  `obj.__class__` returns instead of the real type)
 * type objects that lie about their :term:`MRO`
 
+.. note::
+
+   Classes that override :data:`~object.__mro__` as a property will have this
+   code executed by `getattr_static`.
+
 Descriptors are not resolved (for example slot descriptors or
 getset descriptors on objects implemented in C). The descriptor
 is returned instead of the underlying attribute.
index 57d8c724d608eb2beb02248816fdf61cb92d7135..97e99aa0cee6e861f31958d213edaf6734046684 100644 (file)
@@ -1092,7 +1092,7 @@ def getattr_static(obj, attr, default=_sentinel):
     instance_result = _sentinel
     if not isinstance(obj, type):
         instance_result = _check_instance(obj, attr)
-        klass = obj.__class__
+        klass = type(obj)
     else:
         klass = obj
 
index 88c57d31e1eb66b2a6d505ddc3dbf34c7021781a..e320c68b2be6995d8c02baa020b0cd32335ed13e 100644 (file)
@@ -855,6 +855,18 @@ class TestGetattrStatic(unittest.TestCase):
         self.assertEqual(inspect.getattr_static(Thing, 'd'), meta.__dict__['d'])
 
 
+    def test_class_as_property(self):
+        class Base(object):
+            foo = 3
+
+        class Something(Base):
+            @property
+            def __class__(self):
+                return object
+
+        self.assertEqual(inspect.getattr_static(Something(), 'foo'), 3)
+        self.assertEqual(inspect.getattr_static(Something, 'foo'), 3)
+
 def test_main():
     run_unittest(
         TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBuggyCases,