bpo-45738: Fix computation of error location for invalid continuation characters in the parser (GH-29550) (GH-29552)

(cherry picked from commit 25835c518a)
This commit is contained in:
Pablo Galindo Salgado 2021-11-14 01:47:27 +00:00 committed by GitHub
parent 3e0b830e85
commit 142fcb40b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 11 deletions

View file

@ -953,7 +953,13 @@ def func2():
def test_invalid_line_continuation_error_position(self): def test_invalid_line_continuation_error_position(self):
self._check_error(r"a = 3 \ 4", self._check_error(r"a = 3 \ 4",
"unexpected character after line continuation character", "unexpected character after line continuation character",
lineno=1, offset=(10 if support.use_old_parser() else 9)) lineno=1, offset=8)
self._check_error('1,\\#\n2',
"unexpected character after line continuation character",
lineno=1, offset=4)
self._check_error('\nfgdfgf\n1,\\#\n2\n',
"unexpected character after line continuation character",
lineno=3, offset=4)
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

View file

@ -0,0 +1,2 @@
Fix computation of error location for invalid continuation characters in the
parser. Patch by Pablo Galindo.

View file

@ -348,14 +348,7 @@ tokenizer_error(Parser *p)
msg = "too many levels of indentation"; msg = "too many levels of indentation";
break; break;
case E_LINECONT: { case E_LINECONT: {
char* loc = strrchr(p->tok->buf, '\n'); col_offset = p->tok->cur - p->tok->buf - 1;
const char* last_char = p->tok->cur - 1;
if (loc != NULL && loc != last_char) {
col_offset = p->tok->cur - loc - 1;
p->tok->buf = loc;
} else {
col_offset = last_char - p->tok->buf - 1;
}
msg = "unexpected character after line continuation character"; msg = "unexpected character after line continuation character";
break; break;
} }
@ -363,7 +356,8 @@ tokenizer_error(Parser *p)
msg = "unknown parsing error"; msg = "unknown parsing error";
} }
RAISE_ERROR_KNOWN_LOCATION(p, errtype, p->tok->lineno, col_offset, msg); RAISE_ERROR_KNOWN_LOCATION(p, errtype, p->tok->lineno,
col_offset >= 0 ? col_offset : 0, msg);
return -1; return -1;
} }

View file

@ -1752,7 +1752,6 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
c = tok_nextc(tok); c = tok_nextc(tok);
if (c != '\n') { if (c != '\n') {
tok->done = E_LINECONT; tok->done = E_LINECONT;
tok->cur = tok->inp;
return ERRORTOKEN; return ERRORTOKEN;
} }
c = tok_nextc(tok); c = tok_nextc(tok);