Skip to content

Commit 3bc0d2b

Browse files
[3.11] gh-109120: Fix syntax error in handlinh of incorrect star expressions… (#117464)
gh-109120: Fix syntax error in handlinh of incorrect star expressions (#117444) (cherry picked from commit c97d3af)
1 parent cd12e6c commit 3bc0d2b

File tree

4 files changed

+1204
-999
lines changed

4 files changed

+1204
-999
lines changed

Grammar/python.gram

+3-2
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,7 @@ kwargs[asdl_seq*]:
955955

956956
starred_expression[expr_ty]:
957957
| '*' a=expression { _PyAST_Starred(a, Load, EXTRA) }
958+
| '*' { RAISE_SYNTAX_ERROR("Invalid star expression") }
958959

959960
kwarg_or_starred[KeywordOrStarred*]:
960961
| invalid_kwarg
@@ -1075,8 +1076,8 @@ func_type_comment[Token*]:
10751076

10761077
# From here on, there are rules for invalid syntax with specialised error messages
10771078
invalid_arguments:
1078-
| ((','.(starred_expression | ( assignment_expression | expression !':=') !'=')+ ',' kwargs) | kwargs) ',' b='*' {
1079-
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(b, "iterable argument unpacking follows keyword argument unpacking") }
1079+
| ((','.(starred_expression | ( assignment_expression | expression !':=') !'=')+ ',' kwargs) | kwargs) a=',' ','.(starred_expression !'=')+ {
1080+
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "iterable argument unpacking follows keyword argument unpacking") }
10801081
| a=expression b=for_if_clauses ',' [args | expression for_if_clauses] {
10811082
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, _PyPegen_get_last_comprehension_item(PyPegen_last_item(b, comprehension_ty)), "Generator expression must be parenthesized") }
10821083
| a=NAME b='=' expression for_if_clauses {

Lib/test/test_syntax.py

+19-7
Original file line numberDiff line numberDiff line change
@@ -1725,22 +1725,22 @@
17251725
>>> A[*(1:2)]
17261726
Traceback (most recent call last):
17271727
...
1728-
SyntaxError: invalid syntax
1728+
SyntaxError: Invalid star expression
17291729
>>> A[*(1:2)] = 1
17301730
Traceback (most recent call last):
17311731
...
1732-
SyntaxError: invalid syntax
1732+
SyntaxError: Invalid star expression
17331733
>>> del A[*(1:2)]
17341734
Traceback (most recent call last):
17351735
...
1736-
SyntaxError: invalid syntax
1736+
SyntaxError: Invalid star expression
17371737
17381738
A[*:] and A[:*]
17391739
17401740
>>> A[*:]
17411741
Traceback (most recent call last):
17421742
...
1743-
SyntaxError: invalid syntax
1743+
SyntaxError: Invalid star expression
17441744
>>> A[:*]
17451745
Traceback (most recent call last):
17461746
...
@@ -1751,7 +1751,7 @@
17511751
>>> A[*]
17521752
Traceback (most recent call last):
17531753
...
1754-
SyntaxError: invalid syntax
1754+
SyntaxError: Invalid star expression
17551755
17561756
A[**]
17571757
@@ -1833,11 +1833,23 @@ def f(x: *b)
18331833
18341834
>>> f(**x, *)
18351835
Traceback (most recent call last):
1836-
SyntaxError: iterable argument unpacking follows keyword argument unpacking
1836+
SyntaxError: Invalid star expression
18371837
18381838
>>> f(x, *:)
18391839
Traceback (most recent call last):
1840-
SyntaxError: invalid syntax
1840+
SyntaxError: Invalid star expression
1841+
1842+
>>> f(x, *)
1843+
Traceback (most recent call last):
1844+
SyntaxError: Invalid star expression
1845+
1846+
>>> f(x = 5, *)
1847+
Traceback (most recent call last):
1848+
SyntaxError: Invalid star expression
1849+
1850+
>>> f(x = 5, *:)
1851+
Traceback (most recent call last):
1852+
SyntaxError: Invalid star expression
18411853
"""
18421854

18431855
import re
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added handle of incorrect star expressions, e.g ``f(3, *)``. Patch by
2+
Grigoryev Semyon

0 commit comments

Comments
 (0)