From: Pablo Galindo Salgado Date: Sat, 20 Jul 2024 23:03:10 +0000 (+0200) Subject: [3.12] gh-122026: Fix identification of mismatched parentheses inside f-strings ... X-Git-Tag: v3.12.5~72 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=966eff9ce481b8488c359d68f1b024bfd6ed47e2;p=thirdparty%2FPython%2Fcpython.git [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 --- 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--;