mirror of
https://github.com/python/cpython.git
synced 2025-08-25 19:24:42 +00:00
gh-108113: Make it possible to create an optimized AST (#108154)
This commit is contained in:
parent
47022a079e
commit
10a91d7e98
10 changed files with 124 additions and 14 deletions
|
@ -32,13 +32,15 @@ from enum import IntEnum, auto, _simple_enum
|
|||
|
||||
|
||||
def parse(source, filename='<unknown>', mode='exec', *,
|
||||
type_comments=False, feature_version=None):
|
||||
type_comments=False, feature_version=None, optimize=-1):
|
||||
"""
|
||||
Parse the source into an AST node.
|
||||
Equivalent to compile(source, filename, mode, PyCF_ONLY_AST).
|
||||
Pass type_comments=True to get back type comments where the syntax allows.
|
||||
"""
|
||||
flags = PyCF_ONLY_AST
|
||||
if optimize > 0:
|
||||
flags |= PyCF_OPTIMIZED_AST
|
||||
if type_comments:
|
||||
flags |= PyCF_TYPE_COMMENTS
|
||||
if feature_version is None:
|
||||
|
@ -50,7 +52,7 @@ def parse(source, filename='<unknown>', mode='exec', *,
|
|||
feature_version = minor
|
||||
# Else it should be an int giving the minor version for 3.x.
|
||||
return compile(source, filename, mode, flags,
|
||||
_feature_version=feature_version)
|
||||
_feature_version=feature_version, optimize=optimize)
|
||||
|
||||
|
||||
def literal_eval(node_or_string):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue