]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-122026: Fix identification of mismatched parentheses inside f-strings ...
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Sat, 20 Jul 2024 23:03:10 +0000 (01:03 +0200)
committerGitHub <noreply@github.com>
Sat, 20 Jul 2024 23:03:10 +0000 (23:03 +0000)
(cherry picked from commit 2009e25e26040dca32696e70f91f13665350e7fd)

Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
Lib/test/test_fstring.py
Misc/NEWS.d/next/Core and Builtins/2024-07-19-15-28-05.gh-issue-122026.sta2Ca.rst [new file with mode: 0644]
Parser/tokenizer.c

index 76dd9e89ca3d8e7f99d4ed20df7096a14861f2c5..7fc3702382813eb3b5dcd653a5007743ae6775ca 100644 (file)
@@ -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 (file)
index 0000000..2721a40
--- /dev/null
@@ -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
index 703b3ec7a6b6614e3963b1511bcf38faec887c7b..3118fb198465781154e545b84852fdb45c22d2a1 100644 (file)
@@ -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--;