[Python-checkins] r86575 - in python/branches/py3k: Doc/library/inspect.rst Lib/inspect.py Lib/test/test_inspect.py

michael.foord python-checkins at python.org
Sat Nov 20 17:20:16 CET 2010


Author: michael.foord
Date: Sat Nov 20 17:20:16 2010
New Revision: 86575

Log:
Issue 9732: remove use of __class__ in inspect.getattr_static and note the mro exception to code execution

Modified:
   python/branches/py3k/Doc/library/inspect.rst
   python/branches/py3k/Lib/inspect.py
   python/branches/py3k/Lib/test/test_inspect.py

Modified: python/branches/py3k/Doc/library/inspect.rst
==============================================================================
--- python/branches/py3k/Doc/library/inspect.rst	(original)
+++ python/branches/py3k/Doc/library/inspect.rst	Sat Nov 20 17:20:16 2010
@@ -598,11 +598,13 @@
   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.

Modified: python/branches/py3k/Lib/inspect.py
==============================================================================
--- python/branches/py3k/Lib/inspect.py	(original)
+++ python/branches/py3k/Lib/inspect.py	Sat Nov 20 17:20:16 2010
@@ -1092,7 +1092,7 @@
     instance_result = _sentinel
     if not isinstance(obj, type):
         instance_result = _check_instance(obj, attr)
-        klass = obj.__class__
+        klass = type(obj)
     else:
         klass = obj
 

Modified: python/branches/py3k/Lib/test/test_inspect.py
==============================================================================
--- python/branches/py3k/Lib/test/test_inspect.py	(original)
+++ python/branches/py3k/Lib/test/test_inspect.py	Sat Nov 20 17:20:16 2010
@@ -855,6 +855,18 @@
         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,


More information about the Python-checkins mailing list