Skip to content

Commit 9f49590

Browse files
authored
bpo-40903: Handle multiple '=' in invalid assignment rules in the PEG parser (GH-20697)
Automerge-Triggered-By: @pablogsal
1 parent 843c277 commit 9f49590

5 files changed

Lines changed: 453 additions & 237 deletions

File tree

Grammar/python.gram

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ assignment[stmt_ty]:
9292
| a=('(' b=single_target ')' { b }
9393
| single_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] {
9494
CHECK_VERSION(6, "Variable annotations syntax is", _Py_AnnAssign(a, b, c, 0, EXTRA)) }
95-
| a=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) tc=[TYPE_COMMENT] {
95+
| a=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) !'=' tc=[TYPE_COMMENT] {
9696
_Py_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
9797
| a=single_target b=augassign c=(yield_expr | star_expressions) {
9898
_Py_AugAssign(a, b->kind, c, EXTRA) }
@@ -646,10 +646,11 @@ invalid_assignment:
646646
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
647647
| a=expression ':' expression ['=' annotated_rhs] {
648648
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") }
649-
| a=star_expressions '=' (yield_expr | star_expressions) {
649+
| (star_targets '=')* a=star_expressions '=' {
650650
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
651651
_PyPegen_get_invalid_target(a),
652652
"cannot assign to %s", _PyPegen_get_expr_name(_PyPegen_get_invalid_target(a))) }
653+
| (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") }
653654
| a=star_expressions augassign (yield_expr | star_expressions) {
654655
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
655656
a,

Lib/test/test_syntax.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
Traceback (most recent call last):
6464
SyntaxError: cannot assign to function call
6565
66+
>>> yield = 1
67+
Traceback (most recent call last):
68+
SyntaxError: assignment to yield expression not possible
69+
6670
>>> del f()
6771
Traceback (most recent call last):
6872
SyntaxError: cannot delete function call
@@ -136,6 +140,18 @@
136140
Traceback (most recent call last):
137141
SyntaxError: cannot assign to conditional expression
138142
143+
>>> True = True = 3
144+
Traceback (most recent call last):
145+
SyntaxError: cannot assign to True
146+
147+
>>> x = y = True = z = 3
148+
Traceback (most recent call last):
149+
SyntaxError: cannot assign to True
150+
151+
>>> x = y = yield = 1
152+
Traceback (most recent call last):
153+
SyntaxError: assignment to yield expression not possible
154+
139155
>>> a, b += 1, 2
140156
Traceback (most recent call last):
141157
SyntaxError: 'tuple' is an illegal expression for augmented assignment
@@ -148,14 +164,17 @@
148164
Traceback (most recent call last):
149165
SyntaxError: 'list' is an illegal expression for augmented assignment
150166
167+
>>> p = p =
168+
Traceback (most recent call last):
169+
SyntaxError: invalid syntax
170+
151171
From compiler_complex_args():
152172
153173
>>> def f(None=1):
154174
... pass
155175
Traceback (most recent call last):
156176
SyntaxError: invalid syntax
157177
158-
159178
From ast_for_arguments():
160179
161180
>>> def f(x, y=1, z):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a possible segfault in the new PEG parser when producing error messages for invalid assignments of the form :code:`p=p=`. Patch by Pablo Galindo

0 commit comments

Comments
 (0)