[Python-checkins] [3.10] gh-94949: Disallow parsing parenthesised ctx mgr with old feature_version (GH-94950) (#94990)

pablogsal webhook-mailer at python.org
Tue Jul 19 02:25:07 EDT 2022


https://github.com/python/cpython/commit/906b345dddb4cc5f72a11a10c4e3f1f7712919c0
commit: 906b345dddb4cc5f72a11a10c4e3f1f7712919c0
branch: 3.10
author: Shantanu <12621235+hauntsaninja at users.noreply.github.com>
committer: pablogsal <Pablogsal at gmail.com>
date: 2022-07-19T07:24:48+01:00
summary:

[3.10] gh-94949: Disallow parsing parenthesised ctx mgr with old feature_version (GH-94950) (#94990)

(cherry picked from commit 0daba822212cd5d6c63384a27f390f0945330c2b)

Co-authored-by: Shantanu <12621235+hauntsaninja at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2022-07-18-05-10-29.gh-issue-94949.OsZ7_s.rst
M Grammar/python.gram
M Lib/test/test_ast.py
M Parser/parser.c

diff --git a/Grammar/python.gram b/Grammar/python.gram
index 783f23f058e7b..7570748c3f9b7 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -192,7 +192,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 {
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index 9dcc860a6bc41..eb861096f65cb 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -686,6 +686,14 @@ def test_ast_asdl_signature(self):
         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
index 0000000000000..bc452d434da0f
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-07-18-05-10-29.gh-issue-94949.OsZ7_s.rst	
@@ -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.
diff --git a/Parser/parser.c b/Parser/parser.c
index 7116af45255f0..c0080e80b5654 100644
--- a/Parser/parser.c
+++ b/Parser/parser.c
@@ -4657,7 +4657,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--;



More information about the Python-checkins mailing list