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`
This commit is contained in:
sobolevn 2025-05-03 10:33:14 +03:00 committed by GitHub
parent 345fdce1d0
commit ca0a96dfaa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 372 additions and 222 deletions

View file

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

View file

@ -692,6 +692,63 @@ class AST_Tests(unittest.TestCase):
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:

519
Parser/parser.c generated

File diff suppressed because it is too large Load diff