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: Doc: ast.parse parses source, not just expressions
Type: Stage: resolved
Components: Documentation Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, georg.brandl, terry.reedy
Priority: normal Keywords: patch

Created on 2011-01-24 20:07 by terry.reedy, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
zast.diff terry.reedy, 2011-01-24 20:07 'expr' to 'source'
Messages (3)
msg126952 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-01-24 20:07
"ast.parse(expr, filename='<unknown>', mode='exec') 
Parse an expression into an AST node. Equivalent to compile(expr, filename, mode, ast.PyCF_ONLY_AST)."

but

"compile(source, ...) 
Compile the source into a code or AST object. 
...
The mode argument specifies what kind of code must be compiled; it can be 'exec' if source consists of a sequence of statements,"

and indeed, with all three versions,

>>> import ast
>>> e3=ast.parse('x=1; y=2\nif x: print(y)')
>>> o3 = compile(e3,'','exec')
>>> exec(o3)
2
>>> print(x,y)
(1, 2)

Attached patch (untested) changes doc and doc string.

Inquiry: I suspect that the filename is *not* attached to the ast. (In any case, it must be explicitly supplied to a subsequent compile() call.) If so, it is a useless parameter and the api and doc should be

parse(source, mode) ... Equivalent to compile(source, '<unknown>', ...

If so, and failing such a change, should the doc warn people to not bother supplying a filename arg?

I also wonder whether the mode arg has any effect on the ast. If not, same question.
msg126953 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2011-01-24 20:09
Looks fine.
msg126964 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-01-24 22:29
r88172, r88173, r88175
History
Date User Action Args
2022-04-11 14:57:11adminsetgithub: 55209
2011-01-24 22:29:04terry.reedysetstatus: open -> closed

messages: + msg126964
resolution: fixed
stage: resolved
2011-01-24 20:09:30georg.brandlsetmessages: + msg126953
2011-01-24 20:07:57terry.reedycreate