8000 bpo-46240: Correct the error for unclosed parentheses when the tokeni… · python/cpython@70f415f · GitHub
[go: up one dir, main page]

Skip to content

Commit 70f415f

Browse files
authored
bpo-46240: Correct the error for unclosed parentheses when the tokenizer is not finished (GH-30378)
1 parent a94461d commit 70f415f

File tree

4 files changed

+9
-2
lines changed

4 files changed

+9
-2
lines changed

Lib/test/test_exceptions.py

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,9 @@ def test_error_parenthesis(self):
16631663
for paren in "([{":
16641664
self._check_error(paren + "1 + 2", f"\\{paren}' was never closed")
16651665

1666+
for paren in "([{":
1667+
self._check_error(f"a = {paren} 1, 2, 3\nb=3", f"\\{paren}' was never closed")
1668+
16661669
for paren in ")]}":
16671670
self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'")
16681671

Lines changed: 3 additions & 0 deletions
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_errors.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@ _Pypegen_set_syntax_error(Parser* p, Token* last_token) {
388388
if (PyErr_Occurred()) {
389389
// Prioritize tokenizer errors to custom syntax errors raised
390390
// on the second phase only if the errors come from the parser.
391-
if (p->tok->done == E_DONE && PyErr_ExceptionMatches(PyExc_SyntaxError)) {
391+
int is_tok_ok = (p->tok->done == E_DONE || p->tok->done == E_OK);
392+
if (is_tok_ok && PyErr_ExceptionMatches(PyExc_SyntaxError)) {
392393
_PyPegen_tokenize_full_source_to_check_for_errors(p);
393394
}
394395
// Propagate the existing syntax error.

0 commit comments

Comments
 (0)
0