From fef5536dfcb717a9c38d4a7ff95ad3b7e266eb9d Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 17 Jul 2022 21:24:50 -0700 Subject: [PATCH 1/3] gh-94947: Disallow parsing walrus with feature_version < (3, 8) --- Grammar/python.gram | 4 +++- Lib/test/test_ast.py | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Grammar/python.gram b/Grammar/python.gram index 91d73a9fffd193..6a8bb942fffcc5 100644 --- a/Grammar/python.gram +++ b/Grammar/python.gram @@ -660,7 +660,9 @@ star_named_expression[expr_ty]: | named_expression 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 225892414b8c19..5d7e4419560b81 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -743,6 +743,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())) From 782f266064fe980d0e20e7803aa4fd8262a1fba5 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 17 Jul 2022 21:47:19 -0700 Subject: [PATCH 2/3] oops, commit the parser --- Parser/parser.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Parser/parser.c b/Parser/parser.c index 3d59d4a704c239..309c249141d71e 100644 --- a/Parser/parser.c +++ b/Parser/parser.c @@ -11223,7 +11223,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--; From 5adf546da72a534e7dcbf0eae51d54a1c01e134e Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 18 Jul 2022 04:48:37 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-07-18-04-48-34.gh-issue-94947.df9gUw.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-07-18-04-48-34.gh-issue-94947.df9gUw.rst 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 00000000000000..360ea67048fe22 --- /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.