mirror of
https://github.com/python/cpython.git
synced 2025-07-27 21:24:32 +00:00
[3.10] bpo-44305: Improve syntax error for try blocks without except or finally (GH-26523) (GH-26524)
(cherry picked from commit b250f89bb7
)
Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
This commit is contained in:
parent
3283bf4519
commit
e53f72a1b4
5 changed files with 554 additions and 440 deletions
|
@ -274,6 +274,20 @@ have been incorporated. Some of the most notable ones:
|
||||||
|
|
||||||
(Contributed by Pablo Galindo in :issue:`43823`)
|
(Contributed by Pablo Galindo in :issue:`43823`)
|
||||||
|
|
||||||
|
* ``try`` blocks without ``except`` or ``finally`` blocks:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
>>> try
|
||||||
|
... x = 2
|
||||||
|
... something = 3
|
||||||
|
File "<stdin>", line 3
|
||||||
|
something = 3
|
||||||
|
^^^^^^^^^
|
||||||
|
SyntaxError: expected 'except' or 'finally' block
|
||||||
|
|
||||||
|
(Contributed by Pablo Galindo in :issue:`44305`)
|
||||||
|
|
||||||
* Usage of ``=`` instead of ``==`` in comparisons:
|
* Usage of ``=`` instead of ``==`` in comparisons:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
|
@ -964,6 +964,7 @@ invalid_with_stmt_indent:
|
||||||
invalid_try_stmt:
|
invalid_try_stmt:
|
||||||
| a='try' ':' NEWLINE !INDENT {
|
| a='try' ':' NEWLINE !INDENT {
|
||||||
RAISE_INDENTATION_ERROR("expected an indented block after 'try' statement on line %d", a->lineno) }
|
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") }
|
||||||
invalid_except_stmt:
|
invalid_except_stmt:
|
||||||
| 'except' a=expression ',' expressions ['as' NAME ] ':' {
|
| 'except' a=expression ',' expressions ['as' NAME ] ':' {
|
||||||
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "multiple exception types must be parenthesized") }
|
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "multiple exception types must be parenthesized") }
|
||||||
|
|
|
@ -882,6 +882,14 @@ leading to spurious errors.
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
|
SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
|
||||||
|
|
||||||
|
Custom error messages for try blocks that are not followed by except/finally
|
||||||
|
|
||||||
|
>>> try:
|
||||||
|
... x = 34
|
||||||
|
...
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: expected 'except' or 'finally' block
|
||||||
|
|
||||||
Ensure that early = are not matched by the parser as invalid comparisons
|
Ensure that early = are not matched by the parser as invalid comparisons
|
||||||
>>> f(2, 4, x=34); 1 $ 2
|
>>> f(2, 4, x=34); 1 $ 2
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Improve error message for ``try`` blocks without ``except`` or ``finally``
|
||||||
|
blocks. Patch by Pablo Galindo.
|
969
Parser/parser.c
969
Parser/parser.c
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue