8000 Ensure that early = are not matched by the parser as invalid comparis… · python/cpython@d9151cb · GitHub
[go: up one dir, main page]

Skip to content

Commit d9151cb

Browse files
authored
Ensure that early = are not matched by the parser as invalid comparisons (GH-25375)
1 parent c1a66bd commit d9151cb

File tree

3 files changed

+423
-319
lines changed

3 files changed

+423
-319
lines changed

Grammar/python.gram

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,10 @@ named_expression[expr_ty]:
447447
| invalid_named_expression
448448
| expression !':='
449449

450+
direct_named_expression[expr_ty]:
451+
| a=NAME ':=' ~ b=expression { _PyAST_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
452+
| expression !':='
453+
450454
annotated_rhs[expr_ty]: yield_expr | star_expressions
451455

452456
expressions[expr_ty]:
@@ -671,7 +675,7 @@ arguments[expr_ty] (memo):
671675
| a=args [','] &')' { a }
672676
| invalid_arguments
673677
args[expr_ty]:
674-
| a[asdl_expr_seq*]=','.(starred_expression | named_expression !'=')+ b=[',' k=kwargs {k}] { _PyPegen_collect_call_seqs(p, a, b, EXTRA) }
678+
| a[asdl_expr_seq*]=','.(starred_expression | direct_named_expression !'=')+ b=[',' k=kwargs {k}] { _PyPegen_collect_call_seqs(p, a, b, EXTRA) }
675679
| a=kwargs { _PyAST_Call(_PyPegen_dummy_name(p),
676680
CHECK_NULL_ALLOWED(asdl_expr_seq*, _PyPegen_seq_extract_starred_exprs(p, a)),
677681
CHECK_NULL_ALLOWED(asdl_keyword_seq*, _PyPegen_seq_delete_starred_exprs(p, a)),

Lib/test/test_syntax.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,11 @@
863863
Traceback (most recent call last):
864864
SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
865865
866+
Ensure that early = are not matched by the parser as invalid comparisons
867+
>>> f(2, 4, x=34); {1,2 a}
868+
Traceback (most recent call last):
869+
SyntaxError: invalid syntax
870+
866871
Make sure that the old "raise X, Y[, Z]" form is gone:
867872
>>> raise X, Y
868873
Traceback (most recent call last):

0 commit comments

Comments
 (0)
0