mirror of https://github.com/python/cpython
gh-94947: Disallow parsing walrus with feature_version < (3, 8) (#94948)
* 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>
This commit is contained in:
parent
97b4121f93
commit
ae0be5a53b
|
@ -660,7 +660,9 @@ star_named_expression[expr_ty]:
|
||||||
| named_expression
|
| named_expression
|
||||||
|
|
||||||
assignment_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]:
|
named_expression[expr_ty]:
|
||||||
| assignment_expression
|
| assignment_expression
|
||||||
|
|
|
@ -743,6 +743,11 @@ class AST_Tests(unittest.TestCase):
|
||||||
with self.assertRaises(SyntaxError):
|
with self.assertRaises(SyntaxError):
|
||||||
ast.parse('f"{x=}"', feature_version=(3, 7))
|
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):
|
def test_constant_as_name(self):
|
||||||
for constant in "True", "False", "None":
|
for constant in "True", "False", "None":
|
||||||
expr = ast.Expression(ast.Name(constant, ast.Load()))
|
expr = ast.Expression(ast.Name(constant, ast.Load()))
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
:func:`ast.parse` will no longer parse assignment expressions when passed ``feature_version`` less than ``(3, 8)``. Patch by Shantanu Jain.
|
|
@ -11223,7 +11223,7 @@ assignment_expression_rule(Parser *p)
|
||||||
UNUSED(_end_lineno); // Only used by EXTRA macro
|
UNUSED(_end_lineno); // Only used by EXTRA macro
|
||||||
int _end_col_offset = _token->end_col_offset;
|
int _end_col_offset = _token->end_col_offset;
|
||||||
UNUSED(_end_col_offset); // Only used by EXTRA macro
|
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()) {
|
if (_res == NULL && PyErr_Occurred()) {
|
||||||
p->error_indicator = 1;
|
p->error_indicator = 1;
|
||||||
p->level--;
|
p->level--;
|
||||||
|
|
Loading…
Reference in New Issue