From 097c79eb0c98e0d4911825a7afeca59a38ea9edf Mon Sep 17 00:00:00 2001 From: Shantanu Date: Wed, 27 May 2020 13:30:38 -0700 Subject: [PATCH] bpo-40614: Respect feature version for f-string debug expressions (GH-20196) Co-authored-by: Lysandros Nikolaou Co-authored-by: Pablo Galindo (cherry picked from commit c116c94ff119485761460f1033cdee425bed0310) --- Lib/test/test_ast.py | 6 ++++++ .../next/Library/2020-05-18-22-41-02.bpo-40614.8j3kmq.rst | 1 + Python/ast.c | 6 ++++++ 3 files changed, 13 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-05-18-22-41-02.bpo-40614.8j3kmq.rst diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index 486f2aa707e83f..869346664499c1 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -630,6 +630,12 @@ def test_issue39579_dotted_name_end_col_offset(self): attr_b = tree.body[0].decorator_list[0].value self.assertEqual(attr_b.end_col_offset, 4) + def test_issue40614_feature_version(self): + ast.parse('f"{x=}"', feature_version=(3, 8)) + with self.assertRaises(SyntaxError): + ast.parse('f"{x=}"', feature_version=(3, 7)) + + class ASTHelpers_Test(unittest.TestCase): maxDiff = None diff --git a/Misc/NEWS.d/next/Library/2020-05-18-22-41-02.bpo-40614.8j3kmq.rst b/Misc/NEWS.d/next/Library/2020-05-18-22-41-02.bpo-40614.8j3kmq.rst new file mode 100644 index 00000000000000..238b98c14a3269 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-05-18-22-41-02.bpo-40614.8j3kmq.rst @@ -0,0 +1 @@ +:func:`ast.parse` will not parse self documenting expressions in f-strings when passed ``feature_version`` is less than ``(3, 8)``. diff --git a/Python/ast.c b/Python/ast.c index f70d48ba3a15da..594879bd0ef0bd 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -5202,6 +5202,12 @@ fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, /* Check for =, which puts the text value of the expression in expr_text. */ if (**str == '=') { + if (c->c_feature_version < 8) { + ast_error(c, n, + "f-string: self documenting expressions are " + "only supported in Python 3.8 and greater"); + goto error; + } *str += 1; /* Skip over ASCII whitespace. No need to test for end of string