Skip to content

Commit a49426a

Browse files
[3.12] gh-109120: Fix syntax error in handlinh of incorrect star expressions… (#117465)
gh-109120: Fix syntax error in handlinh of incorrect star expressions (#117444) (cherry picked from commit c97d3af)
1 parent 663e7bc commit a49426a

File tree

4 files changed

+1385
-1182
lines changed

4 files changed

+1385
-1182
lines changed

Grammar/python.gram

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,7 @@ kwargs[asdl_seq*]:
10081008
starred_expression[expr_ty]:
10091009
| invalid_starred_expression
10101010
| '*' a=expression { _PyAST_Starred(a, Load, EXTRA) }
1011+
| '*' { RAISE_SYNTAX_ERROR("Invalid star expression") }
10111012

10121013
kwarg_or_starred[KeywordOrStarred*]:
10131014
| invalid_kwarg
@@ -1128,8 +1129,8 @@ func_type_comment[Token*]:
11281129

11291130
# From here on, there are rules for invalid syntax with specialised error messages
11301131
invalid_arguments:
1131-
| ((','.(starred_expression | ( assignment_expression | expression !':=') !'=')+ ',' kwargs) | kwargs) ',' b='*' {
1132-
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(b, "iterable argument unpacking follows keyword argument unpacking") }
1132+
| ((','.(starred_expression | ( assignment_expression | expression !':=') !'=')+ ',' kwargs) | kwargs) a=',' ','.(starred_expression !'=')+ {
1133+
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "iterable argument unpacking follows keyword argument unpacking") }
11331134
| a=expression b=for_if_clauses ',' [args | expression for_if_clauses] {
11341135
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, _PyPegen_get_last_comprehension_item(PyPegen_last_item(b, comprehension_ty)), "Generator expression must be parenthesized") }
11351136
| a=NAME b='=' expression for_if_clauses {
@@ -1387,6 +1388,7 @@ invalid_kvpair:
13871388
| expression a=':' &('}'|',') {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
13881389
invalid_starred_expression:
13891390
| a='*' expression '=' b=expression { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to iterable argument unpacking") }
1391+
13901392
invalid_replacement_field:
13911393
| '{' a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '='") }
13921394
| '{' 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
@@ -1816,22 +1816,22 @@
18161816
>>> A[*(1:2)]
18171817
Traceback (most recent call last):
18181818
...
1819-
SyntaxError: invalid syntax
1819+
SyntaxError: Invalid star expression
18201820
>>> A[*(1:2)] = 1
18211821
Traceback (most recent call last):
18221822
...
1823-
SyntaxError: invalid syntax
1823+
SyntaxError: Invalid star expression
18241824
>>> del A[*(1:2)]
18251825
Traceback (most recent call last):
18261826
...
1827-
SyntaxError: invalid syntax
1827+
SyntaxError: Invalid star expression
18281828
18291829
A[*:] and A[:*]
18301830
18311831
>>> A[*:]
18321832
Traceback (most recent call last):
18331833
...
1834-
SyntaxError: invalid syntax
1834+
SyntaxError: Invalid star expression
18351835
>>> A[:*]
18361836
Traceback (most recent call last):
18371837
...
@@ -1842,7 +1842,7 @@
18421842
>>> A[*]
18431843
Traceback (most recent call last):
18441844
...
1845-
SyntaxError: invalid syntax
1845+
SyntaxError: Invalid star expression
18461846
18471847
A[**]
18481848
@@ -1986,11 +1986,23 @@ def f(x: *b)
19861986
19871987
>>> f(**x, *)
19881988
Traceback (most recent call last):
1989-
SyntaxError: iterable argument unpacking follows keyword argument unpacking
1989+
SyntaxError: Invalid star expression
19901990
19911991
>>> f(x, *:)
19921992
Traceback (most recent call last):
1993-
SyntaxError: invalid syntax
1993+
SyntaxError: Invalid star expression
1994+
1995+
>>> f(x, *)
1996+
Traceback (most recent call last):
1997+
SyntaxError: Invalid star expression
1998+
1999+
>>> f(x = 5, *)
2000+
Traceback (most recent call last):
2001+
SyntaxError: Invalid star expression
2002+
2003+
>>> f(x = 5, *:)
2004+
Traceback (most recent call last):
2005+
SyntaxError: Invalid star expression
19942006
"""
19952007

19962008
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)