bpo-44305: Improve syntax error for try blocks without except or finally (GH-26523)

This commit is contained in:
Pablo Galindo 2021-06-03 23:52:12 +01:00 committed by GitHub
parent 3446516ffa
commit b250f89bb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 554 additions and 440 deletions

View File

@ -274,6 +274,20 @@ have been incorporated. Some of the most notable ones:
(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:
.. code-block:: python

View File

@ -964,6 +964,7 @@ invalid_with_stmt_indent:
invalid_try_stmt:
| a='try' ':' NEWLINE !INDENT {
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:
| 'except' a=expression ',' expressions ['as' NAME ] ':' {
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "multiple exception types must be parenthesized") }

View File

@ -882,6 +882,14 @@ leading to spurious errors.
Traceback (most recent call last):
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
>>> f(2, 4, x=34); 1 $ 2
Traceback (most recent call last):

View File

@ -0,0 +1,2 @@
Improve error message for ``try`` blocks without ``except`` or ``finally``
blocks. Patch by Pablo Galindo.

File diff suppressed because it is too large Load Diff