From b09028dce4b7a31832d9743b57f1b345ee93e246 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Wed, 20 Jul 2022 15:45:06 -0700 Subject: [PATCH 1/3] gh-95066: ast: Replace AssertionError with ValueError --- Lib/ast.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/ast.py b/Lib/ast.py index 4e2ae859245f99..ebf4529f79b060 100644 --- a/Lib/ast.py +++ b/Lib/ast.py @@ -42,7 +42,8 @@ def parse(source, filename='', 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 From a5f67b21ef31a26bb0c7d009713547c55704086a Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Wed, 20 Jul 2022 15:48:12 -0700 Subject: [PATCH 2/3] add test --- Lib/test/test_ast.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index 480089aa8af448..16f92a7c84d2a2 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -756,6 +756,12 @@ def test_assignment_expression_feature_version(self): 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())) From 87975eb6069794156fb4a95ded088a635476bc8f Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 20 Jul 2022 22:49:50 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-07-20-22-49-48.gh-issue-95066.TuCu0E.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-07-20-22-49-48.gh-issue-95066.TuCu0E.rst 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 index 00000000000000..05ae4a6a2761af --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-07-20-22-49-48.gh-issue-95066.TuCu0E.rst @@ -0,0 +1 @@ +Replaced assert with exception in :func:`ast.parse`, when ``feature_version`` has an invalid major version. Patch by Shantanu Jain.