Skip to content

Commit 97e7004

Browse files
authored
gh-100050: Fix an assertion error when raising unclosed parenthesis errors in the tokenizer (GH-100065)
Automerge-Triggered-By: GH:pablogsal
1 parent abbe448 commit 97e7004

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

Lib/test/test_syntax.py

+16
Original file line numberDiff line numberDiff line change
@@ -2145,6 +2145,22 @@ def test_error_parenthesis(self):
21452145
for paren in ")]}":
21462146
self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'")
21472147

2148+
# Some more complex examples:
2149+
code = """\
2150+
func(
2151+
a=["unclosed], # Need a quote in this comment: "
2152+
b=2,
2153+
)
2154+
"""
2155+
self._check_error(code, "parenthesis '\\)' does not match opening parenthesis '\\['")
2156+
2157+
def test_error_string_literal(self):
2158+
2159+
self._check_error("'blech", "unterminated string literal")
2160+
self._check_error('"blech', "unterminated string literal")
2161+
self._check_error("'''blech", "unterminated triple-quoted string literal")
2162+
self._check_error('"""blech', "unterminated triple-quoted string literal")
2163+
21482164
def test_invisible_characters(self):
21492165
self._check_error('print\x17("Hello")', "invalid non-printable character")
21502166

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Honor existing errors obtained when searching for mismatching parentheses in
2+
the tokenizer. Patch by Pablo Galindo

Parser/pegen_errors.c

+4
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ _PyPegen_tokenize_full_source_to_check_for_errors(Parser *p) {
169169
for (;;) {
170170
switch (_PyTokenizer_Get(p->tok, &new_token)) {
171171
case ERRORTOKEN:
172+
if (PyErr_Occurred()) {
173+
ret = -1;
174+
goto exit;
175+
}
172176
if (p->tok->level != 0) {
173177
int error_lineno = p->tok->parenlinenostack[p->tok->level-1];
174178
if (current_err_line > error_lineno) {

0 commit comments

Comments
 (0)