Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -443,26 +443,30 @@ try_stmt[stmt_ty]:

except_block[excepthandler_ty]:
| invalid_except_stmt_indent
| 'except' e=expression ':' b=block {
_PyAST_ExceptHandler(e, NULL, b, EXTRA) }
| 'except' e=expression 'as' t=NAME ':' b=block {
_PyAST_ExceptHandler(e, ((expr_ty) t)->v.Name.id, b, EXTRA) }
| 'except' e=expressions ':' b=block {
CHECK_VERSION(
excepthandler_ty,
14,
"except expressions without parentheses",
_PyAST_ExceptHandler(e, NULL, b, EXTRA)) }
| 'except' e=expression 'as' t=NAME ':' b=block {
_PyAST_ExceptHandler(e, ((expr_ty) t)->v.Name.id, b, EXTRA) }
"except expressions without parentheses are",
_PyAST_ExceptHandler(e, NULL, b, EXTRA)) }
| 'except' ':' b=block { _PyAST_ExceptHandler(NULL, NULL, b, EXTRA) }
| invalid_except_stmt
except_star_block[excepthandler_ty]:
| invalid_except_star_stmt_indent
| 'except' '*' e=expression ':' b=block {
_PyAST_ExceptHandler(e, NULL, b, EXTRA) }
| 'except' '*' e=expression 'as' t=NAME ':' b=block {
_PyAST_ExceptHandler(e, ((expr_ty) t)->v.Name.id, b, EXTRA) }
| 'except' '*' e=expressions ':' b=block {
CHECK_VERSION(
excepthandler_ty,
14,
"except expressions without parentheses",
"except expressions without parentheses are",
_PyAST_ExceptHandler(e, NULL, b, EXTRA)) }
| 'except' '*' e=expression 'as' t=NAME ':' b=block {
_PyAST_ExceptHandler(e, ((expr_ty) t)->v.Name.id, b, EXTRA) }
| invalid_except_star_stmt
finally_block[asdl_stmt_seq*]:
| invalid_finally_stmt
Expand Down
57 changes: 57 additions & 0 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,63 @@ def test_pep758_except_without_parens(self):
with self.assertRaises(SyntaxError):
ast.parse(code, feature_version=(3, 13))

def test_pep758_except_with_single_expr(self):
single_expr = textwrap.dedent("""
try:
...
except{0} TypeError:
...
""")

single_expr_with_as = textwrap.dedent("""
try:
...
except{0} TypeError as exc:
...
""")

single_tuple_expr = textwrap.dedent("""
try:
...
except{0} (TypeError,):
...
""")

single_tuple_expr_with_as = textwrap.dedent("""
try:
...
except{0} (TypeError,) as exc:
...
""")

single_parens_expr = textwrap.dedent("""
try:
...
except{0} (TypeError):
...
""")

single_parens_expr_with_as = textwrap.dedent("""
try:
...
except{0} (TypeError) as exc:
...
""")

for code in [
single_expr,
single_expr_with_as,
single_tuple_expr,
single_tuple_expr_with_as,
single_parens_expr,
single_parens_expr_with_as,
]:
for star in [True, False]:
code = code.format('*' if star else '')
with self.subTest(code=code, star=star):
ast.parse(code, feature_version=(3, 14))
ast.parse(code, feature_version=(3, 13))

def test_pep758_except_star_without_parens(self):
code = textwrap.dedent("""
try:
Expand Down
Loading
Loading