https://github.com/python/cpython/commit/966eff9ce481b8488c359d68f1b024bfd6ed47e2
commit: 966eff9ce481b8488c359d68f1b024bfd6ed47e2
branch: 3.12
author: Pablo Galindo Salgado <[email protected]>
committer: pablogsal <[email protected]>
date: 2024-07-20T23:03:10Z
summary:

[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 <[email protected]>

files:
A Misc/NEWS.d/next/Core and 
Builtins/2024-07-19-15-28-05.gh-issue-122026.sta2Ca.rst
M Lib/test/test_fstring.py
M Parser/tokenizer.c

diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
index 76dd9e89ca3d8e..7fc3702382813e 100644
--- a/Lib/test/test_fstring.py
+++ b/Lib/test/test_fstring.py
@@ -921,6 +921,7 @@ def test_missing_expression(self):
                 "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 00000000000000..2721a405a50446
--- /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 703b3ec7a6b661..3118fb19846578 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -2607,6 +2607,9 @@ static int tok_get_normal_mode(struct tok_state *tok,
 
     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--;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to