]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43555: Report the column offset for invalid line continuation character (GH-24939...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 22 Mar 2021 19:07:05 +0000 (12:07 -0700)
committerGitHub <noreply@github.com>
Mon, 22 Mar 2021 19:07:05 +0000 (19:07 +0000)
(cherry picked from commit 96eeff516204b7cc751103fa33dcc665e387846e)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Lib/test/test_syntax.py
Misc/NEWS.d/next/Core and Builtins/2021-03-19-22-49-40.bpo-43555.ZmhYSA.rst [new file with mode: 0644]
Parser/pegen/pegen.c
Parser/pegen/pegen.h

index f0c9c988e2441787c7247662867e54c1a5ac015d..650a03173a8f3941860b9fcb531790d7bdc0f98d 100644 (file)
@@ -973,6 +973,11 @@ def func2():
 """
         self._check_error(code, "invalid syntax")
 
+    def test_invalid_line_continuation_error_position(self):
+        self._check_error(r"a = 3 \ 4",
+                          "unexpected character after line continuation character",
+                          lineno=1, offset=9)
+
     def test_invalid_line_continuation_left_recursive(self):
         # Check bpo-42218: SyntaxErrors following left-recursive rules
         # (t_primary_raw in this case) need to be tested explicitly
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-03-19-22-49-40.bpo-43555.ZmhYSA.rst b/Misc/NEWS.d/next/Core and Builtins/2021-03-19-22-49-40.bpo-43555.ZmhYSA.rst
new file mode 100644 (file)
index 0000000..55a2fe2
--- /dev/null
@@ -0,0 +1,2 @@
+Report the column offset for :exc:`SyntaxError` for invalid line
+continuation characters. Patch by Pablo Galindo.
index 4e742a5ec717510c80073162094c71df942a5d72..0a26275b23e0d06b63a21146949ca69030bdb98b 100644 (file)
@@ -313,6 +313,7 @@ tokenizer_error(Parser *p)
 
     const char *msg = NULL;
     PyObject* errtype = PyExc_SyntaxError;
+    Py_ssize_t col_offset = -1;
     switch (p->tok->done) {
         case E_TOKEN:
             msg = "invalid token";
@@ -346,16 +347,14 @@ tokenizer_error(Parser *p)
             msg = "too many levels of indentation";
             break;
         case E_LINECONT:
+            col_offset = strlen(strtok(p->tok->buf, "\n")) - 1;
             msg = "unexpected character after line continuation character";
             break;
         default:
             msg = "unknown parsing error";
     }
 
-    PyErr_Format(errtype, msg);
-    // There is no reliable column information for this error
-    PyErr_SyntaxLocationObject(p->tok->filename, p->tok->lineno, 0);
-
+    RAISE_ERROR_KNOWN_LOCATION(p, errtype, p->tok->lineno, col_offset, msg);
     return -1;
 }
 
index a2f524a597d232990f1aa4879c123eff0341a6e8..224c5cbea634910b4edef8df515cd57ceb86eaec 100644 (file)
@@ -138,8 +138,9 @@ void *_PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
 void *_PyPegen_dummy_name(Parser *p, ...);
 
 Py_LOCAL_INLINE(void *)
-RAISE_ERROR_KNOWN_LOCATION(Parser *p, PyObject *errtype, int lineno,
-                           int col_offset, const char *errmsg, ...)
+RAISE_ERROR_KNOWN_LOCATION(Parser *p, PyObject *errtype,
+                           Py_ssize_t lineno, Py_ssize_t col_offset,
+                           const char *errmsg, ...)
 {
     va_list va;
     va_start(va, errmsg);