Browse Source
[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>
pull/26531/head
Pablo Galindo
4 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with
383 additions and
269 deletions
-
Doc/whatsnew/3.10.rst
-
Grammar/python.gram
-
Lib/test/test_syntax.py
-
Misc/NEWS.d/next/Core and Builtins/2021-06-03-22-51-50.bpo-44305.66dVDG.rst
-
Parser/parser.c
|
|
@ -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 |
|
|
|
|
|
@ -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") } |
|
|
|
|
|
@ -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): |
|
|
|
|
|
@ -0,0 +1,2 @@ |
|
|
|
Improve error message for ``try`` blocks without ``except`` or ``finally`` |
|
|
|
blocks. Patch by Pablo Galindo. |