]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-96587: Raise `SyntaxError` for PEP654 on older `feature_version` (GH-96588...
authorNikita Sobolev <mail@sobolevn.me>
Wed, 5 Oct 2022 22:00:13 +0000 (01:00 +0300)
committerPablo Galindo <pablogsal@gmail.com>
Sat, 22 Oct 2022 19:53:46 +0000 (20:53 +0100)
(cherry picked from commit 2c7d2e8d46164efb6e27a64081d8e949f6876515)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Grammar/python.gram
Lib/test/test_ast.py
Misc/NEWS.d/next/Core and Builtins/2022-09-05-19-20-44.gh-issue-96587.bVxhX2.rst [new file with mode: 0644]
Parser/parser.c

index d4df78b679a4aca49969685c976f96275c111f3c..51f846a57f404bbabac0b7fee1ea7fe286668573 100644 (file)
@@ -412,7 +412,9 @@ try_stmt[stmt_ty]:
     | invalid_try_stmt
     | 'try' &&':' b=block f=finally_block { _PyAST_Try(b, NULL, NULL, f, EXTRA) }
     | 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_block+ el=[else_block] f=[finally_block] { _PyAST_Try(b, ex, el, f, EXTRA) }
-    | 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_star_block+ el=[else_block] f=[finally_block] { _PyAST_TryStar(b, ex, el, f, EXTRA) }
+    | 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_star_block+ el=[else_block] f=[finally_block] { 
+        CHECK_VERSION(stmt_ty, 11, "Exception groups are", 
+                      _PyAST_TryStar(b, ex, el, f, EXTRA)) }
 
 
 # Except statement
index 7275e1559e5bb1301bfbeeb16970cd4fd1e12d45..90ad6af56fdb20696196a79efd2c6c8a86dee852 100644 (file)
@@ -771,6 +771,15 @@ class AST_Tests(unittest.TestCase):
         with self.assertRaises(SyntaxError):
             ast.parse('(x := 0)', feature_version=(3, 7))
 
+    def test_exception_groups_feature_version(self):
+        code = dedent('''
+        try: ...
+        except* Exception: ...
+        ''')
+        ast.parse(code)
+        with self.assertRaises(SyntaxError):
+            ast.parse(code, feature_version=(3, 10))
+
     def test_constant_as_name(self):
         for constant in "True", "False", "None":
             expr = ast.Expression(ast.Name(constant, ast.Load()))
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-09-05-19-20-44.gh-issue-96587.bVxhX2.rst b/Misc/NEWS.d/next/Core and Builtins/2022-09-05-19-20-44.gh-issue-96587.bVxhX2.rst
new file mode 100644 (file)
index 0000000..37e9dcb
--- /dev/null
@@ -0,0 +1,2 @@
+Correctly raise ``SyntaxError`` on exception groups (:pep:`654`) on python
+versions prior to 3.11
index e13dbe04cf98069d42e97d9a72f3431ad3dc337c..3fc12e50833c566c1ef1d77a0977f7b109a44951 100644 (file)
@@ -6970,7 +6970,7 @@ try_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_TryStar ( b , ex , el , f , EXTRA );
+            _res = CHECK_VERSION ( stmt_ty , 11 , "Exception groups are" , _PyAST_TryStar ( b , ex , el , f , EXTRA ) );
             if (_res == NULL && PyErr_Occurred()) {
                 p->error_indicator = 1;
                 p->level--;