Skip to content

Commit

Permalink
pythongh-112387: Fix error positions for decoded strings with backwar…
Browse files Browse the repository at this point in the history
…ds tokenize errors (pythonGH-112409)

(cherry picked from commit 45d6485)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
  • Loading branch information
pablogsal authored and miss-islington committed Nov 27, 2023
1 parent 46047bb commit d961cd3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Lib/test/test_syntax.py
Expand Up @@ -2296,6 +2296,10 @@ def test_error_parenthesis(self):
"""
self._check_error(code, "parenthesis '\\)' does not match opening parenthesis '\\['")

# Examples with dencodings
s = b'# coding=latin\n(aaaaaaaaaaaaaaaaa\naaaaaaaaaaa\xb5'
self._check_error(s, "'\(' was never closed")

def test_error_string_literal(self):

self._check_error("'blech", "unterminated string literal")
Expand Down
@@ -0,0 +1,2 @@
Fix error positions for decoded strings with backwards tokenize errors.
Patch by Pablo Galindo
4 changes: 4 additions & 0 deletions Parser/pegen_errors.c
Expand Up @@ -276,6 +276,10 @@ get_error_line_from_tokenizer_buffers(Parser *p, Py_ssize_t lineno)
Py_ssize_t relative_lineno = p->starting_lineno ? lineno - p->starting_lineno + 1 : lineno;
const char* buf_end = p->tok->fp_interactive ? p->tok->interactive_src_end : p->tok->inp;

if (buf_end < cur_line) {
buf_end = cur_line + strlen(cur_line);
}

for (int i = 0; i < relative_lineno - 1; i++) {
char *new_line = strchr(cur_line, '\n');
// The assert is here for debug builds but the conditional that
Expand Down

0 comments on commit d961cd3

Please sign in to comment.