-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-71765: Fix inspect.getsource() on empty file #20809
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* bpo-27578: Fix inspect.getsource() on empty file 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. Co-authored-by: Oleg Iarygin <[email protected]>
* bpo-27578: Fix inspect.getsource() on empty file 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. Co-authored-by: Oleg Iarygin <[email protected]>
* bpo-27578: Fix inspect.getsource() on empty file 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. Co-authored-by: Oleg Iarygin <[email protected]>
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