]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-105017: Fix including additional NL token when using CRLF (#105022)
authorMarta Gómez Macías <mgmacias@google.com>
Sat, 27 May 2023 16:50:43 +0000 (17:50 +0100)
committerGitHub <noreply@github.com>
Sat, 27 May 2023 16:50:43 +0000 (16:50 +0000)
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Lib/test/test_tokenize.py
Misc/NEWS.d/next/Core and Builtins/2023-05-27-16-23-16.gh-issue-105017.KQrsC0.rst [new file with mode: 0644]
Parser/tokenizer.c

index abb68859be944ca9389e36108a632e3828866bc4..293592b3fd13db49e51026d1df501dc6d0cc1ce4 100644 (file)
@@ -84,6 +84,14 @@ class TokenizeTest(TestCase):
     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
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-05-27-16-23-16.gh-issue-105017.KQrsC0.rst b/Misc/NEWS.d/next/Core and Builtins/2023-05-27-16-23-16.gh-issue-105017.KQrsC0.rst
new file mode 100644 (file)
index 0000000..d41a216
--- /dev/null
@@ -0,0 +1 @@
+Do not include an additional final ``NL`` token when parsing files having CRLF lines. Patch by Marta Gómez.
index a7651b1bd864308868795b8913cee9d3b3594af7..a84c2492b6b17a085b4558afcdda54fcb9bc5fbb 100644 (file)
@@ -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++;
     }