>From Python, this is simple; use something like
import StringIO
import sys
myout = StringIO.StringIO()
myerr = StringIO.StringIO()
saveout = sys.stdout
saveerr = sys.stderr
try:
sys.stdout = myout
sys.stderr = myerr
exec <whatever>
finally:
sys.stdout = saveerr
sys.stderr = saveerr
and then myout.getvalue() and myerr.getvalue() return the standard
output and standard error text from <whatever>.
Also note that if you want a standard exception traceback as well, you
could replace "exec <whatever>" by
try:
except <whatever>
except:
import traceback
traceback.print_exc()
(This prints the traceback on stdout -- redirecting it to stderr is
left as an excercise :-)
--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>