redirect output from embedded C module

vincent wehren vincent at
Tue Jun 15 17:45:36 CEST 2004

legba wrote:
> hi all..  I'm writing an application in python2.3 under linux debian 
> which accepts new "plug-ins" in the form of c-written python extension
> modules.
> My problem is that I'd like to catch the stdout/stderr from one of
> this modules
> and redirect it into a tkinter text widget, in real time. Let's say
> the module's
> name is plugin and that plugin.doSomething() prints out with a C
> printf()
> the string "it's a mess".
> Redirecting sys.stdout before calling plugin.doSomething() doesn't
> work, I guess
> because the plugin code has a different stdout file then python. So
> the second solution is to force every plugin-writer to embed in every
> plugin the C lines:
> if ((fd = open("outfile", O_WRONLY | O_CREAT | O_TRUNC, 0600)) == -1)
>    {
>       perror("open outfile");
>       exit(2);
>    }
>    dup2(fd, fileno(stdout));
> and use as "outfile" the real stout. Then redirect python stdout to
> the text widget.
> But how do I access python stdout from C?

Use the API functions PySys_WriteStdout(format, ...)
and PySys_WriteStderr(format, ...)


Vincent Wehren

> or even better how do I access the text widget directly from C?
> or, since this solution looks quite crappy, how do I do it in another
> way?
> thanks to all
> legba

More information about the Python-list mailing list