Skip to content

Commit a6c1081

Browse files
[3.13] gh-122026: Fix identification of mismatched parentheses inside f-strings (GH-122028) (#122041)
1 parent 9a332f2 commit a6c1081

File tree

3 files changed

+6
-0
lines changed

3 files changed

+6
-0
lines changed

Lib/test/test_fstring.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,7 @@ def test_missing_expression(self):
897897
"f'{:2}'",
898898
"f'''{\t\f\r\n:a}'''",
899899
"f'{:'",
900+
"F'{[F'{:'}[F'{:'}]]]",
900901
])
901902

902903
self.assertAllRaise(SyntaxError,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug that caused the tokenizer to not correctly identify mismatched
2+
parentheses inside f-strings in some situations. Patch by Pablo Galindo

Parser/lexer/lexer.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,9 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
12381238

12391239
if (INSIDE_FSTRING(tok)) {
12401240
current_tok->curly_bracket_depth--;
1241+
if (current_tok->curly_bracket_depth < 0) {
1242+
return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok, "f-string: unmatched '%c'", c));
1243+
}
12411244
if (c == '}' && current_tok->curly_bracket_depth == current_tok->curly_bracket_expr_start_depth) {
12421245
current_tok->curly_bracket_expr_start_depth--;
12431246
current_tok->kind = TOK_FSTRING_MODE;

0 commit comments

Comments
 (0)