]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-122026: Fix identification of mismatched parentheses inside f-strings ...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 19 Jul 2024 17:32:34 +0000 (19:32 +0200)
committerGitHub <noreply@github.com>
Fri, 19 Jul 2024 17:32:34 +0000 (17:32 +0000)
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/lexer/lexer.c

index abec8081349fe8a3f151aa00e7c156b080dd2162..cf60c5084d60ba8e55dfd12bcf16645dc665faf5 100644 (file)
@@ -897,6 +897,7 @@ x = (
                              "f'{:2}'",
                              "f'''{\t\f\r\n:a}'''",
                              "f'{:'",
+                             "F'{[F'{:'}[F'{:'}]]]",
                              ])
 
         self.assertAllRaise(SyntaxError,
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 9ca3bd6a738e1bedc0c64956e763fbd254b9e2df..8c868593f944c8cb4bdc7d624b1056d2cad56a2b 100644 (file)
@@ -1238,6 +1238,9 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
 
         if (INSIDE_FSTRING(tok)) {
             current_tok->curly_bracket_depth--;
+            if (current_tok->curly_bracket_depth < 0) {
+                return MAKE_TOKEN(_PyTokenizer_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--;
                 current_tok->kind = TOK_FSTRING_MODE;