Skip to content

Commit a30a1e7

Browse files
authored
[3.11] gh-115881: Ensure ast.parse() parses conditional context managers even with low feature_version passed (#115920) (#115960)
1 parent 35a43d4 commit a30a1e7

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

Grammar/python.gram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ for_stmt[stmt_ty]:
391391
with_stmt[stmt_ty]:
392392
| invalid_with_stmt_indent
393393
| 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
394-
CHECK_VERSION(stmt_ty, 9, "Parenthesized context managers are", _PyAST_With(a, b, NULL, EXTRA)) }
394+
_PyAST_With(a, b, NULL, EXTRA) }
395395
| 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
396396
_PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
397397
| 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
@@ -772,14 +772,6 @@ def test_positional_only_feature_version(self):
772772
with self.assertRaises(SyntaxError):
773773
ast.parse('lambda x=1, /: ...', feature_version=(3, 7))
774774

775-
def test_parenthesized_with_feature_version(self):
776-
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 10))
777-
# While advertised as a feature in Python 3.10, this was allowed starting 3.9
778-
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 9))
779-
with self.assertRaises(SyntaxError):
780-
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 8))
781-
ast.parse('with CtxManager() as example: ...', feature_version=(3, 8))
782-
783775
def test_debug_f_string_feature_version(self):
784776
ast.parse('f"{x=}"', feature_version=(3, 8))
785777
with self.assertRaises(SyntaxError):
@@ -790,6 +782,10 @@ def test_assignment_expression_feature_version(self):
790782
with self.assertRaises(SyntaxError):
791783
ast.parse('(x := 0)', feature_version=(3, 7))
792784

785+
def test_conditional_context_managers_parse_with_low_feature_version(self):
786+
# regression test for gh-115881
787+
ast.parse('with (x() if y else z()): ...', feature_version=(3, 8))
788+
793789
def test_exception_groups_feature_version(self):
794790
code = dedent('''
795791
try: ...
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)