Skip to content

Commit 45d6485

Browse files
authored
gh-112387: Fix error positions for decoded strings with backwards tokenize errors (#112409)
Signed-off-by: Pablo Galindo <[email protected]>
1 parent 2c8b191 commit 45d6485

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

Lib/test/test_syntax.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,6 +2334,10 @@ def test_error_parenthesis(self):
23342334
"""
23352335
self._check_error(code, "parenthesis '\\)' does not match opening parenthesis '\\['")
23362336

2337+
# Examples with dencodings
2338+
s = b'# coding=latin\n(aaaaaaaaaaaaaaaaa\naaaaaaaaaaa\xb5'
2339+
self._check_error(s, "'\(' was never closed")
2340+
23372341
def test_error_string_literal(self):
23382342

23392343
self._check_error("'blech", r"unterminated string literal \(.*\)$")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix error positions for decoded strings with backwards tokenize errors.
2+
Patch by Pablo Galindo

Parser/pegen_errors.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ get_error_line_from_tokenizer_buffers(Parser *p, Py_ssize_t lineno)
282282
Py_ssize_t relative_lineno = p->starting_lineno ? lineno - p->starting_lineno + 1 : lineno;
283283
const char* buf_end = p->tok->fp_interactive ? p->tok->interactive_src_end : p->tok->inp;
284284

285+
if (buf_end < cur_line) {
286+
buf_end = cur_line + strlen(cur_line);
287+
}
288+
285289
for (int i = 0; i < relative_lineno - 1; i++) {
286290
char *new_line = strchr(cur_line, '\n');
287291
// The assert is here for debug builds but the conditional that

0 commit comments

Comments
 (0)