From 13d19fd28907ac3d605dbf7c749ef8f67515c851 Mon Sep 17 00:00:00 2001 From: Alon Faraj Date: Tue, 17 Nov 2026 12:16:29 +0200 Subject: [PATCH] [3.15] gh-162140: Fix lexer assertion for nested f-string with unclosed replacement field --- Lib/test/test_fstring.py | 10 ++++++++++ .../2026-11-17-10-14-03.gh-issue-162140.lYAERA.rst | 3 +++ Parser/lexer/lexer.c | 3 ++- Parser/lexer/state.h | 2 ++ 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-17-10-14-03.gh-issue-162140.lYAERA.rst diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index 66a214cecabadfd..08c92443d003c34 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -647,6 +647,16 @@ def test_unclosed_multiline_replacement_field(self): f"{prefix[-1]}-string: expecting '}}' to close '{{' " f"on line {lineno}") + def test_unclosed_replacement_field_after_nested_fstring(self): + # gh-162140: an inner f-string that ends with an unclosed '{' must + # not change which opening brace the diagnostic identifies. + import io, tokenize + source = 'f"{\n(\nf"{:\\\n""' + with self.assertRaises(tokenize.TokenError) as cm: + list(tokenize.generate_tokens(io.StringIO(source).readline)) + self.assertEqual(cm.exception.args[0], + "f-string: expecting '}' to close '{' on line 1") + @unittest.skipIf(support.is_wasi, "exhausts limited stack on WASI") def test_mismatched_parens(self): self.assertAllRaise(SyntaxError, r"closing parenthesis '\}' " diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-17-10-14-03.gh-issue-162140.lYAERA.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-17-10-14-03.gh-issue-162140.lYAERA.rst new file mode 100644 index 000000000000000..9b96064a5fc4e1e --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-17-10-14-03.gh-issue-162140.lYAERA.rst @@ -0,0 +1,3 @@ +Fix an assertion failure in the lexer, and a wrong line number in the +error message, for an unclosed f-string replacement field that contains a +nested f-string which itself ends with an unclosed replacement field. diff --git a/Parser/lexer/lexer.c b/Parser/lexer/lexer.c index 496c2df58c55175..b5256e27767c727 100644 --- a/Parser/lexer/lexer.c +++ b/Parser/lexer/lexer.c @@ -1082,6 +1082,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t the_current_tok->string_kind = string_kind; the_current_tok->curly_bracket_depth = 0; the_current_tok->curly_bracket_expr_start_depth = -1; + the_current_tok->paren_level = tok->level; return string_kind == TSTRING ? MAKE_TOKEN(TSTRING_START) : MAKE_TOKEN(FSTRING_START); } @@ -1144,7 +1145,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t tokenizer_mode *the_current_tok = TOK_GET_MODE(tok); if (the_current_tok->quote == quote && the_current_tok->quote_size == quote_size) { - int level = tok->level - the_current_tok->curly_bracket_depth + int level = the_current_tok->paren_level + the_current_tok->curly_bracket_expr_start_depth; assert(level >= 0 && level < tok->level); assert(tok->parenstack[level] == '{'); diff --git a/Parser/lexer/state.h b/Parser/lexer/state.h index 939fdb7587f6ee0..8b3d68f2157351f 100644 --- a/Parser/lexer/state.h +++ b/Parser/lexer/state.h @@ -50,6 +50,8 @@ typedef struct _tokenizer_mode { int curly_bracket_depth; int curly_bracket_expr_start_depth; + /* Value of tok->level when the f-string started. */ + int paren_level; char quote; int quote_size;