]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40614: Respect feature version for f-string debug expressions (GH-20196)
authorShantanu <hauntsaninja@users.noreply.github.com>
Wed, 27 May 2020 20:30:38 +0000 (13:30 -0700)
committerGitHub <noreply@github.com>
Wed, 27 May 2020 20:30:38 +0000 (21:30 +0100)
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
Lib/test/test_ast.py
Misc/NEWS.d/next/Library/2020-05-18-22-41-02.bpo-40614.8j3kmq.rst [new file with mode: 0644]
Parser/pegen/parse_string.c
Python/ast.c

index 3e9c8b55cdff421d38c3352cac5889f1c76c373b..a3b366ec35da125e8dc5f5b9dcd4ab3283a4f86c 100644 (file)
@@ -663,6 +663,11 @@ class AST_Tests(unittest.TestCase):
         expressions[0] = f"expr = {ast.expr.__subclasses__()[0].__doc__}"
         self.assertCountEqual(ast.expr.__doc__.split("\n"), expressions)
 
+    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 (file)
index 0000000..238b98c
--- /dev/null
@@ -0,0 +1 @@
+:func:`ast.parse` will not parse self documenting expressions in f-strings when passed ``feature_version`` is less than ``(3, 8)``.
index a0ec698fa56a24d5d24f7fb40b321f6108f83ada..e24ecc58d3aa1dc3acf7f05423ad3dd1720604ff 100644 (file)
@@ -928,6 +928,11 @@ fstring_find_expr(Parser *p, const char **str, const char *end, int raw, int rec
     /* Check for =, which puts the text value of the expression in
        expr_text. */
     if (**str == '=') {
+        if (p->feature_version < 8) {
+            RAISE_SYNTAX_ERROR("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
index 2d20ca62aa83780ffc5622b3370b1cc164301dfd..c524b8e34e8731a82800c88d6b21340f37d04669 100644 (file)
@@ -5069,6 +5069,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