Repository navigation
3.12: tokenize adds a newline when it is not there #105259
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jun 2, 2023 @lysnikolaou can you take a look at this?
I think this is due to this line:
Line 1309 in e6373c0
/* Last line does not end in \n, fake one */
in combination with this line:cpython/Python/Python-tokenize.c
Line 249 in e6373c0
str = PyUnicode_FromString("\n"); But the real question is what is going to break if we cover the first line with
if (!tok->extra_tokens)and how we can fix it.~- added3.12only security fixesonly security fixes3.13only security fixesonly security fixes
on Jun 3, 2023 @lysnikolaou The problem is that we are always adding a new line at the end of the line buffer so we cannot distinguish if the last one is there for real or not (also, we always hardcode
\nforNEWLINEtokens which we should not do for the last one).The problem is that we are always adding a new line at the end of the line buffer so we cannot distinguish if the last one is there for real or not
Do we? I thought you had removed this in #104850, but I guess I misunderstood that PR. I'll have a look.
Do we? I thought you had removed this in #104850, but I guess I misunderstood that PR. I'll have a look.
That was "reverted" because apparently that was required to keep backward compatibility IIRC. But this is also a different kind of "new line": the tokenize module needs new lines at the end of every logical line to correctly emit the last tokens. Just comment out this part and see what fails (locations of last tokens are wrong as well as the some implicit dedents are missing):
Line 1309 in e6373c0
/* Last line does not end in \n, fake one */ Notice that just removing that is not enough to fix this: we would also need to emit the last artificial newline if the input doesn't contain it.
@Yhg1s can you wait until we fix this for the release?
Yeah, I can wait.
- added a commit that references this issue
on Jun 6, 2023 @asottile can you confirm this fixes it?
- moved this from Todo to Done in Release and Deferred blockers 🚫
on Jun 6, 2023 yep looks fixed -- this is the only outstanding breaking change I'm encountering: #105390
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Bug report
tokenize adds a concrete newline character when it is not present. this breaks any sort of roundtrip source generation and pycodestyle's end-of-file checker
here's an example of a file containing a single byte (generated via
echo -n 1 > t.py)Your environment
cc @pablogsal
Linked PRs