[Tutor] RExec woes
Tue, 23 Oct 2001 19:42:26 +0200
On 0, Scott Moynes <email@example.com> wrote:
> Fortunately, I have read both those documents in full, and they are
> helpful for simple things like adding variables, and changing
> permissions, but unfortunately they do not have any hints on how do
> save the results of the users' command.
> As a quick example, here's approximately what I'm trying to
> >>> r=rexec.RExec()
> >>> m=r.add_module('sys')
> >>> s=StringIO.StringIO()
> >>> m.__dict__['stdin']=s
> >>> r.r_exec('print "something"')
> >>> s.getvalue()
You have a silly error here; you change stdin, but print prints to stdout.
Unfortunately, that doesn't solve the problem :-(.
It does work if you change stdout in the normal program, and then start the
import sys, rexec, StringIO
s = StringIO.StringIO()
# try...finally makes sure that stdout is always reset correctly
sys.stdout = s
sys.stdout = sys.__stdout__
print "s:", s.getvalue(),
This worked for me.
Hope you can use this way to do it. Possibly add more intelligence around
StringIO() if you don't always need to redirect the output inside rexec.
I don't know if this has security implications (rexec is a security thing,