Catching results of an exec
Erik Max Francis
max at alcyone.com
Thu Sep 19 00:21:34 CEST 2002
Greg Fortune wrote:
> Is there any way to "catch" the value produced by an exec? I'm
> things like print statements that don't really assign a value, but do
> a result to stdout.
The problem is "the value produced by an exec" is ambiguous, because
execs (as don't return values). If you want to capture some subpart of
the execution of a statement, you'll have to handle it case by case.
> Right now I've got a nasty hack that redirects stdout, uses an
> InteractiveInterpereter from the code module to execute the code,
> writes to
> an output file, reads the result from the file, restores stdout and
> sends the result to the client. Hopefully, that's a terrible way to
> do it.
That's probably the best way to do it, actually -- or, at least, with an
overridden sys.stdout in the globals dictionary that you're passing to
eval/exec (which doesn't necessarily have to be the same dictionary as
your main script is running).
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/ \ I am the king of sorrow
EmPy / http://www.alcyone.com/pyos/empy/
A system for embedding arbitrary Python in template text as markup.
More information about the Python-list