]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-112387: Fix error positions for decoded strings with backwards tokenize...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 27 Nov 2023 18:57:34 +0000 (19:57 +0100)
committerGitHub <noreply@github.com>
Mon, 27 Nov 2023 18:57:34 +0000 (18:57 +0000)
gh-112387: Fix error positions for decoded strings with backwards tokenize errors (GH-112409)
(cherry picked from commit 45d648597b1146431bf3d91041e60d7f040e70bf)

Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Lib/test/test_syntax.py
Misc/NEWS.d/next/Core and Builtins/2023-11-25-22-39-44.gh-issue-112387.AbBq5W.rst [new file with mode: 0644]
Parser/pegen_errors.c

index 00c5f624ceb3ab6e43aa4f83478e7e037c8002c0..5183aaa506f8728d289acefeb64486646684c370 100644 (file)
@@ -2296,6 +2296,10 @@ func(
 """
         self._check_error(code, "parenthesis '\\)' does not match opening parenthesis '\\['")
 
+        # Examples with dencodings
+        s = b'# coding=latin\n(aaaaaaaaaaaaaaaaa\naaaaaaaaaaa\xb5'
+        self._check_error(s, "'\(' was never closed")
+
     def test_error_string_literal(self):
 
         self._check_error("'blech", "unterminated string literal")
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-11-25-22-39-44.gh-issue-112387.AbBq5W.rst b/Misc/NEWS.d/next/Core and Builtins/2023-11-25-22-39-44.gh-issue-112387.AbBq5W.rst
new file mode 100644 (file)
index 0000000..adac11b
--- /dev/null
@@ -0,0 +1,2 @@
+Fix error positions for decoded strings with backwards tokenize errors.
+Patch by Pablo Galindo
index 6390a66719259a794709a1875ddd9c7d0a13299b..e832422ab1c8fe9d2bbebd5c51380a1ebd806c59 100644 (file)
@@ -276,6 +276,10 @@ get_error_line_from_tokenizer_buffers(Parser *p, Py_ssize_t lineno)
     Py_ssize_t relative_lineno = p->starting_lineno ? lineno - p->starting_lineno + 1 : lineno;
     const char* buf_end = p->tok->fp_interactive ? p->tok->interactive_src_end : p->tok->inp;
 
+    if (buf_end < cur_line) {
+        buf_end = cur_line + strlen(cur_line);
+    }
+
     for (int i = 0; i < relative_lineno - 1; i++) {
         char *new_line = strchr(cur_line, '\n');
         // The assert is here for debug builds but the conditional that