From 966eff9ce481b8488c359d68f1b024bfd6ed47e2 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Sun, 21 Jul 2024 01:03:10 +0200 Subject: [PATCH] [3.12] gh-122026: Fix identification of mismatched parentheses inside f-strings (GH-122028) (#122062) (cherry picked from commit 2009e25e26040dca32696e70f91f13665350e7fd) Signed-off-by: Pablo Galindo --- Lib/test/test_fstring.py | 1 + .../2024-07-19-15-28-05.gh-issue-122026.sta2Ca.rst | 2 ++ Parser/tokenizer.c | 3 +++ 3 files changed, 6 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-07-19-15-28-05.gh-issue-122026.sta2Ca.rst diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index 76dd9e89ca3d..7fc370238281 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -921,6 +921,7 @@ x = ( "f'{:2}'", "f'''{\t\f\r\n:a}'''", "f'{:'", + "F'{[F'{:'}[F'{:'}]]]", ], ) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-07-19-15-28-05.gh-issue-122026.sta2Ca.rst b/Misc/NEWS.d/next/Core and Builtins/2024-07-19-15-28-05.gh-issue-122026.sta2Ca.rst new file mode 100644 index 000000000000..2721a405a504 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-07-19-15-28-05.gh-issue-122026.sta2Ca.rst @@ -0,0 +1,2 @@ +Fix a bug that caused the tokenizer to not correctly identify mismatched +parentheses inside f-strings in some situations. Patch by Pablo Galindo diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index 703b3ec7a6b6..3118fb198465 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -2607,6 +2607,9 @@ letter_quote: if (INSIDE_FSTRING(tok)) { current_tok->curly_bracket_depth--; + if (current_tok->curly_bracket_depth < 0) { + return MAKE_TOKEN(syntaxerror(tok, "f-string: unmatched '%c'", c)); + } if (c == '}' && current_tok->curly_bracket_depth == current_tok->curly_bracket_expr_start_depth) { current_tok->curly_bracket_expr_start_depth--; -- 2.47.3