redirect sys.stdout to C++ ?

Tatsujin tatsujin at despammed.com
Sat Dec 21 09:47:36 EST 2002


Hiya all!  This is my first posting here, so be gentle...

I have this C++ application in Linux that I have embedded a Python interpreter into, 
including an interactive prompt similar to the one "python" has.

My question is this... how do I "redirect" the stuff that gets shoved through 
sys.stdout and sys.stderr so my application can deal with it, like outputting it in 
various manners, log to disk and such? Because invoking things like "print 'a'" gets 
output to where my application was started and that's of course not the desired 
behaviour...

I guess what I basically want to do is subclass a file() object class with my own 
type, and assign an instance of that to sys.stdout and sys.stderr. I've read the 
documentation about how to create new types with C/C++, but i couldn't easily deduce 
how to go about a subclassing of an existing (builtin) type. Is this the way to go? 
Or is there an easier way? Any pointers to stuff out there that do similar stuff? I'm 
not using any helper packages (like boost or swig), just plain Python API.

All help appreciated!

/André






More information about the Python-list mailing list