Skip to content

bpo-35108: handle exceptions in inspect.getmembers #18330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,9 @@ def getmembers(object, predicate=None):
for k, v in base.__dict__.items():
if isinstance(v, types.DynamicClassAttribute):
names.append(k)
except AttributeError:
except Exception:
# Deliberately catch all exceptions for properties that raise
# exceptions (e.g. NotImplementedError). See bug #35108
pass
for key in names:
# First try to get the value via getattr. Some descriptors don't
Expand All @@ -360,6 +362,11 @@ def getmembers(object, predicate=None):
# could be a (currently) missing slot member, or a buggy
# __dir__; discard and move on
continue
except Exception:
# See bug #25108
# Attempt to get the value with getattr_static but skip if
# even that fails.
value = getattr_static(object, key)
if not predicate or predicate(value):
results.append((key, value))
processed.add(key)
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,17 @@ class C(metaclass=M):
attrs = [a[0] for a in inspect.getmembers(C)]
self.assertNotIn('missing', attrs)

def test_getmembers_property_raises_exception(self):
# bpo-35108
class A:
@property
def f(self):
raise NotImplementedError

self.assertIn(("f", A.f), inspect.getmembers(A()))



class TestIsDataDescriptor(unittest.TestCase):

def test_custom_descriptors(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Handle proprties raising exceptions in :meth:`inspect.getmembers`.