[docs] [issue27119] `compile` doesn't compile into an AST object as specified

Eryk Sun report at bugs.python.org
Wed May 25 04:13:01 EDT 2016


Eryk Sun added the comment:

What you're looking for is in the 2nd paragraph of the ast docs:

    An abstract syntax tree can be generated by passing
    ast.PyCF_ONLY_AST as a flag to the compile() built-in
    function, or using the parse() helper provided in this
    module. The result will be a tree of objects whose
    classes all inherit from ast.AST. An abstract syntax
    tree can be compiled into a Python code object using
    the built-in compile() function.

For example:

    >>> mod = compile('42', '', 'exec', ast.PyCF_ONLY_AST)
    >>> mod
    <_ast.Module object at 0x7f0e45b15be0
    >>> ast.dump(mod)
    'Module(body=[Expr(value=Num(n=42))])'

In the discussion of `flags`, I think the compile docs should explicitly list ast.PyCF_ONLY_AST and the CO_FUTURE_* flags in a table.

----------
nosy: +eryksun

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue27119>
_______________________________________


More information about the docs mailing list