]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-115823: Calculate correctly error locations when dealing with implicit...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 26 Feb 2024 13:18:06 +0000 (14:18 +0100)
committerGitHub <noreply@github.com>
Mon, 26 Feb 2024 13:18:06 +0000 (13:18 +0000)
gh-115823: Calculate correctly error locations when dealing with implicit encodings (GH-115824)
(cherry picked from commit 015b97d19a24a169cc3c0939119e1228791e4253)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Lib/test/test_exceptions.py
Misc/NEWS.d/next/Core and Builtins/2024-02-22-16-17-53.gh-issue-115823.c1TreJ.rst [new file with mode: 0644]
Parser/pegen_errors.c

index 2d6c3336744b7e7c5d6b22a4d60c96736bb2a197..6c09c1793c8d7f681537edfe9156bfd409db36db 100644 (file)
@@ -301,6 +301,7 @@ class ExceptionTests(unittest.TestCase):
             {
             6
             0="""''', 5, 13)
+        check('b"fooжжж"'.encode(), 1, 1, 1, 10)
 
         # Errors thrown by symtable.c
         check('x = [(yield i) for i in range(3)]', 1, 7)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-02-22-16-17-53.gh-issue-115823.c1TreJ.rst b/Misc/NEWS.d/next/Core and Builtins/2024-02-22-16-17-53.gh-issue-115823.c1TreJ.rst
new file mode 100644 (file)
index 0000000..8cda4c9
--- /dev/null
@@ -0,0 +1,3 @@
+Properly calculate error ranges in the parser when raising
+:exc:`SyntaxError` exceptions caused by invalid byte sequences. Patch by
+Pablo Galindo
index cefec5d27571bfc892624cb6c7130005c2d71d1f..72f1349897683dcf259b672d4e55acc9626d608f 100644 (file)
@@ -367,20 +367,18 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
     Py_ssize_t col_number = col_offset;
     Py_ssize_t end_col_number = end_col_offset;
 
-    if (p->tok->encoding != NULL) {
-        col_number = _PyPegen_byte_offset_to_character_offset(error_line, col_offset);
-        if (col_number < 0) {
+    col_number = _PyPegen_byte_offset_to_character_offset(error_line, col_offset);
+    if (col_number < 0) {
+        goto error;
+    }
+
+    if (end_col_offset > 0) {
+        end_col_number = _PyPegen_byte_offset_to_character_offset(error_line, end_col_offset);
+        if (end_col_number < 0) {
             goto error;
         }
-        if (end_col_number > 0) {
-            Py_ssize_t end_col_offset = _PyPegen_byte_offset_to_character_offset(error_line, end_col_number);
-            if (end_col_offset < 0) {
-                goto error;
-            } else {
-                end_col_number = end_col_offset;
-            }
-        }
     }
+
     tmp = Py_BuildValue("(OnnNnn)", p->tok->filename, lineno, col_number, error_line, end_lineno, end_col_number);
     if (!tmp) {
         goto error;