]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.10] gh-94947: Disallow parsing walrus with feature_version < (3, 8) (GH-94948...
authorShantanu <12621235+hauntsaninja@users.noreply.github.com>
Mon, 18 Jul 2022 19:43:23 +0000 (12:43 -0700)
committerGitHub <noreply@github.com>
Mon, 18 Jul 2022 19:43:23 +0000 (20:43 +0100)
* gh-94947: Disallow parsing walrus with feature_version < (3, 8)

* oops, commit the parser

* ðŸ“œðŸ¤– Added by blurb_it.

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>.
(cherry picked from commit ae0be5a53bb4caee3de4888341addd9c94133f2d)

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Grammar/python.gram
Lib/test/test_ast.py
Misc/NEWS.d/next/Core and Builtins/2022-07-18-04-48-34.gh-issue-94947.df9gUw.rst [new file with mode: 0644]
Parser/parser.c

index 0dff08cdb35bff3e9f0c22d4e6bcebb2a3a084f8..783f23f058e7b50dfcfde7cd4c7d7d64348e506d 100644 (file)
@@ -515,7 +515,9 @@ star_named_expression[expr_ty]:
 
 
 assignment_expression[expr_ty]:
-    | a=NAME ':=' ~ b=expression { _PyAST_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
+    | a=NAME ':=' ~ b=expression {
+        CHECK_VERSION(expr_ty, 8, "Assignment expressions are",
+        _PyAST_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA)) }
 
 named_expression[expr_ty]:
     | assignment_expression
index 95af9e2aa690c4f8f574d0cc2c81257fc89b4e6e..9dcc860a6bc413bb53ea15ee9d9a19abfde6dac7 100644 (file)
@@ -691,6 +691,11 @@ class AST_Tests(unittest.TestCase):
         with self.assertRaises(SyntaxError):
             ast.parse('f"{x=}"', feature_version=(3, 7))
 
+    def test_assignment_expression_feature_version(self):
+        ast.parse('(x := 0)', feature_version=(3, 8))
+        with self.assertRaises(SyntaxError):
+            ast.parse('(x := 0)', feature_version=(3, 7))
+
     def test_constant_as_name(self):
         for constant in "True", "False", "None":
             expr = ast.Expression(ast.Name(constant, ast.Load()))
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-07-18-04-48-34.gh-issue-94947.df9gUw.rst b/Misc/NEWS.d/next/Core and Builtins/2022-07-18-04-48-34.gh-issue-94947.df9gUw.rst
new file mode 100644 (file)
index 0000000..360ea67
--- /dev/null
@@ -0,0 +1 @@
+:func:`ast.parse` will no longer parse assignment expressions when passed ``feature_version`` less than ``(3, 8)``. Patch by Shantanu Jain.
index 670e58a255ab4c0caded149b9494da3ee8ac59e9..7116af45255f0fbaf97c1b38567daa5c0d25abd1 100644 (file)
@@ -10582,7 +10582,7 @@ assignment_expression_rule(Parser *p)
             UNUSED(_end_lineno); // Only used by EXTRA macro
             int _end_col_offset = _token->end_col_offset;
             UNUSED(_end_col_offset); // Only used by EXTRA macro
-            _res = _PyAST_NamedExpr ( CHECK ( expr_ty , _PyPegen_set_expr_context ( p , a , Store ) ) , b , EXTRA );
+            _res = CHECK_VERSION ( expr_ty , 8 , "Assignment expressions are" , _PyAST_NamedExpr ( CHECK ( expr_ty , _PyPegen_set_expr_context ( p , a , Store ) ) , b , EXTRA ) );
             if (_res == NULL && PyErr_Occurred()) {
                 p->error_indicator = 1;
                 p->level--;