Redirect stdout to a buffer [Errno 9]

Ecir Hana ecir.hana at
Mon Nov 16 18:17:52 CET 2009

On Nov 15, 5:28 pm, Ecir Hana <ecir.h... at> wrote:
> 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....

Also, maybe this matters: it's on WinXP, Python 2.6 and MinGW GCC.

More information about the Python-list mailing list