Skip to content

Commit 142fcb4

Browse files
authored
bpo-45738: Fix computation of error location for invalid continuation characters in the parser (GH-29550) (GH-29552)
(cherry picked from commit 25835c5)
1 parent 3e0b830 commit 142fcb4

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

Lib/test/test_syntax.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,13 @@ def func2():
953953
def test_invalid_line_continuation_error_position(self):
954954
self._check_error(r"a = 3 \ 4",
955955
"unexpected character after line continuation character",
956-
lineno=1, offset=(10 if support.use_old_parser() else 9))
956+
lineno=1, offset=8)
957+
self._check_error('1,\\#\n2',
958+
"unexpected character after line continuation character",
959+
lineno=1, offset=4)
960+
self._check_error('\nfgdfgf\n1,\\#\n2\n',
961+
"unexpected character after line continuation character",
962+
lineno=3, offset=4)
957963

958964
def test_invalid_line_continuation_left_recursive(self):
959965
# Check bpo-42218: SyntaxErrors following left-recursive rules
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix computation of error location for invalid continuation characters in the
2+
parser. Patch by Pablo Galindo.

Parser/pegen/pegen.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -348,22 +348,16 @@ tokenizer_error(Parser *p)
348348
msg = "too many levels of indentation";
349349
break;
350350
case E_LINECONT: {
351-
char* loc = strrchr(p->tok->buf, '\n');
352-
const char* last_char = p->tok->cur - 1;
353-
if (loc != NULL && loc != last_char) {
354-
col_offset = p->tok->cur - loc - 1;
355-
p->tok->buf = loc;
356-
} else {
357-
col_offset = last_char - p->tok->buf - 1;
358-
}
351+
col_offset = p->tok->cur - p->tok->buf - 1;
359352
msg = "unexpected character after line continuation character";
360353
break;
361354
}
362355
default:
363356
msg = "unknown parsing error";
364357
}
365358

366-
RAISE_ERROR_KNOWN_LOCATION(p, errtype, p->tok->lineno, col_offset, msg);
359+
RAISE_ERROR_KNOWN_LOCATION(p, errtype, p->tok->lineno,
360+
col_offset >= 0 ? col_offset : 0, msg);
367361
return -1;
368362
}
369363

Parser/tokenizer.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1752,7 +1752,6 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
17521752
c = tok_nextc(tok);
17531753
if (c != '\n') {
17541754
tok->done = E_LINECONT;
1755-
tok->cur = tok->inp;
17561755
return ERRORTOKEN;
17571756
}
17581757
c = tok_nextc(tok);

0 commit comments

Comments
 (0)