Skip to content

Commit 9a608ac

Browse files
authored
[3.9] bpo-40631: Disallow single parenthesized star target (GH-24027) (GH-24068)
(cherry picked from commit 2ea320d) Automerge-Triggered-By: GH:pablogsal
1 parent 39a7578 commit 9a608ac

File tree

4 files changed

+823
-544
lines changed

4 files changed

+823
-544
lines changed

Grammar/python.gram

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -563,18 +563,23 @@ star_targets[expr_ty]:
563563
| a=star_target !',' { a }
564564
| a=star_target b=(',' c=star_target { c })* [','] {
565565
_Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
566-
star_targets_seq[asdl_seq*]: a=','.star_target+ [','] { a }
566+
star_targets_list_seq[asdl_seq*]: a=','.star_target+ [','] { a }
567+
star_targets_tuple_seq[asdl_seq*]:
568+
| a=star_target b=(',' c=star_target { c })+ [','] { _PyPegen_seq_insert_in_front(p, a, b) }
569+
| a=star_target ',' { _PyPegen_singleton_seq(p, a) }
567570
star_target[expr_ty] (memo):
568571
| '*' a=(!'*' star_target) {
569572
_Py_Starred(CHECK(_PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
573+
| target_with_star_atom
574+
target_with_star_atom[expr_ty] (memo):
570575
| a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
571576
| a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
572577
| star_atom
573578
star_atom[expr_ty]:
574579
| a=NAME { _PyPegen_set_expr_context(p, a, Store) }
575-
| '(' a=star_target ')' { _PyPegen_set_expr_context(p, a, Store) }
576-
| '(' a=[star_targets_seq] ')' { _Py_Tuple(a, Store, EXTRA) }
577-
| '[' a=[star_targets_seq] ']' { _Py_List(a, Store, EXTRA) }
580+
| '(' a=target_with_star_atom ')' { _PyPegen_set_expr_context(p, a, Store) }
581+
| '(' a=[star_targets_tuple_seq] ')' { _Py_Tuple(a, Store, EXTRA) }
582+
| '[' a=[star_targets_list_seq] ']' { _Py_List(a, Store, EXTRA) }
578583

579584
single_target[expr_ty]:
580585
| single_subscript_attribute_target

Lib/test/test_unpack_ex.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,31 @@
346346
...
347347
SyntaxError: can't use starred expression here
348348
349+
>>> (*x),y = 1, 2 # doctest:+ELLIPSIS
350+
Traceback (most recent call last):
351+
...
352+
SyntaxError: can't use starred expression here
353+
354+
>>> (((*x))),y = 1, 2 # doctest:+ELLIPSIS
355+
Traceback (most recent call last):
356+
...
357+
SyntaxError: can't use starred expression here
358+
359+
>>> z,(*x),y = 1, 2, 4 # doctest:+ELLIPSIS
360+
Traceback (most recent call last):
361+
...
362+
SyntaxError: can't use starred expression here
363+
364+
>>> z,(*x) = 1, 2 # doctest:+ELLIPSIS
365+
Traceback (most recent call last):
366+
...
367+
SyntaxError: can't use starred expression here
368+
369+
>>> ((*x),y) = 1, 2 # doctest:+ELLIPSIS
370+
Traceback (most recent call last):
371+
...
372+
SyntaxError: can't use starred expression here
373+
349374
Some size constraints (all fail.)
350375
351376
>>> s = ", ".join("a%d" % i for i in range(1<<8)) + ", *rest = range(1<<8 + 1)"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix regression where a single parenthesized starred expression was a valid
2+
assignment target.

0 commit comments

Comments
 (0)