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 larry
Recipients benjamin.peterson, berker.peksag, georg.brandl, larry, serhiy.storchaka, zach.ware
Date 2015-04-19.18:17:35
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1429467455.99.0.671827982242.issue24002@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2015-04-19 18:17:36larrysetrecipients: + larry, georg.brandl, benjamin.peterson, berker.peksag, zach.ware, serhiy.storchaka
2015-04-19 18:17:35larrysetmessageid: <1429467455.99.0.671827982242.issue24002@psf.upfronthosting.co.za>
2015-04-19 18:17:35larrylinkissue24002 messages
2015-04-19 18:17:35larrycreate