This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: `compile` doesn't compile into an AST object as specified
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.10, Python 3.9
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: docs@python Nosy List: BTaskaya, docs@python, eryksun, leewz
Priority: normal Keywords:

Created on 2016-05-25 06:42 by leewz, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg266311 - (view) Author: Franklin? Lee (leewz) Date: 2016-05-25 06:42
From `compile`'s doc:
    "Compile the source into a code or AST object."

The docs don't say how to compile into an AST object with `compile`, though. As it says later:
    "If you want to parse Python code into its AST representation, see ast.parse()."

I checked 3.4-3.2, 3.0, 2.7, and 2.6. Versions before 3.4, and version 2.6, are missing the `ast.parse` line, but still have the first line.
msg266321 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2016-05-25 08:13
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.
msg266324 - (view) Author: Franklin? Lee (leewz) Date: 2016-05-25 09:11
> What you're looking for is in the 2nd paragraph of the ast docs:

Oh. I considered that, but then compile's docs say:

    The optional arguments flags and dont_inherit
    control which future statements (see PEP 236)
    affect the compilation of source.
msg381017 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2020-11-15 15:59
We've added a reference to the compiler flags into the compile(), see issue 40484 for details.
History
Date User Action Args
2022-04-11 14:58:31adminsetgithub: 71306
2020-11-15 15:59:37BTaskayasetstatus: open -> closed
versions: + Python 3.10, - Python 2.7, Python 3.5, Python 3.6, Python 3.7, Python 3.8
messages: + msg381017

resolution: duplicate
stage: resolved
2019-12-15 13:02:01cheryl.sabellalinkissue34000 superseder
2019-12-01 19:25:30BTaskayasetnosy: + BTaskaya

versions: + Python 3.7, Python 3.8, Python 3.9, - Python 3.2, Python 3.3, Python 3.4
2016-05-25 09:11:46leewzsetmessages: + msg266324
2016-05-25 08:13:01eryksunsetnosy: + eryksun
messages: + msg266321
2016-05-25 06:42:47leewzcreate