Skip to content

Commit

Permalink
pythongh-123440: Improve error message for except as used with not …
Browse files Browse the repository at this point in the history
…a name
  • Loading branch information
sobolevn committed Aug 28, 2024
1 parent 61bef62 commit 29f93da
Show file tree
Hide file tree
Showing 4 changed files with 619 additions and 427 deletions.
10 changes: 10 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,13 @@ except_block[excepthandler_ty]:
_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
| invalid_except_as_expr
except_star_block[excepthandler_ty]:
| invalid_except_star_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) }
| invalid_except_stmt
| invalid_except_star_as_expr
finally_block[asdl_stmt_seq*]:
| invalid_finally_stmt
| 'finally' &&':' a=block { a }
Expand Down Expand Up @@ -1345,6 +1347,14 @@ invalid_except_stmt:
| a='except' '*'? expression ['as' NAME ] NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
| a='except' NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
| a='except' '*' (NEWLINE | ':') { RAISE_SYNTAX_ERROR("expected one or more exception types") }
invalid_except_as_expr:
| 'except' expression 'as' a=expression {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
a, "cannot use except statement with %s", _PyPegen_get_expr_name(a)) }
invalid_except_star_as_expr:
| 'except' '*' expression 'as' a=expression {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
a, "cannot use except* statement with %s", _PyPegen_get_expr_name(a)) }
invalid_finally_stmt:
| a='finally' ':' NEWLINE !INDENT {
RAISE_INDENTATION_ERROR("expected an indented block after 'finally' statement on line %d", a->lineno) }
Expand Down
30 changes: 30 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,36 @@
Traceback (most recent call last):
SyntaxError: cannot have both 'except' and 'except*' on the same 'try'
Better error message for using `except as` with not a name:
>>> try:
... pass
... except TypeError as obj.attr:
... pass
Traceback (most recent call last):
SyntaxError: cannot use except statement with attribute
>>> try:
... pass
... except TypeError as obj[1]:
... pass
Traceback (most recent call last):
SyntaxError: cannot use except statement with subscript
>>> try:
... pass
... except* TypeError as (obj, name):
... pass
Traceback (most recent call last):
SyntaxError: cannot use except* statement with tuple
>>> try:
... pass
... except* TypeError as 1:
... pass
Traceback (most recent call last):
SyntaxError: cannot use except* statement with literal
Ensure that early = are not matched by the parser as invalid comparisons
>>> f(2, 4, x=34); 1 $ 2
Traceback (most recent call last):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve :exc:`SyntaxError` message for using ``except as`` with not a name.
Loading

0 comments on commit 29f93da

Please sign in to comment.