Skip to content

Commit 13d19fd

Browse files
committed
[3.15] gh-162140: Fix lexer assertion for nested f-string with unclosed replacement field
1 parent aded5b9 commit 13d19fd

4 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎Lib/test/test_fstring.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,16 @@ def test_unclosed_multiline_replacement_field(self):
647647
f"{prefix[-1]}-string: expecting '}}' to close '{{' "
648648
f"on line {lineno}")
649649

650+
def test_unclosed_replacement_field_after_nested_fstring(self):
651+
# gh-162140: an inner f-string that ends with an unclosed '{' must
652+
# not change which opening brace the diagnostic identifies.
653+
import io, tokenize
654+
source = 'f"{\n(\nf"{:\\\n""'
655+
with self.assertRaises(tokenize.TokenError) as cm:
656+
list(tokenize.generate_tokens(io.StringIO(source).readline))
657+
self.assertEqual(cm.exception.args[0],
658+
"f-string: expecting '}' to close '{' on line 1")
659+
650660
@unittest.skipIf(support.is_wasi, "exhausts limited stack on WASI")
651661
def test_mismatched_parens(self):
652662
self.assertAllRaise(SyntaxError, r"closing parenthesis '\}' "
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix an assertion failure in the lexer, and a wrong line number in the
2+
error message, for an unclosed f-string replacement field that contains a
3+
nested f-string which itself ends with an unclosed replacement field.

‎Parser/lexer/lexer.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
10821082
the_current_tok->string_kind = string_kind;
10831083
the_current_tok->curly_bracket_depth = 0;
10841084
the_current_tok->curly_bracket_expr_start_depth = -1;
1085+
the_current_tok->paren_level = tok->level;
10851086
return string_kind == TSTRING ? MAKE_TOKEN(TSTRING_START) : MAKE_TOKEN(FSTRING_START);
10861087
}
10871088

@@ -1144,7 +1145,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
11441145
tokenizer_mode *the_current_tok = TOK_GET_MODE(tok);
11451146
if (the_current_tok->quote == quote &&
11461147
the_current_tok->quote_size == quote_size) {
1147-
int level = tok->level - the_current_tok->curly_bracket_depth
1148+
int level = the_current_tok->paren_level
11481149
+ the_current_tok->curly_bracket_expr_start_depth;
11491150
assert(level >= 0 && level < tok->level);
11501151
assert(tok->parenstack[level] == '{');

‎Parser/lexer/state.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ typedef struct _tokenizer_mode {
5050

5151
int curly_bracket_depth;
5252
int curly_bracket_expr_start_depth;
53+
/* Value of tok->level when the f-string started. */
54+
int paren_level;
5355

5456
char quote;
5557
int quote_size;

0 commit comments

Comments
 (0)