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.

Author terry.reedy
Recipients docs@python, georg.brandl, terry.reedy
Date 2011-01-24.20:07:57
SpamBayes Score 1.1108516e-06
Marked as misclassified No
Message-id <1295899678.27.0.449949701251.issue11000@psf.upfronthosting.co.za>
In-reply-to
Content
"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.
History
Date User Action Args
2011-01-24 20:07:58terry.reedysetrecipients: + terry.reedy, georg.brandl, docs@python
2011-01-24 20:07:58terry.reedysetmessageid: <1295899678.27.0.449949701251.issue11000@psf.upfronthosting.co.za>
2011-01-24 20:07:57terry.reedylinkissue11000 messages
2011-01-24 20:07:57terry.reedycreate