Skip to content

Commit

Permalink
pythongh-105017: Fix including additional NL token when using CRLF (p…
Browse files Browse the repository at this point in the history
…ythonGH-105022)

(cherry picked from commit 86d8f48)

Co-authored-by: Marta Gómez Macías <mgmacias@google.com>
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
  • Loading branch information
2 people authored and miss-islington committed May 27, 2023
1 parent edd0cb8 commit 1c7e37d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Lib/test/test_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ def test_basic(self):
NEWLINE '\\n' (4, 26) (4, 27)
DEDENT '' (5, 0) (5, 0)
""")

self.check_tokenize("foo='bar'\r\n", """\
NAME 'foo' (1, 0) (1, 3)
OP '=' (1, 3) (1, 4)
STRING "'bar'" (1, 4) (1, 9)
NEWLINE '\\n' (1, 9) (1, 10)
""")

indent_error_file = b"""\
def k(x):
x += 2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not include an additional final ``NL`` token when parsing files having CRLF lines. Patch by Marta Gómez.
2 changes: 1 addition & 1 deletion Parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ translate_newlines(const char *s, int exec_input, struct tok_state *tok) {
}
/* If this is exec input, add a newline to the end of the string if
there isn't one already. */
if (exec_input && c != '\n') {
if (exec_input && c != '\n' && c != '\0') {
*current = '\n';
current++;
}
Expand Down

0 comments on commit 1c7e37d

Please sign in to comment.