Skip to content

Commit a5dde0f

Browse files
gh-95066: ast: Replace assert with ValueError (GH-95072)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 0d35a59 commit a5dde0f

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

Lib/ast.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def parse(source, filename='<unknown>', mode='exec', *,
4242
flags |= PyCF_TYPE_COMMENTS
4343
if isinstance(feature_version, tuple):
4444
major, minor = feature_version # Should be a 2-tuple.
45-
assert major == 3
45+
if major != 3:
46+
raise ValueError(f"Unsupported major version: {major}")
4647
feature_version = minor
4748
elif feature_version is None:
4849
feature_version = -1

Lib/test/test_ast.py

+6
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,12 @@ def test_assignment_expression_feature_version(self):
756756
with self.assertRaises(SyntaxError):
757757
ast.parse('(x := 0)', feature_version=(3, 7))
758758

759+
def test_invalid_major_feature_version(self):
760+
with self.assertRaises(ValueError):
761+
ast.parse('pass', feature_version=(2, 7))
762+
with self.assertRaises(ValueError):
763+
ast.parse('pass', feature_version=(4, 0))
764+
759765
def test_constant_as_name(self):
760766
for constant in "True", "False", "None":
761767
expr = ast.Expression(ast.Name(constant, ast.Load()))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replaced assert with exception in :func:`ast.parse`, when ``feature_version`` has an invalid major version. Patch by Shantanu Jain.

0 commit comments

Comments
 (0)