Skip to content

Commit b51081c

Browse files
authored
bpo-44180: Report generic syntax errors in the furthest position reached in the first parser pass (GH-26253)
1 parent b11a951 commit b51081c

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

Lib/test/test_exceptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ def testSyntaxErrorOffset(self):
213213
check('[file for str(file) in []\n])', 2, 2)
214214
check('[\nfile\nfor str(file)\nin\n[]\n]', 3, 5)
215215
check('[file for\n str(file) in []]', 2, 2)
216+
check("ages = {'Alice'=22, 'Bob'=23}", 1, 16)
216217

217218
# Errors thrown by compile.c
218219
check('class foo:return 1', 1, 11)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The parser doesn't report generic syntax errors that happen in a position
2+
further away that the one it reached in the first pass. Patch by Pablo
3+
Galindo

Parser/pegen.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,7 @@ _PyPegen_run_parser(Parser *p)
12811281
{
12821282
void *res = _PyPegen_parse(p);
12831283
if (res == NULL) {
1284+
Token *last_token = p->tokens[p->fill - 1];
12841285
reset_parser_state(p);
12851286
_PyPegen_parse(p);
12861287
if (PyErr_Occurred()) {
@@ -1307,7 +1308,11 @@ _PyPegen_run_parser(Parser *p)
13071308
RAISE_INDENTATION_ERROR("unexpected unindent");
13081309
}
13091310
else {
1310-
RAISE_SYNTAX_ERROR("invalid syntax");
1311+
// Use the last token we found on the first pass to avoid reporting
1312+
// incorrect locations for generic syntax errors just because we reached
1313+
// further away when trying to find specific syntax errors in the second
1314+
// pass.
1315+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(last_token, "invalid syntax");
13111316
// _PyPegen_check_tokenizer_errors will override the existing
13121317
// generic SyntaxError we just raised if errors are found.
13131318
_PyPegen_check_tokenizer_errors(p);

0 commit comments

Comments
 (0)