Skip to content

Commit 0219017

Browse files
authored
bpo-45408: Don't override previous tokenizer errors in the second parser pass (GH-28812)
1 parent 6811fda commit 0219017

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

Lib/test/test_ast.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,14 @@ def test_literal_eval_malformed_lineno(self):
10751075
with self.assertRaisesRegex(ValueError, msg):
10761076
ast.literal_eval(node)
10771077

1078+
def test_literal_eval_syntax_errors(self):
1079+
msg = "unexpected character after line continuation character"
1080+
with self.assertRaisesRegex(SyntaxError, msg):
1081+
ast.literal_eval(r'''
1082+
\
1083+
(\
1084+
\ ''')
1085+
10781086
def test_bad_integer(self):
10791087
# issue13436: Bad error message with invalid numeric values
10801088
body = [ast.ImportFrom(module='time',

Lib/test/test_exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def testSyntaxErrorOffset(self):
223223
check('x = "a', 1, 5)
224224
check('lambda x: x = 2', 1, 1)
225225
check('f{a + b + c}', 1, 1)
226-
check('[file for str(file) in []\n])', 2, 2)
226+
check('[file for str(file) in []\n])', 1, 11)
227227
check('a = « hello » « world »', 1, 5)
228228
check('[\nfile\nfor str(file)\nin\n[]\n]', 3, 5)
229229
check('[file for\n str(file) in []]', 2, 2)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash in the parser when reporting tokenizer errors that occur at the
2+
same time unclosed parentheses are detected. Patch by Pablo Galindo.

Parser/pegen.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1342,13 +1342,16 @@ _PyPegen_run_parser(Parser *p)
13421342
{
13431343
void *res = _PyPegen_parse(p);
13441344
if (res == NULL) {
1345+
if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_SyntaxError)) {
1346+
return NULL;
1347+
}
13451348
Token *last_token = p->tokens[p->fill - 1];
13461349
reset_parser_state(p);
13471350
_PyPegen_parse(p);
13481351
if (PyErr_Occurred()) {
13491352
// Prioritize tokenizer errors to custom syntax errors raised
13501353
// on the second phase only if the errors come from the parser.
1351-
if (p->tok->done != E_ERROR && PyErr_ExceptionMatches(PyExc_SyntaxError)) {
1354+
if (p->tok->done == E_DONE && PyErr_ExceptionMatches(PyExc_SyntaxError)) {
13521355
_PyPegen_check_tokenizer_errors(p);
13531356
}
13541357
return NULL;

0 commit comments

Comments
 (0)