[New-bugs-announce] [issue4118] Built-in compile() and ast module doc issues

Terry J. Reedy report at bugs.python.org
Tue Oct 14 01:17:35 CEST 2008

New submission from Terry J. Reedy <tjreedy at udel.edu>:

>From docs.python.org
2.6: Compile the source into a code or AST object.
3.0: Compile the source into a code object.
Add 'or AST ' to the latter.

Both: Refer to the _ast module documentation
<there is none, docs only for the ast module, but see below>

Both: for information on how to compile into and from AST objects.

This sentence should at least have 'and from AST ' deleted. The
information on 'from' is given in the previous sentence.
Both: source can either be a string or an AST object.

The ast doc adds nothing.  "A modified abstract syntax tree can be
compiled into a Python code object using the built-in compile() function."

The remainder of the sentence could be replaced by the shorter
"To compile into an AST object, import ast and pass ast.PyCF_ONLY_AST as
a flag."

This could be followed by "See ast module documentation for more."

In the ast doc, both say: "An abstract syntax tree can be generated by
passing _ast.PyCF_ONLY_AST ..."

That should be ast, not _ast; the existence of the shadow C version is a
CPython implementation detail.

In the parse() doc lines, 'PyCF_ONLY_AST' should, I think, have the
'ast' prefix as the former will not work unless one does 'from ast
import PyCF_ONLY_AST' (or '... import *').

Back to compile():
2.6 (and before): When compiling a string with multi-line statements...
3.0 <Caveats missing>

The first, about '\n' versus '\r\n' still applies.
print (compile("def f():\r\n  pass #haha",'','exec')) #fails
print (compile("def f():\n  pass #haha",'','exec')) # succeeds

The second, at least for 2.5.2 and 3.0c1 (and I presume for 2.6), only
applies, it seems, based on testing, if the last line consists only of a
comment.  In the second example above, putting '#haha' on a separate
line fails.

See: 'compile' built-in function failures when missing EOL

I am not sure what to suggest, but a warning that is nearly always a
false alarm confuses and lulls.

assignee: georg.brandl
components: Documentation
messages: 74712
nosy: georg.brandl, tjreedy
severity: normal
status: open
title: Built-in compile() and ast module doc issues
versions: Python 2.6, Python 2.7, Python 3.0

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list