Skip to content

Commit 0daba82

Browse files
gh-94949: Disallow parsing parenthesised ctx mgr with old feature_version (#94950)
* gh-94949: Disallow parsing parenthesised ctx manager with old feature_version * 📜🤖 Added by blurb_it. * Allow it with feature_version=(3, 9) as well Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 73eab9f commit 0daba82

File tree

4 files changed

+11
-2
lines changed

4 files changed

+11
-2
lines changed

Grammar/python.gram

+1-1
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-
_PyAST_With(a, b, NULL, EXTRA) }
394+
CHECK_VERSION(stmt_ty, 9, "Parenthesized context managers are", _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

+8
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,14 @@ def test_ast_asdl_signature(self):
738738
expressions[0] = f"expr = {ast.expr.__subclasses__()[0].__doc__}"
739739
self.assertCountEqual(ast.expr.__doc__.split("\n"), expressions)
740740

741+
def test_parenthesized_with_feature_version(self):
742+
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 10))
743+
# While advertised as a feature in Python 3.10, this was allowed starting 3.9
744+
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 9))
745+
with self.assertRaises(SyntaxError):
746+
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 8))
747+
ast.parse('with CtxManager() as example: ...', feature_version=(3, 8))
748+
741749
def test_issue40614_feature_version(self):
742750
ast.parse('f"{x=}"', feature_version=(3, 8))
743751
with self.assertRaises(SyntaxError):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:func:`ast.parse` will no longer parse parenthesized context managers when passed ``feature_version`` less than ``(3, 9)``. Patch by Shantanu Jain.

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)