Message329254
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 |
|
Date |
User |
Action |
Args |
2018-11-04 20:22:21 | xtreak | set | recipients:
+ xtreak, yselivanov, rominf |
2018-11-04 20:22:21 | xtreak | set | messageid: <1541362941.43.0.788709270274.issue35108@psf.upfronthosting.co.za> |
2018-11-04 20:22:21 | xtreak | link | issue35108 messages |
2018-11-04 20:22:21 | xtreak | create | |
|