Re: newbie asks how to capture embeded python output

Guido.van.Rossum@cwi.nl
Tue, 03 May 1994 13:08:08 +0200

> I've been playing around with python embeded in a NeXTSTEP app. I'd like
> to capture the python output going to stdout and stderr and pass it into
> my text objects. The only means I've found so far is to dup2 the
> stdout/stderr file descriptors, close them, and read the stdout myself.
> Does python have a more elegant way to do this built in ? I've thus far
> resisted running python using objc subprocess object, which has the above
> redirection capability built in.

I may totally misunderstand what you're trying to do, but Python has a
way built in to capture output intp a Python data structure. If you
create a class with a write() method that takes a string argument, and
assign an instance of it to sys.stdout (and the same or another to
sys.stderr), then everything written by print statements (and
everything written explicitly to sys.stdout/stderr) is passed to the
instance instead, which can do as it please (e.g. display it in a
window if it has a windowing interface).

Maybe this helps...

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
URL: <http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>