Message126952
"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. |
|
Date |
User |
Action |
Args |
2011-01-24 20:07:58 | terry.reedy | set | recipients:
+ terry.reedy, georg.brandl, docs@python |
2011-01-24 20:07:58 | terry.reedy | set | messageid: <1295899678.27.0.449949701251.issue11000@psf.upfronthosting.co.za> |
2011-01-24 20:07:57 | terry.reedy | link | issue11000 messages |
2011-01-24 20:07:57 | terry.reedy | create | |
|