Skip to content

Commit 7140716

Browse files
[3.12] gh-112387: Fix error positions for decoded strings with backwards tokenize errors (GH-112409) (#112468)
gh-112387: Fix error positions for decoded strings with backwards tokenize errors (GH-112409) (cherry picked from commit 45d6485) Signed-off-by: Pablo Galindo <[email protected]> Co-authored-by: Pablo Galindo Salgado <[email protected]>
1 parent 4463d2e commit 7140716

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
@@ -2296,6 +2296,10 @@ def test_error_parenthesis(self):
22962296
"""
22972297
self._check_error(code, "parenthesis '\\)' does not match opening parenthesis '\\['")
22982298

2299+
# Examples with dencodings
2300+
s = b'# coding=latin\n(aaaaaaaaaaaaaaaaa\naaaaaaaaaaa\xb5'
2301+
self._check_error(s, "'\(' was never closed")
2302+
22992303
def test_error_string_literal(self):
23002304

23012305
self._check_error("'blech", "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
@@ -276,6 +276,10 @@ get_error_line_from_tokenizer_buffers(Parser *p, Py_ssize_t lineno)
276276
Py_ssize_t relative_lineno = p->starting_lineno ? lineno - p->starting_lineno + 1 : lineno;
277277
const char* buf_end = p->tok->fp_interactive ? p->tok->interactive_src_end : p->tok->inp;
278278

279+
if (buf_end < cur_line) {
280+
buf_end = cur_line + strlen(cur_line);
281+
}
282+
279283
for (int i = 0; i < relative_lineno - 1; i++) {
280284
char *new_line = strchr(cur_line, '\n');
281285
// The assert is here for debug builds but the conditional that

0 commit comments

Comments
 (0)