Redirecting stdout in rexec

Tim Lesher tim at lesher.ws
Thu Jun 6 10:39:35 EDT 2002


[Python 2.1.1, Linux]

I'm having trouble redirecting output from an rexec call to my own
string.  I want to use commands of the form "print 'foo'" in the
rexec'ed code, and capture them into a string object in the caller. 
This is roughly what I'm doing:
-----
import rexec

class StringCapture:
    def __init__(self):
        self.buf = ""
    def write(self, str):
        self.buf += str

capture = StringCapture()

renv = rexec.RExec()
module = renv.add_module("sys")
module.__dict__["stdout"] = capture

renv.s_exec("print 'Hello World'")

print "Output is: %s" % capture.buf
-----

It doesn't appear that the replacement of stdout is being seen by the
s_exec, although the same procedure works if I replace the local
stdout.  Is there some additional step I need to do to use this with
rexec?

Thanks.

-- 
Tim Lesher
tim at lesher.ws



More information about the Python-list mailing list