8000 gh-100050: Fix an assertion error when raising unclosed parenthesis e… · python/cpython@2b97ddd · GitHub
[go: up one dir, main page]

Skip to content

Commit 2b97ddd

Browse files
gh-100050: Fix an assertion error when raising unclosed parenthesis errors in the tokenizer (GH-100065)
(cherry picked from commit 97e7004) Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com> Automerge-Triggered-By: GH:pablogsal
1 parent fbc3e1e commit 2b97ddd

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

Lib/test/test_syntax.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,22 @@ def test_error_parenthesis(self):
20952095
for paren in ")]}":
20962096
self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'")
20972097

2098+
# Some more complex examples:
2099+
code = """\
2100+
func(
2101+
a=["unclosed], # Need a quote in this comment: "
2102+
b=2,
2103+
)
2104+
"""
2105+
self._check_error(code, "parenthesis '\\)' does not match opening parenthesis '\\['")
2106+
2107+
def test_error_string_literal(self):
2108+
2109+
self._check_error("'blech", "unterminated string literal")
2110+
self._check_error('"blech', "unterminated string literal")
2111+
self._check_error("'''blech", "unterminated triple-quoted string literal")
2112+
self._check_error('"""blech', "unterminated triple-quoted string literal")
2113+
20982114
def test_invisible_characters(self):
20992115
self._check_error('print\x17("Hello")', "invalid non-printable character")
21002116

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
@@ -170,6 +170,10 @@ _PyPegen_tokenize_full_source_to_check_for_errors(Parser *p) {
170170
const char *end;
171171
switch (_PyTokenizer_Get(p->tok, &start, &end)) {
172172
case ERRORTOKEN:
173+
if (PyErr_Occurred()) {
174+
ret = -1;
175+
goto exit;
176+
}
173177
if (p->tok->level != 0) {
174178
int error_lineno = p->tok->parenlinenostack[p->tok->level-1];
175179
if (current_err_line > error_lineno) {

0 commit comments

Comments
 (0)
0