redirect output from embedded C module

Jacek Czerwinski jacek_delete_this at klik.rubikon.pl
Fri Jul 23 13:35:53 CEST 2004


15 Jun 2004 03:29:42 -0700, na comp.lang.python, legba napisał(a):

> 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".
> 

in initialization set C/python code
---------------

 m = initmojeokna((TForm*)Owner);
 c=PyRun_SimpleString("import mojeokna\n");
 PyObject* mod= PyImport_ImportModule("mojeokna");


 c=PyRun_SimpleString("import sys\n"
        "sys.stdout=mojeokna.openwin()\n"
        "sys.stderr=sys.stdout\n");

---------------------


where [mojeokna = polish mywindows]  .openwin() is contructor of standard
Python object with 'write' method.

Work well in Pytnon 2.0/2.1 in Junly 2002.
Im not sure, but 2.3 can have one or two places where it writes to 
'hardwired' stdout, not wirtual sys.stdout.

> 
> if ((fd = open("outfile", O_WRONLY | O_CREAT | O_TRUNC, 0600)) == -1)
>    dup2(fd, fileno(stdout));

Will not work.



More information about the Python-list mailing list