From 7b44ade018cfe6f54002a3cee43e8aa415d4d635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20G=C3=B3rski?= <36813763+macgors@users.noreply.github.com> Date: Mon, 28 Mar 2022 23:08:36 +0200 Subject: [PATCH] bpo-47129: Add more informative messages to f-string syntax errors (32127) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add more informative messages to f-string syntax errors * 📜🤖 Added by blurb_it. * Fix whitespaces * Change error message * Remove the 'else' statement (as sugested in review) Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> --- Lib/test/test_fstring.py | 40 ++++++++++++++----- .../2022-03-26-16-35-57.bpo-47129.hDg2Vt.rst | 1 + Parser/string_parser.c | 5 +++ 3 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-03-26-16-35-57.bpo-47129.hDg2Vt.rst diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index 0c255c2af22..0c3372f0335 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -628,16 +628,27 @@ x = ( ["f'{}'", "f'{ }'" "f' {} '", - "f'{!r}'", - "f'{ !r}'", "f'{10:{ }}'", "f' { } '", # The Python parser ignores also the following # whitespace characters in additional to a space. "f'''{\t\f\r\n}'''", + ]) - # Catch the empty expression before the + # Different error messeges are raised when a specfier ('!', ':' or '=') is used after an empty expression + self.assertAllRaise(SyntaxError, "f-string: expression required before '!'", + ["f'{!r}'", + "f'{ !r}'", + "f'{!}'", + "f'''{\t\f\r\n!a}'''", + + # Catch empty expression before the + # missing closing brace. + "f'{!'", + "f'{!s:'", + + # Catch empty expression before the # invalid conversion. "f'{!x}'", "f'{ !xr}'", @@ -645,16 +656,23 @@ x = ( "f'{!x:a}'", "f'{ !xr:}'", "f'{ !xr:a}'", + ]) - "f'{!}'", - "f'{:}'", - - # We find the empty expression before the - # missing closing brace. - "f'{!'", - "f'{!s:'", + self.assertAllRaise(SyntaxError, "f-string: expression required before ':'", + ["f'{:}'", + "f'{ :!}'", + "f'{:2}'", + "f'''{\t\f\r\n:a}'''", "f'{:'", - "f'{:x'", + ]) + + self.assertAllRaise(SyntaxError, "f-string: expression required before '='", + ["f'{=}'", + "f'{ =}'", + "f'{ =:}'", + "f'{ =!}'", + "f'''{\t\f\r\n=}'''", + "f'{='", ]) # Different error message is raised for other whitespace characters. diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-03-26-16-35-57.bpo-47129.hDg2Vt.rst b/Misc/NEWS.d/next/Core and Builtins/2022-03-26-16-35-57.bpo-47129.hDg2Vt.rst new file mode 100644 index 00000000000..1627aba41d6 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-03-26-16-35-57.bpo-47129.hDg2Vt.rst @@ -0,0 +1 @@ +Improve error messages in f-string syntax errors concerning empty expressions. diff --git a/Parser/string_parser.c b/Parser/string_parser.c index fae2a3648cf..65ddd46874b 100644 --- a/Parser/string_parser.c +++ b/Parser/string_parser.c @@ -357,7 +357,12 @@ fstring_compile_expr(Parser *p, const char *expr_start, const char *expr_end, break; } } + if (s == expr_end) { + if (*expr_end == '!' || *expr_end == ':' || *expr_end == '=') { + RAISE_SYNTAX_ERROR("f-string: expression required before '%c'", *expr_end); + return NULL; + } RAISE_SYNTAX_ERROR("f-string: empty expression not allowed"); return NULL; }