Skip to content

Commit 152a437

Browse files
authored
[3.11] gh-99211: Point to except/except* on syntax errors when mixing them (GH-99215) (GH-99622)
gh-99211: Point to except/except* on syntax errors when mixing them (GH-99215) (cherry picked from commit 9c4232a)
1 parent 56a517e commit 152a437

File tree

3 files changed

+718
-674
lines changed

3 files changed

+718
-674
lines changed

Grammar/python.gram

+3-1
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,9 @@ 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-
| a='try' ':' block* ((except_block+ except_star_block) | (except_star_block+ except_block)) block* {
1250+
| 'try' ':' block* except_block+ a='except' b='*' expression ['as' NAME] ':' {
1251+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot have both 'except' and 'except*' on the same 'try'") }
1252+
| 'try' ':' block* except_star_block+ a='except' [expression ['as' NAME]] ':' {
12511253
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot have both 'except' and 'except*' on the same 'try'") }
12521254
invalid_except_stmt:
12531255
| 'except' '*'? a=expression ',' expressions ['as' NAME ] ':' {

Lib/test/test_syntax.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1988,12 +1988,12 @@ def test_generator_in_function_call(self):
19881988
def test_except_then_except_star(self):
19891989
self._check_error("try: pass\nexcept ValueError: pass\nexcept* TypeError: pass",
19901990
r"cannot have both 'except' and 'except\*' on the same 'try'",
1991-
lineno=1, end_lineno=1, offset=1, end_offset=4)
1991+
lineno=3, end_lineno=3, offset=1, end_offset=8)
19921992

19931993
def test_except_star_then_except(self):
19941994
self._check_error("try: pass\nexcept* ValueError: pass\nexcept TypeError: pass",
19951995
r"cannot have both 'except' and 'except\*' on the same 'try'",
1996-
lineno=1, end_lineno=1, offset=1, end_offset=4)
1996+
lineno=3, end_lineno=3, offset=1, end_offset=7)
19971997

19981998
def test_empty_line_after_linecont(self):
19991999
# See issue-40847

0 commit comments

Comments
 (0)