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
inspect.getmembers passes exceptions from object's properties through #79289
Comments
I use inspect.getmembers for getting members of splinter.webdriver.BaseWebDriver. The problem is that it has a property status_code raises NotImplementedError: https://github.com/cobrateam/splinter/blob/master/splinter/driver/webdriver/__init__.py#L191. This exception passes through try block in https://github.com/python/cpython/blob/master/Lib/inspect.py#L343 because it doesn't contain: I think there are two possible expected behaviors:
|
Thanks for the report. A simple reproducible in Python without splinter code. I think it happens for instances of the class where the attribute is a property raising exception and executing inspect.getmembers(Foo) works fine. # bpo-35108.py import inspect
class Foo():
@property
def bar(self):
raise NotImplementedError
print(inspect.getmembers(Foo())) $. /python.exe ../backups/bpo35108.py
Traceback (most recent call last):
File "../backups/bpo35108.py", line 9, in <module>
print(inspect.getmembers(Foo()))
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/inspect.py", line 344, in getmembers
value = getattr(object, key)
File "../backups/bpo35108.py", line 7, in bar
raise NotImplementedError
NotImplementedError |
Sorry I misread your title stating it's about object getattr for a property has to execute the underlying code it seems as explained in https://stackoverflow.com/a/30143990/2610955 . I don't know if there is a way through which an attribute access in case of property can be verified without executing it. In case of catching the exception and returning the attributes that don't raise exception since it depends on the runtime execution of the property where the exception might occur at one point in time and doesn't occur in another point of time causing confusion during debugging. |
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 python/cpython#46118), 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 bpo-30533 a proposal for implementation of getmembers that uses getattr_static instead of getattr. Thanks |
This "bug" is buzzing around my project head right now, interfering with the operation of the Python Fire CLI library when it attempts to interrogate the Python Pandas DataFrame using the inspect.getmembers() call. See pandas-dev/pandas#31474 and pandas-dev/pandas#31549. I have code that uses Fire and Pandas, but have to "dumb it down" to use Pandas 0.24.3 rather than the latest 0.25.1 which raises a "NotImplementedError" which leaks out of the getmembers() call. The Pandas people are passing the buck to you folks in the Python community. This is terribly frustrating for we minions in the real work trying to implementing real working software systems leveraging all these wonderful libraries (and the Python language). When is this "bug" going to be fixed? Help! |
@tirkarthi Perhaps NotImplementedError can be handled as a special case? It indicates that the property is not really there, so it should be ok to omit it from the output. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: