redirect output from embedded C module

Discussion in 'Python' started by legba, Jun 15, 2004.

  1. legba

    legba Guest

    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
     
    legba, Jun 15, 2004
    #1
    1. Advertising

  2. 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
     
    vincent wehren, Jun 15, 2004
    #2
    1. Advertising

  3. legba

    legba Guest

    >
    > 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.
     
    legba, Jun 16, 2004
    #3
  4. 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.
     
    Jacek Czerwinski, Jul 23, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Paul Gloster
    Replies:
    48
    Views:
    1,892
    Colin Paul Gloster
    Apr 10, 2007
  2. Thomas Dodds

    Databind an embedded control in an embedded datagrid

    Thomas Dodds, Jul 26, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    402
    Thomas Dodds
    Jul 26, 2004
  3. Sal
    Replies:
    1
    Views:
    549
  4. Trans
    Replies:
    11
    Views:
    303
    micathom
    Sep 5, 2007
  5. Num GG
    Replies:
    2
    Views:
    365
    Num GG
    Nov 17, 2008
Loading...

Share This Page