Skip to content

Commit ca0a96d

Browse files
authored
gh-133194: Fix regression with PEP 758 parsing on older feature_version (#133289)
gh-133192: Fix regression with PEP 758 parsing on older `feature_version`
1 parent 345fdce commit ca0a96d

File tree

3 files changed

+372
-222
lines changed

3 files changed

+372
-222
lines changed

Grammar/python.gram

+11-7
Original file line numberDiff line numberDiff line change
@@ -443,26 +443,30 @@ try_stmt[stmt_ty]:
443443

444444
except_block[excepthandler_ty]:
445445
| invalid_except_stmt_indent
446+
| 'except' e=expression ':' b=block {
447+
_PyAST_ExceptHandler(e, NULL, b, EXTRA) }
448+
| 'except' e=expression 'as' t=NAME ':' b=block {
449+
_PyAST_ExceptHandler(e, ((expr_ty) t)->v.Name.id, b, EXTRA) }
446450
| 'except' e=expressions ':' b=block {
447451
CHECK_VERSION(
448452
excepthandler_ty,
449453
14,
450-
"except expressions without parentheses",
451-
_PyAST_ExceptHandler(e, NULL, b, EXTRA)) }
452-
| 'except' e=expression 'as' t=NAME ':' b=block {
453-
_PyAST_ExceptHandler(e, ((expr_ty) t)->v.Name.id, b, EXTRA) }
454+
"except expressions without parentheses are",
455+
_PyAST_ExceptHandler(e, NULL, b, EXTRA)) }
454456
| 'except' ':' b=block { _PyAST_ExceptHandler(NULL, NULL, b, EXTRA) }
455457
| invalid_except_stmt
456458
except_star_block[excepthandler_ty]:
457459
| invalid_except_star_stmt_indent
460+
| 'except' '*' e=expression ':' b=block {
461+
_PyAST_ExceptHandler(e, NULL, b, EXTRA) }
462+
| 'except' '*' e=expression 'as' t=NAME ':' b=block {
463+
_PyAST_ExceptHandler(e, ((expr_ty) t)->v.Name.id, b, EXTRA) }
458464
| 'except' '*' e=expressions ':' b=block {
459465
CHECK_VERSION(
460466
excepthandler_ty,
461467
14,
462-
"except expressions without parentheses",
468+
"except expressions without parentheses are",
463469
_PyAST_ExceptHandler(e, NULL, b, EXTRA)) }
464-
| 'except' '*' e=expression 'as' t=NAME ':' b=block {
465-
_PyAST_ExceptHandler(e, ((expr_ty) t)->v.Name.id, b, EXTRA) }
466470
| invalid_except_star_stmt
467471
finally_block[asdl_stmt_seq*]:
468472
| invalid_finally_stmt

Lib/test/test_ast/test_ast.py

+57
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,63 @@ def test_pep758_except_without_parens(self):
692692
with self.assertRaises(SyntaxError):
693693
ast.parse(code, feature_version=(3, 13))
694694

695+
def test_pep758_except_with_single_expr(self):
696+
single_expr = textwrap.dedent("""
697+
try:
698+
...
699+
except{0} TypeError:
700+
...
701+
""")
702+
703+
single_expr_with_as = textwrap.dedent("""
704+
try:
705+
...
706+
except{0} TypeError as exc:
707+
...
708+
""")
709+
710+
single_tuple_expr = textwrap.dedent("""
711+
try:
712+
...
713+
except{0} (TypeError,):
714+
...
715+
""")
716+
717+
single_tuple_expr_with_as = textwrap.dedent("""
718+
try:
719+
...
720+
except{0} (TypeError,) as exc:
721+
...
722+
""")
723+
724+
single_parens_expr = textwrap.dedent("""
725+
try:
726+
...
727+
except{0} (TypeError):
728+
...
729+
""")
730+
731+
single_parens_expr_with_as = textwrap.dedent("""
732+
try:
733+
...
734+
except{0} (TypeError) as exc:
735+
...
736+
""")
737+
738+
for code in [
739+
single_expr,
740+
single_expr_with_as,
741+
single_tuple_expr,
742+
single_tuple_expr_with_as,
743+
single_parens_expr,
744+
single_parens_expr_with_as,
745+
]:
746+
for star in [True, False]:
747+
code = code.format('*' if star else '')
748+
with self.subTest(code=code, star=star):
749+
ast.parse(code, feature_version=(3, 14))
750+
ast.parse(code, feature_version=(3, 13))
751+
695752
def test_pep758_except_star_without_parens(self):
696753
code = textwrap.dedent("""
697754
try:

0 commit comments

Comments
 (0)