https://github.com/python/cpython/commit/a6c1081cbe8a8d948e0a8335fa17ce75a528c7e0
commit: a6c1081cbe8a8d948e0a8335fa17ce75a528c7e0
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: pablogsal <[email protected]>
date: 2024-07-19T17:32:34Z
summary:
[3.13] gh-122026: Fix identification of mismatched parentheses inside f-strings
(GH-122028) (#122041)
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/lexer/lexer.c
diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
index abec8081349fe8..cf60c5084d60ba 100644
--- a/Lib/test/test_fstring.py
+++ b/Lib/test/test_fstring.py
@@ -897,6 +897,7 @@ def test_missing_expression(self):
"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
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/lexer/lexer.c b/Parser/lexer/lexer.c
index 9ca3bd6a738e1b..8c868593f944c8 100644
--- a/Parser/lexer/lexer.c
+++ b/Parser/lexer/lexer.c
@@ -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;
_______________________________________________
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]