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.getsource raises wrong error on classes in interactive session #88814
Comments
[ins] In [63]: class A:pass [ins] In [64]: import inspect [ins] In [65]: inspect.getsource(A) [snip] /usr/local/Cellar/python@3.9/3.9.1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/inspect.py in getfile(object) The error is 'X is a built-in class', instead it should be OSError, source code is not available. |
Why do you think OSError fits here? Since objects provided on the command line by definition cannot have source code files, the problem isn't that the file is missing or inaccessible. Rather, the value provided to getsource() is wrong. So, in my view, this should rather be ValueError. Moreover, checking for __main__ is insufficient as there are __main__.py modules after all in many packages. |
Łukasz: Thanks for looking at this!
Line 930 in 7915c96
I think it's an implementation detail that class source is not available on interactive prompt, for example function source is available via If a user can do getsource(func) and getsource(cls) in a file, and getsource(func) on interactive prompt, it seems wrong to return a ValueError when the user is trying to getsource(cls) in interactive. I'm open to change it to ValueError though because I think the err message here is more important to avoid user confusion.
I tested it to make sure: ./python.exe ~/temp/main.py |
I'm not sure though how is the unit test succeeding since the test module should have __file__ set. Looking into it.. |
This is kind of interesting:
Thanks for helping to catch this :-) |
Since this is an exception type change, I'd feel more comfortable leaving 3.9 out of the backports here. That way it will be easier for application authors to differentiate between when the change was introduced or not. |
ince this is an exception type change, I'd feel more comfortable leaving 3.9 out of the backports here. That way it will be easier for application authors to differentiate between when the change was introduced or not. |
Thanks, Andrei! ✨ 🍰 ✨ |
Unfortunately, this PR has broken test_inspect when executed with -R, see: |
I am not sure that OSError was a correct exception. Before this, the only exception raised by Even if this change is correct, it should be documented, have |
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: