Skip to content

Commit 9c4232a

Browse files
authored
gh-99211: Point to except/except* on syntax errors when mixing them (GH-99215)
Automerge-Triggered-By: GH:lysnikolaou
1 parent b0e1f9c commit 9c4232a

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
@@ -1254,7 +1254,9 @@ invalid_try_stmt:
12541254
| a='try' ':' NEWLINE !INDENT {
12551255
RAISE_INDENTATION_ERROR("expected an indented block after 'try' statement on line %d", a->lineno) }
12561256
| 'try' ':' block !('except' | 'finally') { RAISE_SYNTAX_ERROR("expected 'except' or 'finally' block") }
1257-
| a='try' ':' block* ((except_block+ except_star_block) | (except_star_block+ except_block)) block* {
1257+
| 'try' ':' block* except_block+ a='except' b='*' expression ['as' NAME] ':' {
1258+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot have both 'except' and 'except*' on the same 'try'") }
1259+
| 'try' ':' block* except_star_block+ a='except' [expression ['as' NAME]] ':' {
12581260
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot have both 'except' and 'except*' on the same 'try'") }
12591261
invalid_except_stmt:
12601262
| 'except' '*'? a=expression ',' expressions ['as' NAME ] ':' {

Lib/test/test_syntax.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2017,12 +2017,12 @@ def test_generator_in_function_call(self):
20172017
def test_except_then_except_star(self):
20182018
self._check_error("try: pass\nexcept ValueError: pass\nexcept* TypeError: pass",
20192019
r"cannot have both 'except' and 'except\*' on the same 'try'",
2020-
lineno=1, end_lineno=1, offset=1, end_offset=4)
2020+
lineno=3, end_lineno=3, offset=1, end_offset=8)
20212021

20222022
def test_except_star_then_except(self):
20232023
self._check_error("try: pass\nexcept* ValueError: pass\nexcept TypeError: pass",
20242024
r"cannot have both 'except' and 'except\*' on the same 'try'",
2025-
lineno=1, end_lineno=1, offset=1, end_offset=4)
2025+
lineno=3, end_lineno=3, offset=1, end_offset=7)
20262026

20272027
def test_empty_line_after_linecont(self):
20282028
# See issue-40847

0 commit comments

Comments
 (0)