Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def testSyntaxErrorOffset(self):
check('[file for str(file) in []\n])', 2, 2)
check('[\nfile\nfor str(file)\nin\n[]\n]', 3, 5)
check('[file for\n str(file) in []]', 2, 2)
check("ages = {'Alice'=22, 'Bob'=23}", 1, 16)

# Errors thrown by compile.c
check('class foo:return 1', 1, 11)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The parser doesn't report generic syntax errors that happen in a position
further away that the one it reached in the first pass. Patch by Pablo
Galindo
7 changes: 6 additions & 1 deletion Parser/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,7 @@ _PyPegen_run_parser(Parser *p)
{
void *res = _PyPegen_parse(p);
if (res == NULL) {
Token *last_token = p->tokens[p->fill - 1];
reset_parser_state(p);
_PyPegen_parse(p);
if (PyErr_Occurred()) {
Expand All @@ -1307,7 +1308,11 @@ _PyPegen_run_parser(Parser *p)
RAISE_INDENTATION_ERROR("unexpected unindent");
}
else {
RAISE_SYNTAX_ERROR("invalid syntax");
// Use the last token we found on the first pass to avoid reporting
// incorrect locations for generic syntax errors just because we reached
// further away when trying to find specific syntax errors in the second
// pass.
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(last_token, "invalid syntax");
// _PyPegen_check_tokenizer_errors will override the existing
// generic SyntaxError we just raised if errors are found.
_PyPegen_check_tokenizer_errors(p);
Expand Down