Skip to content

Commit 1559389

Browse files
isidenticalpablogsalhauntsaninja
authored
bpo-40484: Document compiler flags under AST module (GH-19885)
Co-authored-by: Pablo Galindo <[email protected]> Co-authored-by: Shantanu <[email protected]>
1 parent 3c0ac18 commit 1559389

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

Doc/library/ast.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,6 +1756,34 @@ and classes for traversing abstract syntax trees:
17561756
Added the *indent* option.
17571757

17581758

1759+
.. _ast-compiler-flags:
1760+
1761+
Compiler Flags
1762+
--------------
1763+
1764+
The following flags may be passed to :func:`compile` in order to change
1765+
effects on the compilation of a program:
1766+
1767+
.. data:: PyCF_ALLOW_TOP_LEVEL_AWAIT
1768+
1769+
Enables support for top-level ``await``, ``async for``, ``async with``
1770+
and async comprehensions.
1771+
1772+
.. versionadded:: 3.8
1773+
1774+
.. data:: PyCF_ONLY_AST
1775+
1776+
Generates and returns an abstract syntax tree instead of returning a
1777+
compiled code object.
1778+
1779+
.. data:: PyCF_TYPE_COMMENTS
1780+
1781+
Enables support for :pep:`484` and :pep:`526` style type comments
1782+
(``# type: <type>``, ``# type: ignore <stuff>``).
1783+
1784+
.. versionadded:: 3.8
1785+
1786+
17591787
.. _ast-cli:
17601788

17611789
Command-Line Usage

Doc/library/functions.rst

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -259,26 +259,24 @@ are always available. They are listed here in alphabetical order.
259259
interactive statement (in the latter case, expression statements that
260260
evaluate to something other than ``None`` will be printed).
261261

262-
The optional arguments *flags* and *dont_inherit* control which :ref:`future
263-
statements <future>` affect the compilation of *source*. If neither
264-
is present (or both are zero) the code is compiled with those future
265-
statements that are in effect in the code that is calling :func:`compile`. If the
266-
*flags* argument is given and *dont_inherit* is not (or is zero) then the
267-
future statements specified by the *flags* argument are used in addition to
268-
those that would be used anyway. If *dont_inherit* is a non-zero integer then
269-
the *flags* argument is it -- the future statements in effect around the call
270-
to compile are ignored.
271-
272-
Future statements are specified by bits which can be bitwise ORed together to
273-
specify multiple statements. The bitfield required to specify a given feature
274-
can be found as the :attr:`~__future__._Feature.compiler_flag` attribute on
275-
the :class:`~__future__._Feature` instance in the :mod:`__future__` module.
276-
277-
The optional argument *flags* also controls whether the compiled source is
278-
allowed to contain top-level ``await``, ``async for`` and ``async with``.
279-
When the bit ``ast.PyCF_ALLOW_TOP_LEVEL_AWAIT`` is set, the return code
280-
object has ``CO_COROUTINE`` set in ``co_code``, and can be interactively
281-
executed via ``await eval(code_object)``.
262+
The optional argument *flags* and *dont_inherit* controls which
263+
:ref:`compiler options <ast-compiler-flags>` should be activated
264+
and which :ref:`future features <future>` should be allowed. If neither
265+
is present (or both are zero) the code is compiled with the same flags that
266+
affect the code that is calling :func:`compile`. If the *flags*
267+
argument is given and *dont_inherit* is not (or is zero) then the compiler
268+
options and the future statements specified by the *flags* argument are used
269+
in addition to those that would be used anyway. If *dont_inherit* is a
270+
non-zero integer then the *flags* argument is it -- the flags (future
271+
features and compiler options) in the surrounding code are ignored.
272+
273+
Compiler options and future statements are specified by bits which can be
274+
bitwise ORed together to specify multiple options. The bitfield required to
275+
specify a given future feature can be found as the
276+
:attr:`~__future__._Feature.compiler_flag` attribute on the
277+
:class:`~__future__._Feature` instance in the :mod:`__future__` module.
278+
:ref:`Compiler flags <ast-compiler-flags>` can be found in :mod:`ast`
279+
module, with ``PyCF_`` prefix.
282280

283281
The argument *optimize* specifies the optimization level of the compiler; the
284282
default value of ``-1`` selects the optimization level of the interpreter as

0 commit comments

Comments
 (0)