gh-134036: Improve error messages for invalid raise
statements (#134077)
This commit is contained in:
parent
a7d41e8aab
commit
0d9ccc87a2
@ -184,6 +184,7 @@ return_stmt[stmt_ty]:
|
|||||||
| 'return' a=[star_expressions] { _PyAST_Return(a, EXTRA) }
|
| 'return' a=[star_expressions] { _PyAST_Return(a, EXTRA) }
|
||||||
|
|
||||||
raise_stmt[stmt_ty]:
|
raise_stmt[stmt_ty]:
|
||||||
|
| invalid_raise_stmt
|
||||||
| 'raise' a=expression b=['from' z=expression { z }] { _PyAST_Raise(a, b, EXTRA) }
|
| 'raise' a=expression b=['from' z=expression { z }] { _PyAST_Raise(a, b, EXTRA) }
|
||||||
| 'raise' { _PyAST_Raise(NULL, NULL, EXTRA) }
|
| 'raise' { _PyAST_Raise(NULL, NULL, EXTRA) }
|
||||||
|
|
||||||
@ -1287,6 +1288,11 @@ invalid_ann_assign_target[expr_ty]:
|
|||||||
| list
|
| list
|
||||||
| tuple
|
| tuple
|
||||||
| '(' a=invalid_ann_assign_target ')' { a }
|
| '(' a=invalid_ann_assign_target ')' { a }
|
||||||
|
invalid_raise_stmt:
|
||||||
|
| a='raise' b='from' {
|
||||||
|
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "did you forget an expression between 'raise' and 'from'?") }
|
||||||
|
| 'raise' expression a='from' {
|
||||||
|
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "did you forget an expression after 'from'?") }
|
||||||
invalid_del_stmt:
|
invalid_del_stmt:
|
||||||
| 'del' a=star_expressions {
|
| 'del' a=star_expressions {
|
||||||
RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
|
RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
|
||||||
|
@ -1695,6 +1695,28 @@ Make sure that the old "raise X, Y[, Z]" form is gone:
|
|||||||
...
|
...
|
||||||
SyntaxError: invalid syntax
|
SyntaxError: invalid syntax
|
||||||
|
|
||||||
|
Better errors for `raise` statement:
|
||||||
|
|
||||||
|
>>> raise ValueError from
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: did you forget an expression after 'from'?
|
||||||
|
|
||||||
|
>>> raise mod.ValueError() from
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: did you forget an expression after 'from'?
|
||||||
|
|
||||||
|
>>> raise from exc
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: did you forget an expression between 'raise' and 'from'?
|
||||||
|
|
||||||
|
>>> raise from None
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: did you forget an expression between 'raise' and 'from'?
|
||||||
|
|
||||||
|
>>> raise from
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: did you forget an expression between 'raise' and 'from'?
|
||||||
|
|
||||||
Check that an multiple exception types with missing parentheses
|
Check that an multiple exception types with missing parentheses
|
||||||
raise a custom exception only when using 'as'
|
raise a custom exception only when using 'as'
|
||||||
|
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
Improve :exc:`SyntaxError` message when using invalid :keyword:`raise`
|
||||||
|
statements.
|
901
Parser/parser.c
generated
901
Parser/parser.c
generated
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user