8000 [3.11] gh-99153: set location on SyntaxError for try with both except… · python/cpython@d8a42bc · GitHub
[go: up one dir, main page]

Skip to content

Commit d8a42bc

Browse files
authored
[3.11] gh-99153: set location on SyntaxError for try with both except and except* (GH-99160) (#99168)
1 parent 263e983 commit d8a42bc

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

Grammar/python.gram

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,8 +1247,8 @@ invalid_try_stmt:
12471247
| a='try' ':' NEWLINE !INDENT {
12481248
RAISE_INDENTATION_ERROR("expected an indented block after 'try' statement on line %d", a->lineno) }
12491249
| 'try' ':' block !('except' | 'finally') { RAISE_SYNTAX_ERROR("expected 'except' or 'finally' block") }
1250-
| 'try' ':' block* ((except_block+ except_star_block) | (except_star_block+ except_block)) block* {
1251-
RAISE_SYNTAX_ERROR("cannot have both 'except' and 'except*' on the same 'try'") }
1250+
| a='try' ':' block* ((except_block+ except_star_block) | (except_star_block+ except_block)) block* {
1251+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot have both 'except' and 'except*' on the same 'try'") }
12521252
invalid_except_stmt:
12531253
| 'except' '*'? a=expression ',' expressions ['as' NAME ] ':' {
12541254
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "multiple exception types must be parenthesized") }

Lib/test/test_syntax.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,6 +1985,16 @@ def test_generator_in_function_call(self):
19851985
"Generator expression must be parenthesized",
19861986
lineno=1, end_lineno=1, offset=11, end_offset=53)
19871987

1988+
def test_except_then_except_star(self):
1989+
self._check_error("try: pass\nexcept ValueError: pass\nexcept* TypeError: pass",
1990+
r"cannot have both 'except' and 'except\*' on the same 'try'",
1991+
lineno=1, end_lineno=1, offset=1, end_offset=4)
1992+
1993+
def test_except_star_then_except(self):
1994+
self._check_error("try: pass\nexcept* ValueError: pass\nexcept TypeError: pass",
1995+
r"cannot have both 'except' and 'except\*' on the same 'try'",
1996+
lineno=1, end_lineno=1, offset=1, end_offset=4)
1997+
19881998
def test_empty_line_after_linecont(self):
19891999
# See issue-40847
19902000
s = r"""\
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix location of :exc:`SyntaxError` for a :keyword:`try` block with both :keyword:`except` and :keyword:`except* <except_star>`.

Parser/parser.c

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0