Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash in :mod:`tokenize` when a mismatched closing bracket follows the
opening brace of an f-string or t-string replacement field, as in ``f'{]a'``.
It now raises the same "unmatched" error as the parser.
11 changes: 7 additions & 4 deletions Parser/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1283,9 +1283,9 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
if (tok->level > 0) {
tok->level--;
int opening = tok->parenstack[tok->level];
if (!tok->tok_extra_tokens && !((opening == '(' && c == ')') ||
(opening == '[' && c == ']') ||
(opening == '{' && c == '}'))) {
if (!((opening == '(' && c == ')') ||
(opening == '[' && c == ']') ||
(opening == '{' && c == '}'))) {
/* If the opening bracket belongs to an f-string's expression
part (e.g. f"{)}") and the closing bracket is an arbitrary
nested expression, then instead of matching a different
Expand All @@ -1299,7 +1299,10 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
"%c-string: unmatched '%c'", TOK_GET_STRING_PREFIX(tok), c));
}
}
if (tok->parenlinenostack[tok->level] != tok->lineno) {
if (tok->tok_extra_tokens) {
/* Allow other mismatched brackets. */
}
else if (tok->parenlinenostack[tok->level] != tok->lineno) {
return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok,
"closing parenthesis '%c' does not match "
"opening parenthesis '%c' on line %d",
Expand Down
Loading