Skip to content

Commit 4d8cf74

Browse files
committed
gh-100050: Fix an assertion error when raising unclosed parenthesis errors in the tokenizer
1 parent a87c46e commit 4d8cf74

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Lib/test/test_syntax.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,6 +2145,23 @@ def test_error_parenthesis(self):
21452145
for paren in ")]}":
21462146
self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'")
21472147

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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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)