Skip to content

Commit e6fae7e

Browse files
committed
bpo-43555: Report the column offset for invalid line continuation character
1 parent 148bc05 commit e6fae7e

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

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
@@ -324,6 +324,7 @@ tokenizer_error(Parser *p)
324324

325325
const char *msg = NULL;
326326
PyObject* errtype = PyExc_SyntaxError;
327+
Py_ssize_t col_offset = -1;
327328
switch (p->tok->done) {
328329
case E_TOKEN:
329330
msg = "invalid token";
@@ -355,16 +356,14 @@ tokenizer_error(Parser *p)
355356
msg = "too many levels of indentation";
356357
break;
357358
case E_LINECONT:
359+
col_offset = strlen(strtok(p->tok->buf, "\n")) - 1;
358360
msg = "unexpected character after line continuation character";
359361
break;
360362
default:
361363
msg = "unknown parsing error";
362364
}
363365

364-
PyErr_Format(errtype, msg);
365-
// There is no reliable column information for this error
366-
PyErr_SyntaxLocationObject(p->tok->filename, p->tok->lineno, 0);
367-
366+
RAISE_ERROR_KNOWN_LOCATION(p, errtype, p->tok->lineno, col_offset, msg);
368367
return -1;
369368
}
370369

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)