]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-94949: Disallow parsing parenthesised ctx mgr with old feature_version (#94950)
authorShantanu <12621235+hauntsaninja@users.noreply.github.com>
Mon, 18 Jul 2022 21:10:49 +0000 (14:10 -0700)
committerGitHub <noreply@github.com>
Mon, 18 Jul 2022 21:10:49 +0000 (22:10 +0100)
* 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>
Grammar/python.gram
Lib/test/test_ast.py
Misc/NEWS.d/next/Core and Builtins/2022-07-18-05-10-29.gh-issue-94949.OsZ7_s.rst [new file with mode: 0644]
Parser/parser.c

index 6a8bb942fffcc51e1ed7c6162b2819b59a54f2cc..6a81e14decbde00943967468e148cf80afaf14c1 100644 (file)
@@ -391,7 +391,7 @@ for_stmt[stmt_ty]:
 with_stmt[stmt_ty]:
     | invalid_with_stmt_indent
     | 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
-        _PyAST_With(a, b, NULL, EXTRA) }
+        CHECK_VERSION(stmt_ty, 9, "Parenthesized context managers are", _PyAST_With(a, b, NULL, EXTRA)) }
     | 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
         _PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
     | ASYNC 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
index 5d7e4419560b81c20819d970b7eed76e0a1279d3..480089aa8af4482b84eedb2b7e7c8979978f0de3 100644 (file)
@@ -738,6 +738,14 @@ class AST_Tests(unittest.TestCase):
         expressions[0] = f"expr = {ast.expr.__subclasses__()[0].__doc__}"
         self.assertCountEqual(ast.expr.__doc__.split("\n"), expressions)
 
+    def test_parenthesized_with_feature_version(self):
+        ast.parse('with (CtxManager() as example): ...', feature_version=(3, 10))
+        # While advertised as a feature in Python 3.10, this was allowed starting 3.9
+        ast.parse('with (CtxManager() as example): ...', feature_version=(3, 9))
+        with self.assertRaises(SyntaxError):
+            ast.parse('with (CtxManager() as example): ...', feature_version=(3, 8))
+        ast.parse('with CtxManager() as example: ...', feature_version=(3, 8))
+
     def test_issue40614_feature_version(self):
         ast.parse('f"{x=}"', feature_version=(3, 8))
         with self.assertRaises(SyntaxError):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-07-18-05-10-29.gh-issue-94949.OsZ7_s.rst b/Misc/NEWS.d/next/Core and Builtins/2022-07-18-05-10-29.gh-issue-94949.OsZ7_s.rst
new file mode 100644 (file)
index 0000000..bc452d4
--- /dev/null
@@ -0,0 +1 @@
+:func:`ast.parse` will no longer parse parenthesized context managers when passed ``feature_version`` less than ``(3, 9)``. Patch by Shantanu Jain.
index 309c249141d71e6425985c73fde944786e6c532e..f4082aba56a2726e2312d709b1d583476f796c95 100644 (file)
@@ -6515,7 +6515,7 @@ with_stmt_rule(Parser *p)
             UNUSED(_end_lineno); // Only used by EXTRA macro
             int _end_col_offset = _token->end_col_offset;
             UNUSED(_end_col_offset); // Only used by EXTRA macro
-            _res = _PyAST_With ( a , b , NULL , EXTRA );
+            _res = CHECK_VERSION ( stmt_ty , 9 , "Parenthesized context managers are" , _PyAST_With ( a , b , NULL , EXTRA ) );
             if (_res == NULL && PyErr_Occurred()) {
                 p->error_indicator = 1;
                 p->level--;