redirect output from embedded C module

L

legba

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?
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
 
V

vincent wehren

legba said:
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, ...)

HTH,

Vincent Wehren
 
L

legba

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

the main problem here is that I do not control all the code I'm using
as plugins.
I'd like people to mix existent code with my program and I can't ask
them to convert every printf with PySys_WriteStdout...

thanks,
legba.
 
J

Jacek Czerwinski

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top