Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > redirect output from embedded C module

Reply
Thread Tools

redirect output from embedded C module

 
 
legba
Guest
Posts: n/a
 
      06-15-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
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
 
Reply With Quote
 
 
 
 
vincent wehren
Guest
Posts: n/a
 
      06-15-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, ...)

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

 
Reply With Quote
 
 
 
 
legba
Guest
Posts: n/a
 
      06-16-2004
>
> 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.
 
Reply With Quote
 
Jacek Czerwinski
Guest
Posts: n/a
 
      07-23-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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Embedded vs. Non-embedded Tests Trans Ruby 11 09-05-2007 11:22 AM
Embedded languages based on early Ada (from "Re: Preferred OS, processor family for running embedded Ada?") Colin Paul Gloster VHDL 48 04-10-2007 10:31 AM
How to display images embedded in e-mail as embedded, not attachments Jim Firefox 4 12-11-2004 05:36 AM
Databind an embedded control in an embedded datagrid Thomas Dodds ASP .Net Datagrid Control 0 07-26-2004 08:20 PM
Basic Q - Response.Redirect, all redirect to first Response.Redirect statement Sal ASP .Net Web Controls 1 05-15-2004 03:46 PM



Advertisments