Skip to content

Commit 7a3518e

Browse files
authored
gh-115881: Ensure ast.parse() parses conditional context managers even with low feature_version passed (#115920)
1 parent 8e8ab75 commit 7a3518e

File tree

5 files changed

+11
-11
lines changed

5 files changed

+11
-11
lines changed

Grammar/python.gram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ for_stmt[stmt_ty]:
393393
with_stmt[stmt_ty]:
394394
| invalid_with_stmt_indent
395395
| 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' tc=[TYPE_COMMENT] b=block {
396-
CHECK_VERSION(stmt_ty, 9, "Parenthesized context managers are", _PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
396+
_PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
397397
| 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
398398
_PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
399399
| 'async' 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {

Lib/test/test_ast.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,19 +1045,15 @@ def test_positional_only_feature_version(self):
10451045
with self.assertRaises(SyntaxError):
10461046
ast.parse('lambda x=1, /: ...', feature_version=(3, 7))
10471047

1048-
def test_parenthesized_with_feature_version(self):
1049-
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 10))
1050-
# While advertised as a feature in Python 3.10, this was allowed starting 3.9
1051-
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 9))
1052-
with self.assertRaises(SyntaxError):
1053-
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 8))
1054-
ast.parse('with CtxManager() as example: ...', feature_version=(3, 8))
1055-
10561048
def test_assignment_expression_feature_version(self):
10571049
ast.parse('(x := 0)', feature_version=(3, 8))
10581050
with self.assertRaises(SyntaxError):
10591051
ast.parse('(x := 0)', feature_version=(3, 7))
10601052

1053+
def test_conditional_context_managers_parse_with_low_feature_version(self):
1054+
# regression test for gh-115881
1055+
ast.parse('with (x() if y else z()): ...', feature_version=(3, 8))
1056+
10611057
def test_exception_groups_feature_version(self):
10621058
code = dedent('''
10631059
try: ...

Lib/test/test_type_comments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def test_withstmt(self):
309309
self.assertEqual(tree.body[0].type_comment, None)
310310

311311
def test_parenthesized_withstmt(self):
312-
for tree in self.parse_all(parenthesized_withstmt, minver=9):
312+
for tree in self.parse_all(parenthesized_withstmt):
313313
self.assertEqual(tree.body[0].type_comment, "int")
314314
self.assertEqual(tree.body[1].type_comment, "int")
315315
tree = self.classic_parse(parenthesized_withstmt)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix issue where :func:`ast.parse` would incorrectly flag conditional context
2+
managers (such as ``with (x() if y else z()): ...``) as invalid syntax if
3+
``feature_version=(3, 8)`` was passed. This reverts changes to the
4+
grammar made as part of gh-94949.

Parser/parser.c

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)