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
tokenize.py emits spurious NEWLINE if file ends on a comment without a newline #88833
Comments
Using tokenize.py to tokenize the attached file yields: This output is wrong in that it adds 2 newlines one as a NL which is a correct and one as a NEWLINE which is not since there is no preceding code. If a new line is added at the end of the file, one gets: 0,0-0,0: ENCODING 'utf-8' Similarly if code is added before the comment, a single NEWLINE is generated (with no text since it is fake). The extra NEWLINE found when tokenizing the attached file can cause issue when parsing the file. It was found in we-like-parsers/pegen#11 (comment) where a pure python parser based on pegen is being built. The extra NEWLINE is an issue since the grammar does not accept NEWLINE at the end of a block and cause parsing to fail using the same rules as the python grammar while the cpython parser can handle this file without any issue. I believe this issue stems from https://github.com/python/cpython/blob/3.9/Lib/tokenize.py#L605 where the check does not account for a last line limited to comments. Adding a check to determine if the line starts with a # should be sufficient to avoid emitting the extra NEWLINE. |
This appears to have been a duplicate of bpo-35107, where the failing example was '#' and it was NL, NEWLINE pair was noted. So this either predates 3.9 or was re-introduced. In any case, thanks for the fix. |
- Fix test break, since `tokenize.tokenize` has buggy behavior that wasn't backported. - See python/cpython#79288 and python/cpython#88833. - Adjust typing using so everything from typing is prepended with `typing.`.
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: