Skip to content

Commit dda9198

Browse files
authored
[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 ae0be5a) Co-authored-by: Shantanu <[email protected]>
1 parent 95ae29d commit dda9198

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

Grammar/python.gram

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,9 @@ star_named_expression[expr_ty]:
515515

516516

517517
assignment_expression[expr_ty]:
518-
| a=NAME ':=' ~ b=expression { _PyAST_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
518+
| a=NAME ':=' ~ b=expression {
519+
CHECK_VERSION(expr_ty, 8, "Assignment expressions are",
520+
_PyAST_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA)) }
519521

520522
named_expression[expr_ty]:
521523
| assignment_expression

Lib/test/test_ast.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,11 @@ def test_issue40614_feature_version(self):
691691
with self.assertRaises(SyntaxError):
692692
ast.parse('f"{x=}"', feature_version=(3, 7))
693693

694+
def test_assignment_expression_feature_version(self):
695+
ast.parse('(x := 0)', feature_version=(3, 8))
696+
with self.assertRaises(SyntaxError):
697+
ast.parse('(x := 0)', feature_version=(3, 7))
698+
694699
def test_constant_as_name(self):
695700
for constant in "True", "False", "None":
696701
expr = ast.Expression(ast.Name(constant, ast.Load()))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:func:`ast.parse` will no longer parse assignment expressions when passed ``feature_version`` less than ``(3, 8)``. Patch by Shantanu Jain.

Parser/parser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10582,7 +10582,7 @@ assignment_expression_rule(Parser *p)
1058210582
UNUSED(_end_lineno); // Only used by EXTRA macro
1058310583
int _end_col_offset = _token->end_col_offset;
1058410584
UNUSED(_end_col_offset); // Only used by EXTRA macro
10585-
_res = _PyAST_NamedExpr ( CHECK ( expr_ty , _PyPegen_set_expr_context ( p , a , Store ) ) , b , EXTRA );
10585+
_res = CHECK_VERSION ( expr_ty , 8 , "Assignment expressions are" , _PyAST_NamedExpr ( CHECK ( expr_ty , _PyPegen_set_expr_context ( p , a , Store ) ) , b , EXTRA ) );
1058610586
if (_res == NULL && PyErr_Occurred()) {
1058710587
p->error_indicator = 1;
1058810588
p->level--;

0 commit comments

Comments
 (0)