From bde70e98fb00fecb8ae1319491bc1f96ed2317af Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Fri, 19 Jul 2024 15:28:34 +0200 Subject: [PATCH 1/2] gh-122026: Fix identification of mismatched parentheses inside f-strings --- Lib/test/test_fstring.py | 1 + .../2024-07-19-15-28-05.gh-issue-122026.sta2Ca.rst | 2 ++ Parser/lexer/lexer.c | 3 +++ 3 files changed, 6 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-07-19-15-28-05.gh-issue-122026.sta2Ca.rst diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index 49c6f761e5b4f0..fc240fd6a783ad 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -895,6 +895,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..193613f40fd975 --- /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 identified 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 82b0e4ee352d62..c66f753f7d4f89 100644 --- a/Parser/lexer/lexer.c +++ b/Parser/lexer/lexer.c @@ -1232,6 +1232,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; From 001d9ed9a2537f7a7835aaf5c53ffd3119a3f6a2 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Fri, 19 Jul 2024 16:03:48 +0200 Subject: [PATCH 2/2] Update 2024-07-19-15-28-05.gh-issue-122026.sta2Ca.rst Co-authored-by: Tomas R --- .../2024-07-19-15-28-05.gh-issue-122026.sta2Ca.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 193613f40fd975..2721a405a50446 100644 --- 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 @@ -1,2 +1,2 @@ -Fix a bug that caused the tokenizer to not correctly identified mismatched +Fix a bug that caused the tokenizer to not correctly identify mismatched parentheses inside f-strings in some situations. Patch by Pablo Galindo