]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-95066: ast: Replace assert with ValueError (GH-95072)
authorShantanu <12621235+hauntsaninja@users.noreply.github.com>
Tue, 26 Jul 2022 09:43:09 +0000 (02:43 -0700)
committerGitHub <noreply@github.com>
Tue, 26 Jul 2022 09:43:09 +0000 (11:43 +0200)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Lib/ast.py
Lib/test/test_ast.py
Misc/NEWS.d/next/Library/2022-07-20-22-49-48.gh-issue-95066.TuCu0E.rst [new file with mode: 0644]

index 4e2ae859245f99b76f70a3108bc1c6c39091f761..ebf4529f79b0609b8ee29ba0b0c832efa52f9d20 100644 (file)
@@ -42,7 +42,8 @@ def parse(source, filename='<unknown>', mode='exec', *,
         flags |= PyCF_TYPE_COMMENTS
     if isinstance(feature_version, tuple):
         major, minor = feature_version  # Should be a 2-tuple.
-        assert major == 3
+        if major != 3:
+            raise ValueError(f"Unsupported major version: {major}")
         feature_version = minor
     elif feature_version is None:
         feature_version = -1
index 9734218c21be33e7829fe6bcb106631eaa610abf..de34ccff2ef0539af14229b22a185578225ce739 100644 (file)
@@ -756,6 +756,12 @@ class AST_Tests(unittest.TestCase):
         with self.assertRaises(SyntaxError):
             ast.parse('(x := 0)', feature_version=(3, 7))
 
+    def test_invalid_major_feature_version(self):
+        with self.assertRaises(ValueError):
+            ast.parse('pass', feature_version=(2, 7))
+        with self.assertRaises(ValueError):
+            ast.parse('pass', feature_version=(4, 0))
+
     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/Library/2022-07-20-22-49-48.gh-issue-95066.TuCu0E.rst b/Misc/NEWS.d/next/Library/2022-07-20-22-49-48.gh-issue-95066.TuCu0E.rst
new file mode 100644 (file)
index 0000000..05ae4a6
--- /dev/null
@@ -0,0 +1 @@
+Replaced assert with exception in :func:`ast.parse`, when ``feature_version`` has an invalid major version. Patch by Shantanu Jain.