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
bpo-27578: Fix inspect.getsource() on empty file #20809
base: main
Are you sure you want to change the base?
Conversation
For modules from empty files, `inspect.getsource()` now returns an empty string, and `inspect.getsourcelines()` returns a list of one empty string, fixing the expected invariant. As indicated by `exec('')`, empty strings are valid Python source code.
Thanks for looking into this. I think it would make more sense to change https://github.com/python/cpython/blob/3.9/Lib/linecache.py#L140-L141 to:
if not lines:
lines = ['\n']
elif not lines[-1].endswith('\n'):
lines[-1] += '\n'
and then inspect
would not need to be changed.
Thanks @remilapeyre! Indeed, using the whole existing pipeline makes plenty more sense. With trailing Is the discrepancy between |
The change in the pdb test is related to this issue, it had nowhere to put the current line marker, but I think having the current line marker on an empty line when the file is empty is not an issue.
It is useful to have a sentinel value that is a string as you don't have to worry to handle For the change to be accepted, you will have to add a test for the change in |
Thanks. Test for cpython/Lib/test/test_linecache.py Lines 84 to 86 in 95ebd0e
|
Thanks for making the change, I just have a last remark for the tests.
I didn't expect the Spanish Inquisition |
Nobody expects the Spanish Inquisition! : please review the changes made to this pull request. |
For modules from empty files,
inspect.getsource()
instead of raising inaccurateOSError
now returns an empty string, andinspect.getsourcelines()
returns a list of one empty string, fixing the expected invariant.As indicated by
exec('')
, empty strings are valid Python source code.https://bugs.python.org/issue27578
The text was updated successfully, but these errors were encountered: