]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-115881: Ensure `ast.parse()` parses conditional context managers even with low...
authorAlex Waygood <Alex.Waygood@Gmail.com>
Mon, 26 Feb 2024 09:22:09 +0000 (09:22 +0000)
committerGitHub <noreply@github.com>
Mon, 26 Feb 2024 09:22:09 +0000 (09:22 +0000)
Grammar/python.gram
Lib/test/test_ast.py
Lib/test/test_type_comments.py
Misc/NEWS.d/next/Library/2024-02-25-19-20-05.gh-issue-115881.ro_Kuw.rst [new file with mode: 0644]
Parser/parser.c

index 174b4dbb6f78427ea5fcc70bd8caf7c99076eecb..797c195a0a91ba7f1b92d30bf0aa435002e34ea4 100644 (file)
@@ -393,7 +393,7 @@ for_stmt[stmt_ty]:
 with_stmt[stmt_ty]:
     | invalid_with_stmt_indent
     | 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' tc=[TYPE_COMMENT] b=block {
-       CHECK_VERSION(stmt_ty, 9, "Parenthesized context managers are", _PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
+       _PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), 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 3789ac22e3899cf84204e748fb16f85e86551985..d49c149da42592a5ced585b3cf4d8d96f4e94d8c 100644 (file)
@@ -1045,19 +1045,15 @@ class AST_Tests(unittest.TestCase):
         with self.assertRaises(SyntaxError):
             ast.parse('lambda x=1, /: ...', feature_version=(3, 7))
 
-    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_assignment_expression_feature_version(self):
         ast.parse('(x := 0)', feature_version=(3, 8))
         with self.assertRaises(SyntaxError):
             ast.parse('(x := 0)', feature_version=(3, 7))
 
+    def test_conditional_context_managers_parse_with_low_feature_version(self):
+        # regression test for gh-115881
+        ast.parse('with (x() if y else z()): ...', feature_version=(3, 8))
+
     def test_exception_groups_feature_version(self):
         code = dedent('''
         try: ...
index 5a911da56f8f8a14f63f02ab7893e7300aa6cb38..ee8939f62d082cd29459cdfae68d165cb0d10e46 100644 (file)
@@ -309,7 +309,7 @@ class TypeCommentTests(unittest.TestCase):
         self.assertEqual(tree.body[0].type_comment, None)
 
     def test_parenthesized_withstmt(self):
-        for tree in self.parse_all(parenthesized_withstmt, minver=9):
+        for tree in self.parse_all(parenthesized_withstmt):
             self.assertEqual(tree.body[0].type_comment, "int")
             self.assertEqual(tree.body[1].type_comment, "int")
         tree = self.classic_parse(parenthesized_withstmt)
diff --git a/Misc/NEWS.d/next/Library/2024-02-25-19-20-05.gh-issue-115881.ro_Kuw.rst b/Misc/NEWS.d/next/Library/2024-02-25-19-20-05.gh-issue-115881.ro_Kuw.rst
new file mode 100644 (file)
index 0000000..99bccb2
--- /dev/null
@@ -0,0 +1,4 @@
+Fix issue where :func:`ast.parse` would incorrectly flag conditional context\r
+managers (such as ``with (x() if y else z()): ...``) as invalid syntax if\r
+``feature_version=(3, 8)`` was passed. This reverts changes to the\r
+grammar made as part of gh-94949.\r
index 779b18e9650e9f5f7f63d1cd213cbf6a27608da2..f1170c2619745209553d806f69b25e85e66afbb7 100644 (file)
@@ -6603,7 +6603,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 = CHECK_VERSION ( stmt_ty , 9 , "Parenthesized context managers are" , _PyAST_With ( a , b , NEW_TYPE_COMMENT ( p , tc ) , EXTRA ) );
+            _res = _PyAST_With ( a , b , NEW_TYPE_COMMENT ( p , tc ) , EXTRA );
             if (_res == NULL && PyErr_Occurred()) {
                 p->error_indicator = 1;
                 p->level--;