when embedding Python, how do you redirect stdout/stderr?

Dave Kuhlman dkuhlman at rexx.com
Thu Jun 14 12:55:47 EDT 2001


If you want to do it all in C, then I do _not_ have an answer for
you.

However, if you want to do some of it in Python and initiate it
from C, then create a Python class with a "write" method and use
PyRun_SimpleString to assign an instance of that class to
sys.stdout.

You can read about it at:

    http://www.python.org/doc/current/lib/module-sys.html

(search for "stdout")

And here is an example:

    PyRun_SimpleString("import sys");
    PyRun_SimpleString("import myredirector");
    PyRun_SimpleString("sys.stdout = myredirector.Redirector()");

OK, _I_ did the easy part.  Now _you_ create the custom GUI texbox in
Python.

  - Dave


David Gravereaux <davygrvy at pobox.com> wrote:
> 
> Hi all,
> 
> The subject says it all.  I'm embedding (or trying) and want to have all std
> traffic sent to a custom textbox in a windows GUI.  Is there anyway to do this?
> A good place for examples for me to read?  I played around a little bit with
> this, but need a more stream'ish way:
> 
>         output = PycStringIO->NewOutput(1024);
> 
>         /* capture all interpreter error output */
>         PySys_SetObject("stderr", output);
> 
>         /* so that error is printed on standard error, redirected above */
>         PyErr_Print();
>         message = PycStringIO->cgetvalue(output);
>         string = PyString_AsString(message);
>         length = PyString_Size(message);
> 
> I've since updated the exception getting part, so I don't have to redirect, but
> I do need to have the std traffic now go somewhere.  Any ideas?
> --
> David Gravereaux <davygrvy at pobox.com>
> -=[ Ray's Famous and Truly the Original Pizza, NYC ]=-

-- 
Dave Kuhlman
dkuhlman at rexx.com



More information about the Python-list mailing list