Skip to content

Commit 61b6c40

Browse files
authored
gh-99153: set location on SyntaxError for try with both except and except* (GH-99160)
1 parent d3b82b4 commit 61b6c40

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

Grammar/python.gram

+2-2
Original file line numberDiff line numberDiff line change
@@ -1254,8 +1254,8 @@ 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-
| 'try' ':' block* ((except_block+ except_star_block) | (except_star_block+ except_block)) block* {
1258-
RAISE_SYNTAX_ERROR("cannot have both 'except' and 'except*' on the same 'try'") }
1257+
| a='try' ':' block* ((except_block+ except_star_block) | (except_star_block+ except_block)) block* {
1258+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot have both 'except' and 'except*' on the same 'try'") }
12591259
invalid_except_stmt:
12601260
| 'except' '*'? a=expression ',' expressions ['as' NAME ] ':' {
12611261
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "multiple exception types must be parenthesized") }

Lib/test/test_syntax.py

+10
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,16 @@ def test_generator_in_function_call(self):
20142014
"Generator expression must be parenthesized",
20152015
lineno=1, end_lineno=1, offset=11, end_offset=53)
20162016

2017+
def test_except_then_except_star(self):
2018+
self._check_error("try: pass\nexcept ValueError: pass\nexcept* TypeError: pass",
2019+
r"cannot have both 'except' and 'except\*' on the same 'try'",
2020+
lineno=1, end_lineno=1, offset=1, end_offset=4)
2021+
2022+
def test_except_star_then_except(self):
2023+
self._check_error("try: pass\nexcept* ValueError: pass\nexcept TypeError: pass",
2024+
r"cannot have both 'except' and 'except\*' on the same 'try'",
2025+
lineno=1, end_lineno=1, offset=1, end_offset=4)
2026+
20172027
def test_empty_line_after_linecont(self):
20182028
# See issue-40847
20192029
s = r"""\
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

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)