You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
3.12: tokenize adds a newline when it is not there #105259
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)
@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 \n for NEWLINE tokens which we should not do for the last one).
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):
Activity
pablogsal commentedon Jun 3, 2023
@lysnikolaou can you take a look at this?
I think this is due to this line:
cpython/Parser/tokenizer.c
Line 1309 in e6373c0
in combination with this line:
cpython/Python/Python-tokenize.c
Line 249 in e6373c0
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.~pablogsal commentedon 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).lysnikolaou commentedon Jun 5, 2023
Do we? I thought you had removed this in #104850, but I guess I misunderstood that PR. I'll have a look.
pablogsal commentedon Jun 5, 2023
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):
cpython/Parser/tokenizer.c
Line 1309 in e6373c0
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.
pablogsal commentedon Jun 6, 2023
@Yhg1s can you wait until we fix this for the release?
pythongh-105259: Ensure we don't show newline characters for trailing…
Yhg1s commentedon Jun 6, 2023
Yeah, I can wait.
pythongh-105259: Ensure we don't show newline characters for trailing…
gh-105259: Ensure we don't show newline characters for trailing NEWLI…
pythongh-105259: Ensure we don't show newline characters for trailing…
[3.12] gh-105259: Ensure we don't show newline characters for trailin…
pablogsal commentedon Jun 6, 2023
@asottile can you confirm this fixes it?
asottile commentedon Jun 6, 2023
yep looks fixed -- this is the only outstanding breaking change I'm encountering: #105390