Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96eeff5

Browse files
authored
bpo-43555: Report the column offset for invalid line continuation character (pythonGH-24939)
1 parent 123ff26 commit 96eeff5

4 files changed

Lines changed: 13 additions & 6 deletions

File tree

Lib/test/test_syntax.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,11 @@ def func2():
11621162
"""
11631163
self._check_error(code, "expected ':'")
11641164

1165+
def test_invalid_line_continuation_error_position(self):
1166+
self._check_error(r"a = 3 \ 4",
1167+
"unexpected character after line continuation character",
1168+
lineno=1, offset=9)
1169+
11651170
def test_invalid_line_continuation_left_recursive(self):
11661171
# Check bpo-42218: SyntaxErrors following left-recursive rules
11671172
# (t_primary_raw in this case) need to be tested explicitly
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Report the column offset for :exc:`SyntaxError` for invalid line
2+
continuation characters. Patch by Pablo Galindo.

Parser/pegen.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ tokenizer_error(Parser *p)
328328

329329
const char *msg = NULL;
330330
PyObject* errtype = PyExc_SyntaxError;
331+
Py_ssize_t col_offset = -1;
331332
switch (p->tok->done) {
332333
case E_TOKEN:
333334
msg = "invalid token";
@@ -359,16 +360,14 @@ tokenizer_error(Parser *p)
359360
msg = "too many levels of indentation";
360361
break;
361362
case E_LINECONT:
363+
col_offset = strlen(strtok(p->tok->buf, "\n")) - 1;
362364
msg = "unexpected character after line continuation character";
363365
break;
364366
default:
365367
msg = "unknown parsing error";
366368
}
367369

368-
PyErr_Format(errtype, msg);
369-
// There is no reliable column information for this error
370-
PyErr_SyntaxLocationObject(p->tok->filename, p->tok->lineno, 0);
371-
370+
RAISE_ERROR_KNOWN_LOCATION(p, errtype, p->tok->lineno, col_offset, msg);
372371
return -1;
373372
}
374373

Parser/pegen.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,9 @@ void *_PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
136136
void *_PyPegen_dummy_name(Parser *p, ...);
137137

138138
Py_LOCAL_INLINE(void *)
139-
RAISE_ERROR_KNOWN_LOCATION(Parser *p, PyObject *errtype, int lineno,
140-
int col_offset, const char *errmsg, ...)
139+
RAISE_ERROR_KNOWN_LOCATION(Parser *p, PyObject *errtype,
140+
Py_ssize_t lineno, Py_ssize_t col_offset,
141+
const char *errmsg, ...)
141142
{
142143
va_list va;
143144
va_start(va, errmsg);

0 commit comments

Comments
 (0)