]> granicus.if.org Git - python/commitdiff
fix inspect.isclass() on instances with a custom __getattr__ #1225107
authorBenjamin Peterson <benjamin@python.org>
Sat, 17 Jan 2009 22:27:54 +0000 (22:27 +0000)
committerBenjamin Peterson <benjamin@python.org>
Sat, 17 Jan 2009 22:27:54 +0000 (22:27 +0000)
Lib/inspect.py
Lib/test/test_inspect.py
Misc/NEWS

index a89d62e6a23ec5b1c2b7a98f9c65f54fda177c92..1453f3b7cf715f8cc6c0c1be81b191b2ec82a8f1 100644 (file)
@@ -62,7 +62,7 @@ def isclass(object):
     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.
index b653f4020042a8777aa9a49499225d616c4d14f0..7312cac0080f65ebbd71c541db50dceed9801082 100644 (file)
@@ -65,7 +65,6 @@ class TestPredicates(IsTestBase):
     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 @@ class TestPredicates(IsTestBase):
         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")
index 2e9e47dbe14d8718e802b4ee1950f4b7c661617f..4c71db1eaf1c8c64a59107c3f9d372b5b4927ac5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -137,6 +137,9 @@ Core and Builtins
 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.