[issue35108] inspect.getmembers passes exceptions from object's properties through
Karthikeyan Singaravelan
report at bugs.python.org
Sun Nov 4 15:22:21 EST 2018
Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:
There are some relevant docs in the inspect module about properties triggering code execution and thus using getattr_static to fetch attributes. Ref : https://docs.python.org/3/library/inspect.html#fetching-attributes-statically . I tried the below and it passes for your code but there might be still cases where getattr_static triggers an exception too where we need to decide whether to skip the attribute from being listed and if so with a test for the scenario.
Using getattr_static first causes test_inspect.TestPredicates.test_get_slot_members to fail by including slot_member. Thus we try for getattr and if it raises an exception that is not AttributeError (@property might also raise AttributeError consciously at runtime) and then use getattr_static in case of exception other than AttributeError. This helps in passing the test suite and also the example attached listing bar which raises NotImplementedError or other Exceptions.
diff --git a/Lib/inspect.py b/Lib/inspect.py
index b8a142232b..9df2173e0c 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -341,7 +341,12 @@ def getmembers(object, predicate=None):
# like calling their __get__ (see bug #1785), so fall back to
# looking in the __dict__.
try:
- value = getattr(object, key)
+ try:
+ value = getattr(object, key)
+ except Exception as e:
+ if isinstance(e, AttributeError):
+ raise e
+ value = getattr_static(object, key)
# handle the duplicate key
if key in processed:
raise AttributeError
I am adding Yury. Removing 3.5 and 3.4 since they are in security fixes only mode.
Also see issue30533 a proposal for implementation of getmembers that uses getattr_static instead of getattr.
Thanks
----------
nosy: +yselivanov
versions: -Python 3.4, Python 3.5
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35108>
_______________________________________
More information about the Python-bugs-list
mailing list