gh-133194: Add CHECK_VERSION to new PEP758 grammar (#133195)

This commit is contained in:
sobolevn
2025-04-30 13:39:26 +03:00
committed by GitHub
parent 44e4c479fb
commit b1f893875b
4 changed files with 36 additions and 4 deletions

View File

@@ -442,7 +442,11 @@ try_stmt[stmt_ty]:
except_block[excepthandler_ty]: except_block[excepthandler_ty]:
| invalid_except_stmt_indent | invalid_except_stmt_indent
| 'except' e=expressions ':' b=block { | 'except' e=expressions ':' b=block {
_PyAST_ExceptHandler(e, NULL, b, EXTRA) } CHECK_VERSION(
excepthandler_ty,
14,
"except expressions without parentheses",
_PyAST_ExceptHandler(e, NULL, b, EXTRA)) }
| 'except' e=expression 'as' t=NAME ':' b=block { | 'except' e=expression 'as' t=NAME ':' b=block {
_PyAST_ExceptHandler(e, ((expr_ty) t)->v.Name.id, b, EXTRA) } _PyAST_ExceptHandler(e, ((expr_ty) t)->v.Name.id, b, EXTRA) }
| 'except' ':' b=block { _PyAST_ExceptHandler(NULL, NULL, b, EXTRA) } | 'except' ':' b=block { _PyAST_ExceptHandler(NULL, NULL, b, EXTRA) }
@@ -450,7 +454,11 @@ except_block[excepthandler_ty]:
except_star_block[excepthandler_ty]: except_star_block[excepthandler_ty]:
| invalid_except_star_stmt_indent | invalid_except_star_stmt_indent
| 'except' '*' e=expressions ':' b=block { | 'except' '*' e=expressions ':' b=block {
_PyAST_ExceptHandler(e, NULL, b, EXTRA) } CHECK_VERSION(
excepthandler_ty,
14,
"except expressions without parentheses",
_PyAST_ExceptHandler(e, NULL, b, EXTRA)) }
| 'except' '*' e=expression 'as' t=NAME ':' b=block { | 'except' '*' e=expression 'as' t=NAME ':' b=block {
_PyAST_ExceptHandler(e, ((expr_ty) t)->v.Name.id, b, EXTRA) } _PyAST_ExceptHandler(e, ((expr_ty) t)->v.Name.id, b, EXTRA) }
| invalid_except_star_stmt | invalid_except_star_stmt

View File

@@ -675,6 +675,28 @@ class AST_Tests(unittest.TestCase):
with self.assertRaises(SyntaxError): with self.assertRaises(SyntaxError):
ast.parse('(x := 0)', feature_version=(3, 7)) ast.parse('(x := 0)', feature_version=(3, 7))
def test_pep758_except_without_parens(self):
code = textwrap.dedent("""
try:
...
except ValueError, TypeError:
...
""")
ast.parse(code, feature_version=(3, 14))
with self.assertRaises(SyntaxError):
ast.parse(code, feature_version=(3, 13))
def test_pep758_except_star_without_parens(self):
code = textwrap.dedent("""
try:
...
except* ValueError, TypeError:
...
""")
ast.parse(code, feature_version=(3, 14))
with self.assertRaises(SyntaxError):
ast.parse(code, feature_version=(3, 13))
def test_conditional_context_managers_parse_with_low_feature_version(self): def test_conditional_context_managers_parse_with_low_feature_version(self):
# regression test for gh-115881 # regression test for gh-115881
ast.parse('with (x() if y else z()): ...', feature_version=(3, 8)) ast.parse('with (x() if y else z()): ...', feature_version=(3, 8))

View File

@@ -0,0 +1,2 @@
:func:`ast.parse` will no longer parse new :pep:`758` syntax with older
*feature_version* passed.

4
Parser/parser.c generated
View File

@@ -7140,7 +7140,7 @@ except_block_rule(Parser *p)
UNUSED(_end_lineno); // Only used by EXTRA macro UNUSED(_end_lineno); // Only used by EXTRA macro
int _end_col_offset = _token->end_col_offset; int _end_col_offset = _token->end_col_offset;
UNUSED(_end_col_offset); // Only used by EXTRA macro UNUSED(_end_col_offset); // Only used by EXTRA macro
_res = _PyAST_ExceptHandler ( e , NULL , b , EXTRA ); _res = CHECK_VERSION ( excepthandler_ty , 14 , "except expressions without parentheses" , _PyAST_ExceptHandler ( e , NULL , b , EXTRA ) );
if (_res == NULL && PyErr_Occurred()) { if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1; p->error_indicator = 1;
p->level--; p->level--;
@@ -7342,7 +7342,7 @@ except_star_block_rule(Parser *p)
UNUSED(_end_lineno); // Only used by EXTRA macro UNUSED(_end_lineno); // Only used by EXTRA macro
int _end_col_offset = _token->end_col_offset; int _end_col_offset = _token->end_col_offset;
UNUSED(_end_col_offset); // Only used by EXTRA macro UNUSED(_end_col_offset); // Only used by EXTRA macro
_res = _PyAST_ExceptHandler ( e , NULL , b , EXTRA ); _res = CHECK_VERSION ( excepthandler_ty , 14 , "except expressions without parentheses" , _PyAST_ExceptHandler ( e , NULL , b , EXTRA ) );
if (_res == NULL && PyErr_Occurred()) { if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1; p->error_indicator = 1;
p->level--; p->level--;