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
Tokenizer module does not handle backslash characters correctly #90432
A source of one or more backslash-escaped newlines, and one final newline, is not tokenized the same as a source where those lines are "manually joined".
The source
\
\
\
produces the tokens NEWLINE, ENDMARKER when piped to the tokenize module.
Whereas the source
produces the tokens NL, ENDMARKER.
What I expect is to receive only one NL token from both sources. As per the documentation "Two or more physical lines may be joined into logical lines using backslash characters" ... "A logical line that contains only spaces, tabs, formfeeds and possibly a comment, is ignored (i.e., no NEWLINE token is generated)"
And, because these logical lines are not being ignored, if there are spaces/tabs, INDENT and DEDENT tokens are also being unexpectedly produced.
The source
\
produces the tokens INDENT, NEWLINE, DEDENT, ENDMARKER.
changed the title [-]backslash creating statement out of nothing[/-][+]Tokenizer module does not handle backslash characters correctly[/+]on Jan 6, 2022
changed the title [-]backslash creating statement out of nothing[/-][+]Tokenizer module does not handle backslash characters correctly[/+]on Jan 6, 2022
I think that the 3.12 version is the correct one regarding the NEWLINE/NL tokens. Regarding the location of the DEDENT token, I don't have a strong opinion, but feel that it could stay this way. @pablogsal@mgmacias95 What do you think?
Aditionally, there may be some minor behavioral changes as a consecuence of the changes required to support PEP 701. Some of these changes include:
Some final DEDENT tokens are now emitted within the bounds of the input. This means that for a file containing 3 lines, the old version of the tokenizer returned a DEDENT token in line 4 whilst the new version returns the token in line 3.
The type attribute of the tokens emitted when tokenizing some invalid Python characters such as ! has changed from ERRORTOKEN to OP.
Activity
ucodery commentedon Jan 5, 2022
A source of one or more backslash-escaped newlines, and one final newline, is not tokenized the same as a source where those lines are "manually joined".
The source
produces the tokens NEWLINE, ENDMARKER when piped to the tokenize module.
Whereas the source
produces the tokens NL, ENDMARKER.
What I expect is to receive only one NL token from both sources. As per the documentation "Two or more physical lines may be joined into logical lines using backslash characters" ... "A logical line that contains only spaces, tabs, formfeeds and possibly a comment, is ignored (i.e., no NEWLINE token is generated)"
And, because these logical lines are not being ignored, if there are spaces/tabs, INDENT and DEDENT tokens are also being unexpectedly produced.
The source
produces the tokens INDENT, NEWLINE, DEDENT, ENDMARKER.
Whereas the source (with spaces)
produces the tokens NL, ENDMARKER.
[-]backslash creating statement out of nothing[/-][+]Tokenizer module does not handle backslash characters correctly[/+][-]backslash creating statement out of nothing[/-][+]Tokenizer module does not handle backslash characters correctly[/+]7 remaining items
lysnikolaou commentedon May 24, 2023
This appears to have been fixed in #104323. @asottile Can you check whether that solves the issue for you as well?
asottile commentedon May 24, 2023
@lysnikolaou it seems to fix that -- but also includes a regression(?) (this is 3.12.0b1):
lysnikolaou commentedon May 24, 2023
I think that the 3.12 version is the correct one regarding the
NEWLINE/NLtokens. Regarding the location of theDEDENTtoken, I don't have a strong opinion, but feel that it could stay this way. @pablogsal @mgmacias95 What do you think?pablogsal commentedon May 24, 2023
This is now covered in the 3.12 what's new:
Aditionally, there may be some minor behavioral changes as a consecuence of the changes required to support PEP 701. Some of these changes include:
Some final DEDENT tokens are now emitted within the bounds of the input. This means that for a file containing 3 lines, the old version of the tokenizer returned a DEDENT token in line 4 whilst the new version returns the token in line 3.
The type attribute of the tokens emitted when tokenizing some invalid Python characters such as ! has changed from ERRORTOKEN to OP.
pablogsal commentedon May 24, 2023
I think we can close this issue
lysnikolaou commentedon May 24, 2023
Thanks @pablogsal!
pablogsal commentedon May 25, 2023
Singe the latest fix, the only different now is the DEDENT:
asottile commentedon May 25, 2023
@pablogsal then this should be reopened
pablogsal commentedon May 25, 2023
Why? That is an expected difference as I mentioned in my previous comment.
asottile commentedon May 25, 2023
the NEWLINE token should be a NL if this issue is resolved
pablogsal commentedon May 25, 2023
No, that was a bug, see #104866 and #104870
asottile commentedon May 25, 2023
I'm confused -- I think we're talking about different things -- are you using the source here?
#90432 (comment)
pablogsal commentedon May 25, 2023
I am. The difference I was mentioning was between Python 3.11 and 3.12. Just to be clear, given
with 3.12:
With 3.11:
The difference between 3.11 and 3.12 for the "correct" version is still the location of the last DEDENT token, which is expected.
asottile commentedon May 25, 2023
hmmm ok but this diff doesn't show the NEWLINE -> NL: #90432 (comment)