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

amaury.forgeotdarc python-checkins at python.org
Wed Jan 14 00:39:22 CET 2009


Author: amaury.forgeotdarc
Date: Wed Jan 14 00:39:22 2009
New Revision: 68596

Log:
#1162154: inspect.getmembers() now skips attributes that raise AttributeError,
e.g. a __slots__ attribute which has not been set.


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	Wed Jan 14 00:39:22 2009
@@ -253,7 +253,10 @@
     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()

Modified: python/trunk/Lib/test/test_inspect.py
==============================================================================
--- python/trunk/Lib/test/test_inspect.py	(original)
+++ python/trunk/Lib/test/test_inspect.py	Wed Jan 14 00:39:22 2009
@@ -91,6 +91,17 @@
         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)

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Wed Jan 14 00:39:22 2009
@@ -137,7 +137,10 @@
 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.


More information about the Python-checkins mailing list