Skip to content

Commit b1f8938

Browse files
authored
pythongh-133194: Add CHECK_VERSION to new PEP758 grammar (python#133195)
1 parent 44e4c47 commit b1f8938

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

Grammar/python.gram

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,15 +442,23 @@ try_stmt[stmt_ty]:
442442
except_block[excepthandler_ty]:
443443
| invalid_except_stmt_indent
444444
| 'except' e=expressions ':' b=block {
445-
_PyAST_ExceptHandler(e, NULL, b, EXTRA) }
445+
CHECK_VERSION(
446+
excepthandler_ty,
447+
14,
448+
"except expressions without parentheses",
449+
_PyAST_ExceptHandler(e, NULL, b, EXTRA)) }
446450
| 'except' e=expression 'as' t=NAME ':' b=block {
447451
_PyAST_ExceptHandler(e, ((expr_ty) t)->v.Name.id, b, EXTRA) }
448452
| 'except' ':' b=block { _PyAST_ExceptHandler(NULL, NULL, b, EXTRA) }
449453
| invalid_except_stmt
450454
except_star_block[excepthandler_ty]:
451455
| invalid_except_star_stmt_indent
452456
| 'except' '*' e=expressions ':' b=block {
453-
_PyAST_ExceptHandler(e, NULL, b, EXTRA) }
457+
CHECK_VERSION(
458+
excepthandler_ty,
459+
14,
460+
"except expressions without parentheses",
461+
_PyAST_ExceptHandler(e, NULL, b, EXTRA)) }
454462
| 'except' '*' e=expression 'as' t=NAME ':' b=block {
455463
_PyAST_ExceptHandler(e, ((expr_ty) t)->v.Name.id, b, EXTRA) }
456464
| invalid_except_star_stmt

Lib/test/test_ast/test_ast.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,28 @@ def test_assignment_expression_feature_version(self):
675675
with self.assertRaises(SyntaxError):
676676
ast.parse('(x := 0)', feature_version=(3, 7))
677677

678+
def test_pep758_except_without_parens(self):
679+
code = textwrap.dedent("""
680+
try:
681+
...
682+
except ValueError, TypeError:
683+
...
684+
""")
685+
ast.parse(code, feature_version=(3, 14))
686+
with self.assertRaises(SyntaxError):
687+
ast.parse(code, feature_version=(3, 13))
688+
689+
def test_pep758_except_star_without_parens(self):
690+
code = textwrap.dedent("""
691+
try:
692+
...
693+
except* ValueError, TypeError:
694+
...
695+
""")
696+
ast.parse(code, feature_version=(3, 14))
697+
with self.assertRaises(SyntaxError):
698+
ast.parse(code, feature_version=(3, 13))
699+
678700
def test_conditional_context_managers_parse_with_low_feature_version(self):
679701
# regression test for gh-115881
680702
ast.parse('with (x() if y else z()): ...', feature_version=(3, 8))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`ast.parse` will no longer parse new :pep:`758` syntax with older
2+
*feature_version* passed.

Parser/parser.c

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

0 commit comments

Comments
 (0)