[3.11] gh-112387: Fix error positions for decoded strings with backwards tokenize errors (GH-112409) (#112469)

gh-112387: Fix error positions for decoded strings with backwards tokenize errors (GH-112409)
(cherry picked from commit 45d648597b)

Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
This commit is contained in:
Miss Islington (bot) 2023-11-27 20:05:20 +01:00 committed by GitHub
parent 43b081bfc4
commit 390a5b81a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 0 deletions

View file

@ -2143,6 +2143,10 @@ func(
"""
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")

View file

@ -0,0 +1,2 @@
Fix error positions for decoded strings with backwards tokenize errors.
Patch by Pablo Galindo

View file

@ -270,6 +270,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