Skip to content

Commit

Permalink
gh-99211: Point to except/except* on syntax errors when mixing them (G…
Browse files Browse the repository at this point in the history
…H-99215)

Automerge-Triggered-By: GH:lysnikolaou
  • Loading branch information
lysnikolaou authored Nov 20, 2022
1 parent b0e1f9c commit 9c4232a
Show file tree
Hide file tree
Showing 3 changed files with 718 additions and 674 deletions.
4 changes: 3 additions & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,9 @@ invalid_try_stmt:
| a='try' ':' NEWLINE !INDENT {
RAISE_INDENTATION_ERROR("expected an indented block after 'try' statement on line %d", a->lineno) }
| 'try' ':' block !('except' | 'finally') { RAISE_SYNTAX_ERROR("expected 'except' or 'finally' block") }
| a='try' ':' block* ((except_block+ except_star_block) | (except_star_block+ except_block)) block* {
| 'try' ':' block* except_block+ a='except' b='*' expression ['as' NAME] ':' {
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot have both 'except' and 'except*' on the same 'try'") }
| 'try' ':' block* except_star_block+ a='except' [expression ['as' NAME]] ':' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot have both 'except' and 'except*' on the same 'try'") }
invalid_except_stmt:
| 'except' '*'? a=expression ',' expressions ['as' NAME ] ':' {
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2017,12 +2017,12 @@ def test_generator_in_function_call(self):
def test_except_then_except_star(self):
self._check_error("try: pass\nexcept ValueError: pass\nexcept* TypeError: pass",
r"cannot have both 'except' and 'except\*' on the same 'try'",
lineno=1, end_lineno=1, offset=1, end_offset=4)
lineno=3, end_lineno=3, offset=1, end_offset=8)

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

def test_empty_line_after_linecont(self):
# See issue-40847
Expand Down
Loading

0 comments on commit 9c4232a

Please sign in to comment.