Skip to content

Commit d9c08c5

Browse files
[3.14] gh-133999: Fix except parsing regression in 3.14 (GH-134035) (#134206)
gh-133999: Fix `except` parsing regression in 3.14 (GH-134035) (cherry picked from commit 84914ad) Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent 9136ccf commit d9c08c5

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

Grammar/python.gram

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,15 +1418,15 @@ invalid_except_stmt:
14181418
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "multiple exception types must be parenthesized when using 'as'") }
14191419
| a='except' expression ['as' NAME ] NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
14201420
| a='except' NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1421-
| 'except' expression 'as' a=expression {
1421+
| 'except' expression 'as' a=expression ':' block {
14221422
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
14231423
a, "cannot use except statement with %s", _PyPegen_get_expr_name(a)) }
14241424
invalid_except_star_stmt:
14251425
| 'except' '*' a=expression ',' expressions 'as' NAME ':' {
14261426
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "multiple exception types must be parenthesized when using 'as'") }
14271427
| a='except' '*' expression ['as' NAME ] NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
14281428
| a='except' '*' (NEWLINE | ':') { RAISE_SYNTAX_ERROR("expected one or more exception types") }
1429-
| 'except' '*' expression 'as' a=expression {
1429+
| 'except' '*' expression 'as' a=expression ':' block {
14301430
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
14311431
a, "cannot use except* statement with %s", _PyPegen_get_expr_name(a)) }
14321432
invalid_finally_stmt:

Lib/test/test_syntax.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,23 @@
14311431
Traceback (most recent call last):
14321432
SyntaxError: cannot use except* statement with literal
14331433

1434+
Regression tests for gh-133999:
1435+
1436+
>>> try: pass
1437+
... except TypeError as name: raise from None
1438+
Traceback (most recent call last):
1439+
SyntaxError: invalid syntax
1440+
1441+
>>> try: pass
1442+
... except* TypeError as name: raise from None
1443+
Traceback (most recent call last):
1444+
SyntaxError: invalid syntax
1445+
1446+
>>> match 1:
1447+
... case 1 | 2 as abc: raise from None
1448+
Traceback (most recent call last):
1449+
SyntaxError: invalid syntax
1450+
14341451
Ensure that early = are not matched by the parser as invalid comparisons
14351452
>>> f(2, 4, x=34); 1 $ 2
14361453
Traceback (most recent call last):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :exc:`SyntaxError` regression in :keyword:`except` parsing after
2+
:gh:`123440`.

Parser/parser.c

Lines changed: 22 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy