Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,32 @@ def test_error_parenthesis(self):
for paren in ")]}":
self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'")

self._check_error("(a+b+c])", "']' was never opened")
self._check_error("[a+b+c)]", "'\\)' was never opened")
self._check_error("(a+b+c})", "'}' was never opened")

self._check_error("""\\
(
a+b+c]


)""", "']' was never opened", lineno=3, offset=17)

self._check_error("""\\
(
a+b+c]


# )""", "']' does not match opening", lineno=3, offset=17)

self._check_error("""\\
(
a+b+c]


# )
)""", "']' was never opened", lineno=3, offset=17)

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 @@
Improve the error message in the parser for closing parentheses or brackets
that were never opened. Patch by Pablo Galindo.
65 changes: 65 additions & 0 deletions Parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,66 @@ tok_decimal_tail(struct tok_state *tok)
return c;
}

static int
paren_will_be_closed(struct tok_state *tok, char opening) {
char target;
switch (opening) {
case '(':
target = ')';
break;
case '[':
target = ']';
break;
case '{':
target = '}';
break;
default:
Py_UNREACHABLE();
}

int found = 0;
char c = 0;
int lineno = tok->lineno;
size_t line_start_offset = tok->cur - tok->line_start;
size_t advanced_chars = 0;

for (;;) {
c = tok_nextc(tok);
advanced_chars++;
if (!c || c == EOF) {
goto exit;
}
// Skip comments
if (c == '#') {
while (c != EOF && c != '\n') {
c = tok_nextc(tok);
advanced_chars++;
}
}

// In interactive mode, we don't want to consume more input
// just to check if the parentheses will be closed.
if (tok->fp_interactive && c == '\n') {
tok_backup(tok, c);
return 0;
}
if (c != target) {
continue;
}
found = 1;
break;
}
exit:
// Backup to the place when we started
if (tok->cur - advanced_chars < tok->buf) {
Py_FatalError("tokenizer beginning of buffer");
}
tok->cur = tok->cur - advanced_chars;
tok->line_start = tok->cur - line_start_offset + 1;
tok->lineno = lineno;
return found;
}

/* Get next token, after space stripping etc. */

static int
Expand Down Expand Up @@ -1847,6 +1907,11 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
(opening == '[' && c == ']') ||
(opening == '{' && c == '}')))
{
if (paren_will_be_closed(tok, opening)) {
return syntaxerror(tok, "closing parenthesis '%c' was never opened",
c, tok->parenlinenostack[tok->level]);
}

if (tok->parenlinenostack[tok->level] != tok->lineno) {
return syntaxerror(tok,
"closing parenthesis '%c' does not match "
Expand Down