ast.parse: check feature_version common case first (GH-94640)

This commit is contained in:
Anthony Sottile 2022-08-29 10:05:24 -04:00 committed by GitHub
parent 026ab6f4e5
commit 9c2b9348e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -40,13 +40,13 @@ def parse(source, filename='<unknown>', mode='exec', *,
flags = PyCF_ONLY_AST
if type_comments:
flags |= PyCF_TYPE_COMMENTS
if isinstance(feature_version, tuple):
if feature_version is None:
feature_version = -1
elif isinstance(feature_version, tuple):
major, minor = feature_version # Should be a 2-tuple.
if major != 3:
raise ValueError(f"Unsupported major version: {major}")
feature_version = minor
elif feature_version is None:
feature_version = -1
# Else it should be an int giving the minor version for 3.x.
return compile(source, filename, mode, flags,
_feature_version=feature_version)