Skip to content

Commit c97d3af

Browse files
gh-109120: Fix syntax error in handlinh of incorrect star expressions (#117444)
1 parent 1d5479b commit c97d3af

File tree

4 files changed

+1442
-1239
lines changed

4 files changed

+1442
-1239
lines changed

Grammar/python.gram

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,7 @@ kwargs[asdl_seq*]:
10131013
starred_expression[expr_ty]:
10141014
| invalid_starred_expression
10151015
| '*' a=expression { _PyAST_Starred(a, Load, EXTRA) }
1016+
| '*' { RAISE_SYNTAX_ERROR("Invalid star expression") }
10161017

10171018
kwarg_or_starred[KeywordOrStarred*]:
10181019
| invalid_kwarg
@@ -1133,8 +1134,8 @@ func_type_comment[Token*]:
11331134

11341135
# From here on, there are rules for invalid syntax with specialised error messages
11351136
invalid_arguments:
1136-
| ((','.(starred_expression | ( assignment_expression | expression !':=') !'=')+ ',' kwargs) | kwargs) ',' b='*' {
1137-
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(b, "iterable argument unpacking follows keyword argument unpacking") }
1137+
| ((','.(starred_expression | ( assignment_expression | expression !':=') !'=')+ ',' kwargs) | kwargs) a=',' ','.(starred_expression !'=')+ {
1138+
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "iterable argument unpacking follows keyword argument unpacking") }
11381139
| a=expression b=for_if_clauses ',' [args | expression for_if_clauses] {
11391140
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, _PyPegen_get_last_comprehension_item(PyPegen_last_item(b, comprehension_ty)), "Generator expression must be parenthesized") }
11401141
| a=NAME b='=' expression for_if_clauses {
@@ -1396,6 +1397,7 @@ invalid_kvpair:
13961397
| expression a=':' &('}'|',') {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
13971398
invalid_starred_expression:
13981399
| a='*' expression '=' b=expression { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to iterable argument unpacking") }
1400+
13991401
invalid_replacement_field:
14001402
| '{' a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '='") }
14011403
| '{' a='!' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '!'") }

Lib/test/test_syntax.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,22 +1911,22 @@
19111911
>>> A[*(1:2)]
19121912
Traceback (most recent call last):
19131913
...
1914-
SyntaxError: invalid syntax
1914+
SyntaxError: Invalid star expression
19151915
>>> A[*(1:2)] = 1
19161916
Traceback (most recent call last):
19171917
...
1918-
SyntaxError: invalid syntax
1918+
SyntaxError: Invalid star expression
19191919
>>> del A[*(1:2)]
19201920
Traceback (most recent call last):
19211921
...
1922-
SyntaxError: invalid syntax
1922+
SyntaxError: Invalid star expression
19231923
19241924
A[*:] and A[:*]
19251925
19261926
>>> A[*:]
19271927
Traceback (most recent call last):
19281928
...
1929-
SyntaxError: invalid syntax
1929+
SyntaxError: Invalid star expression
19301930
>>> A[:*]
19311931
Traceback (most recent call last):
19321932
...
@@ -1937,7 +1937,7 @@
19371937
>>> A[*]
19381938
Traceback (most recent call last):
19391939
...
1940-
SyntaxError: invalid syntax
1940+
SyntaxError: Invalid star expression
19411941
19421942
A[**]
19431943
@@ -2081,11 +2081,23 @@ def f(x: *b)
20812081
20822082
>>> f(**x, *)
20832083
Traceback (most recent call last):
2084-
SyntaxError: iterable argument unpacking follows keyword argument unpacking
2084+
SyntaxError: Invalid star expression
20852085
20862086
>>> f(x, *:)
20872087
Traceback (most recent call last):
2088-
SyntaxError: invalid syntax
2088+
SyntaxError: Invalid star expression
2089+
2090+
>>> f(x, *)
2091+
Traceback (most recent call last):
2092+
SyntaxError: Invalid star expression
2093+
2094+
>>> f(x = 5, *)
2095+
Traceback (most recent call last):
2096+
SyntaxError: Invalid star expression
2097+
2098+
>>> f(x = 5, *:)
2099+
Traceback (most recent call last):
2100+
SyntaxError: Invalid star expression
20892101
"""
20902102

20912103
import re
Lines changed: 2 additions & 0 deletions
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)