Skip to content

bpo-45834: Move runtime 'except:' check to the parser #29625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,12 @@ with_item[withitem_ty]:
try_stmt[stmt_ty]:
| invalid_try_stmt
| 'try' &&':' b=block f=finally_block { _PyAST_Try(b, NULL, NULL, f, EXTRA) }
| 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_block+ el=[else_block] f=[finally_block] { _PyAST_Try(b, ex, el, f, EXTRA) }
| 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_block+ d_ex[asdl_match_case_seq*]=[default_except_block] el=[else_block] f=[finally_block] {
_PyAST_Try(b, (d_ex) ? (asdl_excepthandler_seq*)_PyPegen_seq_append_to_end(p, (asdl_seq*)ex, d_ex) : ex, el, f, EXTRA)
}
| 'try' &&':' b=block d_ex[asdl_match_case_seq*]=default_except_block el=[else_block] f=[finally_block] {
_PyAST_Try(b, (asdl_excepthandler_seq*)_PyPegen_singleton_seq(p, d_ex), el, f, EXTRA)
}

# Except statement
# ----------------
Expand All @@ -411,8 +416,9 @@ except_block[excepthandler_ty]:
| invalid_except_stmt_indent
| 'except' e=expression t=['as' z=NAME { z }] ':' b=block {
_PyAST_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
| 'except' ':' b=block { _PyAST_ExceptHandler(NULL, NULL, b, EXTRA) }
| invalid_except_stmt
default_except_block[excepthandler_ty]:
| 'except' ':' b=block { _PyAST_ExceptHandler(NULL, NULL, b, EXTRA) }
finally_block[asdl_stmt_seq*]:
| invalid_finally_stmt
| 'finally' &&':' a=block { a }
Expand Down Expand Up @@ -1187,6 +1193,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") }
| 'try' ':' block except_block* a=default_except_block (except_block|default_except_block)+ {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "default 'except:' must be last") }

invalid_except_stmt:
| 'except' a=expression ',' expressions ['as' NAME ] ':' {
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "multiple exception types must be parenthesized") }
Expand Down
43 changes: 43 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,49 @@
Traceback (most recent call last):
IndentationError: expected an indented block after 'finally' statement on line 5

>>> try:
... something()
... except:
... pass
... except A:
... pass
... else:
... pass
... finally:
... pass
Traceback (most recent call last):
SyntaxError: default 'except:' must be last

>>> try:
... something()
... except:
... pass
... except:
... pass
... else:
... pass
... finally:
... pass
Traceback (most recent call last):
SyntaxError: default 'except:' must be last

>>> try:
... something()
... except A:
... pass
... except B:
... pass
... except:
... pass
... except C:
... pass
... else:
... pass
... finally:
... pass
Traceback (most recent call last):
SyntaxError: default 'except:' must be last

>>> with A:
... pass
Traceback (most recent call last):
Expand Down
Loading