By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,494 Members | 890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,494 IT Pros & Developers. It's quick & easy.

redirect output from embedded C module

P: n/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".

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
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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, ...)

HTH,

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

Jul 18 '05 #2

P: n/a
>
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.
Jul 18 '05 #3

P: n/a
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.
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.