Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 206cbda

Browse files
authored
bpo-43149: Improve error message for exception group without parentheses (GH-24467)
1 parent 0ec57e2 commit 206cbda

4 files changed

Lines changed: 611 additions & 352 deletions

File tree

Grammar/python.gram

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,10 @@ try_stmt[stmt_ty]:
201201
| 'try' &&':' b=block f=finally_block { _Py_Try(b, NULL, NULL, f, EXTRA) }
202202
| 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_block+ el=[else_block] f=[finally_block] { _Py_Try(b, ex, el, f, EXTRA) }
203203
except_block[excepthandler_ty]:
204-
| 'except' e=expression t=['as' z=NAME { z }] &&':' b=block {
204+
| 'except' e=expression t=['as' z=NAME { z }] ':' b=block {
205205
_Py_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
206-
| 'except' &&':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) }
206+
| 'except' ':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) }
207+
| invalid_except_block
207208
finally_block[asdl_stmt_seq*]: 'finally' ':' a=block { a }
208209

209210
return_stmt[stmt_ty]:
@@ -737,3 +738,9 @@ invalid_import_from_targets:
737738
invalid_with_stmt:
738739
| [ASYNC] 'with' ','.(expression ['as' star_target])+ &&':'
739740
| [ASYNC] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' &&':'
741+
742+
invalid_except_block:
743+
| 'except' a=expression ',' expressions ['as' NAME ] ':' {
744+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "exception group must be parenthesized") }
745+
| 'except' expression ['as' NAME ] &&':'
746+
| 'except' &&':'

Lib/test/test_syntax.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,39 @@
835835
...
836836
SyntaxError: invalid syntax
837837
838+
Check that an exception group with missing parentheses
839+
raise a custom exception
840+
841+
>>> try:
842+
... pass
843+
... except A, B:
844+
... pass
845+
Traceback (most recent call last):
846+
SyntaxError: exception group must be parenthesized
847+
848+
>>> try:
849+
... pass
850+
... except A, B, C:
851+
... pass
852+
Traceback (most recent call last):
853+
SyntaxError: exception group must be parenthesized
854+
855+
>>> try:
856+
... pass
857+
... except A, B, C as blech:
858+
... pass
859+
Traceback (most recent call last):
860+
SyntaxError: exception group must be parenthesized
861+
862+
>>> try:
863+
... pass
864+
... except A, B, C as blech:
865+
... pass
866+
... finally:
867+
... pass
868+
Traceback (most recent call last):
869+
SyntaxError: exception group must be parenthesized
870+
838871
839872
>>> f(a=23, a=234)
840873
Traceback (most recent call last):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve the error message in the parser for exception groups without
2+
parentheses. Patch by Pablo Galindo.

0 commit comments

Comments
 (0)