stderr and stdout

Arinté shouldbe at message.com
Thu Jun 29 09:55:34 EDT 2000


I have python embedded in an app, so I need to redirect stdout and stderr
somewhere else:
    IOmod = PyImport_ImportModule("cStringIO");
    obj = PyObject_GetAttrString(IOmod, "StringIO");
    errorIO = PyObject_CallFunction(obj, NULL);
    stdIO = PyObject_CallFunction(obj,NULL);
    Py_DECREF(obj);

    /* redirect sys.stderr to a cStringIO instance */
    PySys_SetObject("stderr", errorIO);
    PySys_SetObject("stdout", stdIO);

Is there a better way to do this, especially so the output would be
synchronized?  Right now I print all the stdout then I print all the stderr
and that makes it hard to figure out which line in stdout came before such
and such a line in stderr.

Oh I print stdout and err like so:
   char *pix = PyString_AsString(temp);
   Py_XDECREF(temp);
   appcallback()->status(pix);
where temp is either errorIO or stdIO.






More information about the Python-list mailing list