Message241527
There is! compile() will do it, though the docstring doesn't mention it. (The full documentation does.)
The following function meets both my use cases:
def eval_ast_expr(node, symbols=None, *, filename='-'):
"""
Takes an ast.Expr node. Compiles and evaluates it.
Returns the result of the expression.
symbols represents the globals dict the expression
should see. (There's no equivalent for "locals" here.)
"""
if not isinstance(node, ast.Expr):
raise RuntimeError(
"eval_ast_expr: node must be of type ast.Expr")
if symbols == None:
symbols = globals()
a = ast.Expression(node.value)
co = compile(a, filename, 'eval')
fn = types.FunctionType(co, symbols)
return fn()
I am therefore closing this bug. It still seems like a nice-to-have but I'll let somebody else do it when *they* have a use case. |
|
Date |
User |
Action |
Args |
2015-04-19 18:17:36 | larry | set | recipients:
+ larry, georg.brandl, benjamin.peterson, berker.peksag, zach.ware, serhiy.storchaka |
2015-04-19 18:17:35 | larry | set | messageid: <1429467455.99.0.671827982242.issue24002@psf.upfronthosting.co.za> |
2015-04-19 18:17:35 | larry | link | issue24002 messages |
2015-04-19 18:17:35 | larry | create | |
|