[Python-checkins] [3.10] gh-94947: Disallow parsing walrus with feature_version < (3, 8) (GH-94948) (#94969)

pablogsal webhook-mailer at python.org
Mon Jul 18 15:43:46 EDT 2022


https://github.com/python/cpython/commit/dda9198021b3c34a4882c8524f720b2f83a56ea1
commit: dda9198021b3c34a4882c8524f720b2f83a56ea1
branch: 3.10
author: Shantanu <12621235+hauntsaninja at users.noreply.github.com>
committer: pablogsal <Pablogsal at gmail.com>
date: 2022-07-18T20:43:23+01:00
summary:

[3.10] gh-94947: Disallow parsing walrus with feature_version < (3, 8) (GH-94948) (#94969)

* 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 at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2022-07-18-04-48-34.gh-issue-94947.df9gUw.rst
M Grammar/python.gram
M Lib/test/test_ast.py
M Parser/parser.c

diff --git a/Grammar/python.gram b/Grammar/python.gram
index 0dff08cdb35bf..783f23f058e7b 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -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
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index 95af9e2aa690c..9dcc860a6bc41 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -691,6 +691,11 @@ def test_issue40614_feature_version(self):
         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
index 0000000000000..360ea67048fe2
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-07-18-04-48-34.gh-issue-94947.df9gUw.rst	
@@ -0,0 +1 @@
+:func:`ast.parse` will no longer parse assignment expressions when passed ``feature_version`` less than ``(3, 8)``. Patch by Shantanu Jain.
diff --git a/Parser/parser.c b/Parser/parser.c
index 670e58a255ab4..7116af45255f0 100644
--- a/Parser/parser.c
+++ b/Parser/parser.c
@@ -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--;



More information about the Python-checkins mailing list