]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#1162154: inspect.getmembers() now skips attributes that raise AttributeError,
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Tue, 13 Jan 2009 23:39:22 +0000 (23:39 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Tue, 13 Jan 2009 23:39:22 +0000 (23:39 +0000)
e.g. a __slots__ attribute which has not been set.

Lib/inspect.py
Lib/test/test_inspect.py
Misc/NEWS

index 1685bfc98d415d799f7ab3b421b4991439a48bfc..8268be134f5d64880a2a917c4b1c9f0f092060a0 100644 (file)
@@ -253,7 +253,10 @@ def getmembers(object, predicate=None):
     Optionally, only return members that satisfy a given predicate."""
     results = []
     for key in dir(object):
-        value = getattr(object, key)
+        try:
+            value = getattr(object, key)
+        except AttributeError:
+            continue
         if not predicate or predicate(value):
             results.append((key, value))
     results.sort()
index 300d1436f20fb76455fe10a5af83c7ab56831f87..b653f4020042a8777aa9a49499225d616c4d14f0 100644 (file)
@@ -91,6 +91,17 @@ class TestPredicates(IsTestBase):
         self.assert_(inspect.isroutine(mod.spam))
         self.assert_(inspect.isroutine([].count))
 
+    def test_get_slot_members(self):
+        class C(object):
+            __slots__ = ("a", "b")
+
+        x = C()
+        x.a = 42
+        members = dict(inspect.getmembers(x))
+        self.assert_('a' in members)
+        self.assert_('b' not in members)
+
+
 class TestInterpreterStack(IsTestBase):
     def __init__(self, *args, **kwargs):
         unittest.TestCase.__init__(self, *args, **kwargs)
index 79393235841da43f81007e275a6ae817c92b343f..9ced73d54d09038af8dcd61c0e53cd8de41a0641 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -137,7 +137,10 @@ Core and Builtins
 Library
 -------
 
-- Issue #1696199:  Add collections.Counter() for rapid and convenient
+- Issue #1162154: inspect.getmembers() now skips attributes that raise
+  AttributeError, e.g. a __slots__ attribute which has not been set.
+
+- Issue #1696199: Add collections.Counter() for rapid and convenient
   counting.
 
 - Issue #3860: GzipFile and BZ2File now support the context manager protocol.