Skip to content

Commit

Permalink
gh-95066: ast: Replace assert with ValueError (GH-95072)
Browse files Browse the repository at this point in the history
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
  • Loading branch information
hauntsaninja and blurb-it[bot] committed Jul 26, 2022
1 parent 0d35a59 commit a5dde0f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Lib/ast.py
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_ast.py
Expand Up @@ -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()))
Expand Down
@@ -0,0 +1 @@
Replaced assert with exception in :func:`ast.parse`, when ``feature_version`` has an invalid major version. Patch by Shantanu Jain.

0 comments on commit a5dde0f

Please sign in to comment.