[Python-checkins] r68676 - in python/trunk: Lib/inspect.py Lib/test/test_inspect.py Misc/NEWS

benjamin.peterson python-checkins at python.org
Sat Jan 17 23:27:54 CET 2009


Author: benjamin.peterson
Date: Sat Jan 17 23:27:54 2009
New Revision: 68676

Log:
fix inspect.isclass() on instances with a custom __getattr__ #1225107

Modified:
   python/trunk/Lib/inspect.py
   python/trunk/Lib/test/test_inspect.py
   python/trunk/Misc/NEWS

Modified: python/trunk/Lib/inspect.py
==============================================================================
--- python/trunk/Lib/inspect.py	(original)
+++ python/trunk/Lib/inspect.py	Sat Jan 17 23:27:54 2009
@@ -62,7 +62,7 @@
     Class objects provide these attributes:
         __doc__         documentation string
         __module__      name of module in which this class was defined"""
-    return isinstance(object, types.ClassType) or hasattr(object, '__bases__')
+    return isinstance(object, (type, types.ClassType))
 
 def ismethod(object):
     """Return true if the object is an instance method.

Modified: python/trunk/Lib/test/test_inspect.py
==============================================================================
--- python/trunk/Lib/test/test_inspect.py	(original)
+++ python/trunk/Lib/test/test_inspect.py	Sat Jan 17 23:27:54 2009
@@ -65,7 +65,6 @@
     def test_excluding_predicates(self):
         self.istest(inspect.isbuiltin, 'sys.exit')
         self.istest(inspect.isbuiltin, '[].append')
-        self.istest(inspect.isclass, 'mod.StupidGit')
         self.istest(inspect.iscode, 'mod.spam.func_code')
         self.istest(inspect.isframe, 'tb.tb_frame')
         self.istest(inspect.isfunction, 'mod.spam')
@@ -91,6 +90,18 @@
         self.assert_(inspect.isroutine(mod.spam))
         self.assert_(inspect.isroutine([].count))
 
+    def test_isclass(self):
+        self.istest(inspect.isclass, 'mod.StupidGit')
+        self.assertTrue(inspect.isclass(list))
+
+        class newstyle(object): pass
+        self.assertTrue(inspect.isclass(newstyle))
+
+        class CustomGetattr(object):
+            def __getattr__(self, attr):
+                return None
+        self.assertFalse(inspect.isclass(CustomGetattr()))
+
     def test_get_slot_members(self):
         class C(object):
             __slots__ = ("a", "b")

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sat Jan 17 23:27:54 2009
@@ -137,6 +137,9 @@
 Library
 -------
 
+- Issue #1225107: inspect.isclass() returned True for instances with a custom
+  __getattr__.
+
 - Issue #3997: zipfiles generated with more than 65536 files could not be
   opened with other applications.
 


More information about the Python-checkins mailing list