XP STDOUT & STDERR Redirection

Andrew Csillag andrew at starmedia.net
Fri Oct 8 09:37:31 EDT 1999


Brandon Ehle wrote:
> 
> Can someone come up with a good way that I can catch Python's STDOUT and
> STDERR from a C/C++ program?

You have a couple of choices, but the easiest one is probably to create
cStringIO objects from C/C++ land and assign them to sys.stdout and
sys.stderr and call the getvalue() method on them when the python script
part finishes.  If you need the output as you go along (not wait till
the script exits), you can instead create python objects (perhaps in
C/C++) that have write methods and assign them to sys.stdout/stderr and
the methods output the stuff to the engine's console.
-- 
import string,sys;t=string;f=t.index;j=t.join;print(lambda
s:j(map(lambda
x,s=s:x[:f(x,':')+2]+j(map(lambda y:'%02X'%ord(y),x[f(x,':')+2:]),'
'),map
(lambda i,s=s:("%04X:
"%i)+s[i:i+16],range(0,len(s),16))),'\n'))(open(sys.
argv[1]).read())




More information about the Python-list mailing list