Skip to content
Merged
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
11 changes: 9 additions & 2 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,10 @@ try_stmt[stmt_ty]:
| 'try' &&':' b=block f=finally_block { _Py_Try(b, NULL, NULL, f, EXTRA) }
| 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_block+ el=[else_block] f=[finally_block] { _Py_Try(b, ex, el, f, EXTRA) }
except_block[excepthandler_ty]:
| 'except' e=expression t=['as' z=NAME { z }] &&':' b=block {
| 'except' e=expression t=['as' z=NAME { z }] ':' b=block {
_Py_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
| 'except' &&':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) }
| 'except' ':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) }
| invalid_except_block
finally_block[asdl_stmt_seq*]: 'finally' ':' a=block { a }

return_stmt[stmt_ty]:
Expand Down Expand Up @@ -737,3 +738,9 @@ invalid_import_from_targets:
invalid_with_stmt:
| [ASYNC] 'with' ','.(expression ['as' star_target])+ &&':'
| [ASYNC] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' &&':'

invalid_except_block:
| 'except' a=expression ',' expressions ['as' NAME ] ':' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "exception group must be parenthesized") }
| 'except' expression ['as' NAME ] &&':'
| 'except' &&':'
33 changes: 33 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,39 @@
...
SyntaxError: invalid syntax

Check that an exception group with missing parentheses
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This terminology conflicts with PEP 654 exception groups

cc @iritkatriel

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out. If exceptions group pep is merged we will surely change this sentence.

On the other hand, do you have some alternative phasing for the error you think would be clearer?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isinstance calls this thing a classinfo

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I find that even more confusing :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exception sequence?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"multiple exception types"?

Copy link
Contributor

@graingert graingert May 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iritkatriel yeah I like it: SyntaxError: multiple exception types must be parenthesized

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #25996

raise a custom exception

>>> try:
... pass
... except A, B:
... pass
Traceback (most recent call last):
SyntaxError: exception group must be parenthesized

>>> try:
... pass
... except A, B, C:
... pass
Traceback (most recent call last):
SyntaxError: exception group must be parenthesized

>>> try:
... pass
... except A, B, C as blech:
... pass
Traceback (most recent call last):
SyntaxError: exception group must be parenthesized

>>> try:
... pass
... except A, B, C as blech:
... pass
... finally:
... pass
Traceback (most recent call last):
SyntaxError: exception group must be parenthesized


>>> f(a=23, a=234)
Traceback (most recent call last):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve the error message in the parser for exception groups without
parentheses. Patch by Pablo Galindo.
Loading