mirror of
https://github.com/python/cpython
synced 2024-10-03 02:32:51 +00:00
bpo-43555: Report the column offset for invalid line continuation character (GH-24939)
This commit is contained in:
parent
123ff266cd
commit
96eeff5162
|
@ -1162,6 +1162,11 @@ def func2():
|
||||||
"""
|
"""
|
||||||
self._check_error(code, "expected ':'")
|
self._check_error(code, "expected ':'")
|
||||||
|
|
||||||
|
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):
|
def test_invalid_line_continuation_left_recursive(self):
|
||||||
# Check bpo-42218: SyntaxErrors following left-recursive rules
|
# Check bpo-42218: SyntaxErrors following left-recursive rules
|
||||||
# (t_primary_raw in this case) need to be tested explicitly
|
# (t_primary_raw in this case) need to be tested explicitly
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Report the column offset for :exc:`SyntaxError` for invalid line
|
||||||
|
continuation characters. Patch by Pablo Galindo.
|
|
@ -328,6 +328,7 @@ tokenizer_error(Parser *p)
|
||||||
|
|
||||||
const char *msg = NULL;
|
const char *msg = NULL;
|
||||||
PyObject* errtype = PyExc_SyntaxError;
|
PyObject* errtype = PyExc_SyntaxError;
|
||||||
|
Py_ssize_t col_offset = -1;
|
||||||
switch (p->tok->done) {
|
switch (p->tok->done) {
|
||||||
case E_TOKEN:
|
case E_TOKEN:
|
||||||
msg = "invalid token";
|
msg = "invalid token";
|
||||||
|
@ -359,16 +360,14 @@ tokenizer_error(Parser *p)
|
||||||
msg = "too many levels of indentation";
|
msg = "too many levels of indentation";
|
||||||
break;
|
break;
|
||||||
case E_LINECONT:
|
case E_LINECONT:
|
||||||
|
col_offset = strlen(strtok(p->tok->buf, "\n")) - 1;
|
||||||
msg = "unexpected character after line continuation character";
|
msg = "unexpected character after line continuation character";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
msg = "unknown parsing error";
|
msg = "unknown parsing error";
|
||||||
}
|
}
|
||||||
|
|
||||||
PyErr_Format(errtype, msg);
|
RAISE_ERROR_KNOWN_LOCATION(p, errtype, p->tok->lineno, col_offset, msg);
|
||||||
// There is no reliable column information for this error
|
|
||||||
PyErr_SyntaxLocationObject(p->tok->filename, p->tok->lineno, 0);
|
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -136,8 +136,9 @@ void *_PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
|
||||||
void *_PyPegen_dummy_name(Parser *p, ...);
|
void *_PyPegen_dummy_name(Parser *p, ...);
|
||||||
|
|
||||||
Py_LOCAL_INLINE(void *)
|
Py_LOCAL_INLINE(void *)
|
||||||
RAISE_ERROR_KNOWN_LOCATION(Parser *p, PyObject *errtype, int lineno,
|
RAISE_ERROR_KNOWN_LOCATION(Parser *p, PyObject *errtype,
|
||||||
int col_offset, const char *errmsg, ...)
|
Py_ssize_t lineno, Py_ssize_t col_offset,
|
||||||
|
const char *errmsg, ...)
|
||||||
{
|
{
|
||||||
va_list va;
|
va_list va;
|
||||||
va_start(va, errmsg);
|
va_start(va, errmsg);
|
||||||
|
|
Loading…
Reference in a new issue