redirect output from embedded C module
vincent at visualtrans.de
Tue Jun 15 17:45:36 CEST 2004
> 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
> 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
> 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");
> 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, ...)
> 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
> thanks to all
More information about the Python-list