Catching results of an exec
Greg Fortune
lists at gregfortune.com
Fri Sep 20 02:12:33 EDT 2002
Erik Max Francis wrote:
> Greg Fortune wrote:
>
>> Is there any way to "catch" the value produced by an exec? I'm
>> considering
>> things like print statements that don't really assign a value, but do
>> send
>> 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
>> then
>> 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).
>
Thanks, I hadn't thought to use the namespace stuff on exec. That'll clean
up a couple more lines..
Greg
More information about the Python-list
mailing list