#1162154: inspect.getmembers() now skips attributes that raise AttributeError,
e.g. a __slots__ attribute which has not been set.
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 1685bfc..8268be1 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -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()
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 300d143..b653f40 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -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)