how to get string printed by PyErr_Print( )?

grbgooglefan ganeshborse at
Wed Jan 9 13:01:42 CET 2008

On Dec 19 2007, 5:55 pm, Christian Heimes <li... at> wrote:
> grbgooglefan wrote:
> > PythonC API functionPyErr_Print( ) prints an error string onto stderr
> > if PyErr_Occurred() is true.
> > I don't want to print this to stderr because my Python+C code is
> > running daemon mode & won't have terminal / stderr.
> > So, I want to retrieve the string whichPyErr_Print( ) will print.
> > E.g.,PyErr_Print() printed following string when I tried to call
> > setTuple with one extra argument
> > Traceback (most recent call last):
> >   File "<string>", line 2, in isTacticSafe
> > IndexError: tuple assignment index out of range
> I suggest a different approach. A daemon must have a stdin, stdout and
> stderr connected to a terminal. You can use freopen() to redirect stderr
> and stdout to a log file and fclose() to close stdin.
> Christian

I do not want to redirect anything to file. Because I do not want to
avoid the disk access completely - for reading as well as writing.
I liked the 1st option suggested by Robert Kern.

Can you please explain a little bit how can I replace sys.stderr with
StringIO or my char* buffer?
I have to show the error message of Python code compilation &
execution to the user on the GUI & therefore I want to capture the
error message directly instead of logging it to file.

Thanks in advance for all your help.

More information about the Python-list mailing list