Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-133357: Fix SyntaxError carret location on invalid starred exprs #133455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,10 @@ invalid_kvpair:
invalid_starred_expression_unpacking:
| a='*' expression '=' b=expression { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to iterable argument unpacking") }
invalid_starred_expression:
| '*' { RAISE_SYNTAX_ERROR("Invalid star expression") }
| a='*' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
a,
"invalid star expression") }

invalid_fstring_replacement_field:
| '{' a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '='") }
Expand Down
28 changes: 18 additions & 10 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2320,22 +2320,22 @@
>>> A[*(1:2)]
Traceback (most recent call last):
...
SyntaxError: Invalid star expression
SyntaxError: invalid star expression
>>> A[*(1:2)] = 1
Traceback (most recent call last):
...
SyntaxError: Invalid star expression
SyntaxError: invalid star expression
>>> del A[*(1:2)]
Traceback (most recent call last):
...
SyntaxError: Invalid star expression
SyntaxError: invalid star expression

A[*:] and A[:*]

>>> A[*:]
Traceback (most recent call last):
...
SyntaxError: Invalid star expression
SyntaxError: invalid star expression
>>> A[:*]
Traceback (most recent call last):
...
Expand All @@ -2346,7 +2346,7 @@
>>> A[*]
Traceback (most recent call last):
...
SyntaxError: Invalid star expression
SyntaxError: invalid star expression

A[**]

Expand Down Expand Up @@ -2623,23 +2623,23 @@ def f(x: *b)

>>> f(**x, *)
Traceback (most recent call last):
SyntaxError: Invalid star expression
SyntaxError: invalid star expression

>>> f(x, *:)
Traceback (most recent call last):
SyntaxError: Invalid star expression
SyntaxError: invalid star expression

>>> f(x, *)
Traceback (most recent call last):
SyntaxError: Invalid star expression
SyntaxError: invalid star expression

>>> f(x = 5, *)
Traceback (most recent call last):
SyntaxError: Invalid star expression
SyntaxError: invalid star expression

>>> f(x = 5, *:)
Traceback (most recent call last):
SyntaxError: Invalid star expression
SyntaxError: invalid star expression
"""

import re
Expand Down Expand Up @@ -3146,6 +3146,14 @@ def test_multiline_compiler_error_points_to_the_end(self):
lineno=3
)

def test_carret_location_invalid_star_expr(self):
with self.assertRaises(SyntaxError) as cm:
compile("A[*]", "<string>", "exec")
e = cm.exception
self.assertEqual(e.msg, "invalid star expression")
self.assertEqual(e.text.strip(), "A[*]")
self.assertEqual(e.offset, 3)

@support.cpython_only
def test_syntax_error_on_deeply_nested_blocks(self):
# This raises a SyntaxError, it used to raise a SystemError. Context
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the location of the :exc:`SyntaxError` carret on invalid starred
expressions.
6 changes: 3 additions & 3 deletions Parser/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading