Redirect stdout to a buffer

Ecir Hana ecir.hana at gmail.com
Sun Nov 15 11:28:07 EST 2009


Hello,

I'm trying to write a simple Win32 app, which may run some Python
scripts. Since it is a Windows GUI app, I would like to redirect all
output (Python print, C printf, fprinf stderr, ...) to a text area
inside the app. In other words, I'm trying to log all the output from
the app (C, Python) to a window. So far, this works for C printf():

int fds[2];
_pipe(fds, 1024, O_TEXT);
_dup2(fds[1], 1);
...
and then I read from pipe's read-end and append the text to the text
area.

But when I try to run:
Py_Initialize();
PyRun_SimpleString("print 'abc'");
Py_Finalize();

I get an error:
IOError: [Errno 9] Bad file descriptor

What am I doing wrong? How to redirect standard IO, both for C and for
Python?
PS: Maybe I'm doind something wrong, but SetStdHandle() does not work
at all....



More information about the Python-list mailing list