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
20 changes: 11 additions & 9 deletions Lib/test/test_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2675,15 +2675,17 @@ def test_tolerant_incompatible_prefix_position_after_non_ascii(self):
)

def test_tolerant_fstring_closer_at_expression_entry_depth(self):
source = "f'1:{]]{}}r}''"
for extra_tokens, position in ((False, (1, 6)), (True, (1, 7))):
with self.subTest(extra_tokens=extra_tokens):
with self.assertRaises(tokenize.TokenError) as caught:
self._get_tokens(source, extra_tokens=extra_tokens)
self.assertEqual(
caught.exception.args,
("f-string: unmatched ']'", position),
)
cases = [
("f'1:{]]{}}r}''", ("f-string: unmatched ']'", (1, 6))),
("f'{]a'", ("f-string: unmatched ']'", (1, 4))),
("t'{\nx)'", ("t-string: unmatched ')'", (2, 2))),
]
for source, error in cases:
for extra_tokens in (False, True):
with self.subTest(source=source, extra_tokens=extra_tokens):
with self.assertRaises(tokenize.TokenError) as caught:
self._get_tokens(source, extra_tokens=extra_tokens)
self.assertEqual(caught.exception.args, error)

def test_int(self):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
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.
14 changes: 9 additions & 5 deletions Parser/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,12 @@ _PyLexer_get_normal(struct tok_state *tok, ftstring_state *current, struct token
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 == '}'))) {
/* Do not match a closer against the brace that opened the
* current replacement field. */
* current replacement field. The f-string state relies on
* that brace, so this is checked even with extra tokens. */
if (current != NULL && opening == '{') {
int bracket_depth =
_PyLexer_FTStringBracketDepth(tok, current);
Expand All @@ -461,7 +462,10 @@ _PyLexer_get_normal(struct tok_state *tok, ftstring_state *current, struct token
_PyLexer_StringPrefix(current->kind), 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