making python write to ostreams

Brian Quinlan BrianQ at ActiveState.com
Tue Jul 10 20:04:50 EDT 2001


> i cant find a way to make python redirect its output to a
> ostream in my embedded app,
>
> ...wait ostreams are c++, so there is probably _no_ way to
> redirect the output!?
> im doomed
> thx to anyone proving the different!

Actually, doing this is quite straightforward. All you have to do is
create a new Python object type that implements a write() method and
contains the ostream that you want to redirect too. When the object's
write method is called, you simply forward the string to your ostream
instance.  The python code to assign an instance of your type to the
python stdout instance would look like:

import sys
sys.stdout = InstanceOfMyType

Cheers,
Brian





More information about the Python-list mailing list