Skip to content

Commit

Permalink
[3.10] pythongh-100050: Fix an assertion error when raising unclosed …
Browse files Browse the repository at this point in the history
…parenthesis errors in the tokenizer (pythonGH-100065)

Automerge-Triggered-By: GH:pablogsal.
(cherry picked from commit 97e7004)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
  • Loading branch information
pablogsal committed Dec 7, 2022
1 parent 3843973 commit 5a69f95
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,22 @@ def test_error_parenthesis(self):
for paren in ")]}":
self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'")

# Some more complex examples:
code = """\
func(
a=["unclosed], # Need a quote in this comment: "
b=2,
)
"""
self._check_error(code, "parenthesis '\\)' does not match opening parenthesis '\\['")

def test_error_string_literal(self):

self._check_error("'blech", "unterminated string literal")
self._check_error('"blech', "unterminated string literal")
self._check_error("'''blech", "unterminated triple-quoted string literal")
self._check_error('"""blech', "unterminated triple-quoted string literal")

def test_match_call_does_not_raise_syntax_error(self):
code = """
def match(x):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Honor existing errors obtained when searching for mismatching parentheses in
the tokenizer. Patch by Pablo Galindo
4 changes: 4 additions & 0 deletions Parser/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,10 @@ _PyPegen_check_tokenizer_errors(Parser *p) {
const char *end;
switch (PyTokenizer_Get(p->tok, &start, &end)) {
case ERRORTOKEN:
if (PyErr_Occurred()) {
ret = -1;
goto exit;
}
if (p->tok->level != 0) {
int error_lineno = p->tok->parenlinenostack[p->tok->level-1];
if (current_err_line > error_lineno) {
Expand Down

0 comments on commit 5a69f95

Please sign in to comment.