Skip to content

Commit 633db1c

Browse files
authored
[3.10] bpo-46240: Correct the error for unclosed parentheses when the tokenizer is not finished (GH-30378). (GH-30819)
(cherry picked from commit 70f415f) Co-authored-by: Pablo Galindo Salgado <[email protected]>
1 parent f66ef3e commit 633db1c

File tree

4 files changed

+9
-2
lines changed

4 files changed

+9
-2
lines changed

Lib/test/test_exceptions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def testSyntaxErrorOffset(self):
227227
check('x = "a', 1, 5)
228228
check('lambda x: x = 2', 1, 1)
229229
check('f{a + b + c}', 1, 2)
230-
check('[file for str(file) in []\n])', 1, 11)
230+
check('[file for str(file) in []\n]', 1, 11)
231231
check('a = « hello » « world »', 1, 5)
232232
check('[\nfile\nfor str(file)\nin\n[]\n]', 3, 5)
233233
check('[file for\n str(file) in []]', 2, 2)

Lib/test/test_syntax.py

+3
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,9 @@ def test_error_parenthesis(self):
15131513
for paren in "([{":
15141514
self._check_error(paren + "1 + 2", f"\\{paren}' was never closed")
15151515

1516+
for paren in "([{":
1517+
self._check_error(f"a = {paren} 1, 2, 3\nb=3", f"\\{paren}' was never closed")
1518+
15161519
for paren in ")]}":
15171520
self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'")
15181521

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Correct the error message for unclosed parentheses when the tokenizer
2+
doesn't reach the end of the source when the error is reported. Patch by
3+
Pablo Galindo

Parser/pegen.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,8 @@ _PyPegen_run_parser(Parser *p)
13421342
if (PyErr_Occurred()) {
13431343
// Prioritize tokenizer errors to custom syntax errors raised
13441344
// on the second phase only if the errors come from the parser.
1345-
if (p->tok->done == E_DONE && PyErr_ExceptionMatches(PyExc_SyntaxError)) {
1345+
int is_tok_ok = (p->tok->done == E_DONE || p->tok->done == E_OK);
1346+
if (is_tok_ok && PyErr_ExceptionMatches(PyExc_SyntaxError)) {
13461347
_PyPegen_check_tokenizer_errors(p);
13471348
}
13481349
return NULL;

0 commit comments

Comments
 (0)