Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 53b84e7

Browse files
authored
[3.12] gh-115881: Ensure ast.parse() parses conditional context managers even with low feature_version passed (#115920) (#115959)
1 parent 1932da0 commit 53b84e7

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

Grammar/python.gram

+1-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ for_stmt[stmt_ty]:
392392
with_stmt[stmt_ty]:
393393
| invalid_with_stmt_indent
394394
| 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
395-
CHECK_VERSION(stmt_ty, 9, "Parenthesized context managers are", _PyAST_With(a, b, NULL, EXTRA)) }
395+
_PyAST_With(a, b, NULL, EXTRA) }
396396
| 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
397397
_PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
398398
| ASYNC 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {

Lib/test/test_ast.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -1007,19 +1007,15 @@ def test_positional_only_feature_version(self):
10071007
with self.assertRaises(SyntaxError):
10081008
ast.parse('lambda x=1, /: ...', feature_version=(3, 7))
10091009

1010-
def test_parenthesized_with_feature_version(self):
1011-
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 10))
1012-
# While advertised as a feature in Python 3.10, this was allowed starting 3.9
1013-
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 9))
1014-
with self.assertRaises(SyntaxError):
1015-
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 8))
1016-
ast.parse('with CtxManager() as example: ...', feature_version=(3, 8))
1017-
10181010
def test_assignment_expression_feature_version(self):
10191011
ast.parse('(x := 0)', feature_version=(3, 8))
10201012
with self.assertRaises(SyntaxError):
10211013
ast.parse('(x := 0)', feature_version=(3, 7))
10221014

1015+
def test_conditional_context_managers_parse_with_low_feature_version(self):
1016+
# regression test for gh-115881
1017+
ast.parse('with (x() if y else z()): ...', feature_version=(3, 8))
1018+
10231019
def test_exception_groups_feature_version(self):
10241020
code = dedent('''
10251021
try: ...
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

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)