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
parent
commit
e53f72a1b4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      Doc/whatsnew/3.10.rst
  2. 1
      Grammar/python.gram
  3. 8
      Lib/test/test_syntax.py
  4. 2
      Misc/NEWS.d/next/Core and Builtins/2021-06-03-22-51-50.bpo-44305.66dVDG.rst
  5. 627
      Parser/parser.c

14
Doc/whatsnew/3.10.rst

@ -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

1
Grammar/python.gram

@ -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") }

8
Lib/test/test_syntax.py

@ -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):

2
Misc/NEWS.d/next/Core and Builtins/2021-06-03-22-51-50.bpo-44305.66dVDG.rst

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

627
Parser/parser.c
File diff suppressed because it is too large
View File

Loading…
Cancel
Save