From 5edd0481b9b050f198ffe5c9aa2d8b0ae2c0bdcf Mon Sep 17 00:00:00 2001 From: "Eric V. Smith" Date: Wed, 1 Jun 2022 19:20:06 -0400 Subject: [PATCH] gh-93418: Fix an assert when an f-string expression is followed by an '=', but no closing brace. (gh-93419) (cherry picked from commit ee70c70aa93d7a41cbe47a0b361b17f9d7ec8acd) Co-authored-by: Eric V. Smith --- Lib/test/test_fstring.py | 1 + .../2022-06-01-17-47-40.gh-issue-93418.24dJuc.rst | 2 ++ Parser/string_parser.c | 4 +++- 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-06-01-17-47-40.gh-issue-93418.24dJuc.rst diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index 0c3372f0335517..93aa229709a0a5 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -1073,6 +1073,7 @@ def test_mismatched_braces(self): "f'{'", "f'x{<'", # See bpo-46762. "f'x{>'", + "f'{i='", # See gh-93418. ]) # But these are just normal strings. diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-06-01-17-47-40.gh-issue-93418.24dJuc.rst b/Misc/NEWS.d/next/Core and Builtins/2022-06-01-17-47-40.gh-issue-93418.24dJuc.rst new file mode 100644 index 00000000000000..74ad06bfeee7c4 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-06-01-17-47-40.gh-issue-93418.24dJuc.rst @@ -0,0 +1,2 @@ +Fixed an assert where an f-string has an equal sign '=' following an +expression, but there's no trailing brace. For example, f"{i=". diff --git a/Parser/string_parser.c b/Parser/string_parser.c index 9c12d8ca101d00..5e94d477bc04a8 100644 --- a/Parser/string_parser.c +++ b/Parser/string_parser.c @@ -756,7 +756,9 @@ fstring_find_expr(Parser *p, const char **str, const char *end, int raw, int rec while (Py_ISSPACE(**str)) { *str += 1; } - + if (*str >= end) { + goto unexpected_end_of_string; + } /* Set *expr_text to the text of the expression. */ *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start); if (!*expr_text) {