how to get string printed by PyErr_Print( )?

Robert Kern robert.kern at gmail.com
Wed Dec 19 04:38:14 EST 2007


grbgooglefan wrote:
> PythonC API function PyErr_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 which PyErr_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
> 
> How do I get this error message in a local char* & use it for further
> error handling?
> 
> Also, is there any way to get an error number associated for these
> error conditions using some getError function on the object returned
> by PyErr_Occurred()?

PyErr_Print() will import the sys module and try to use whatever file-like
object is sys.stderr. Replace this with a StringIO or an open file object just
like you would for output from the Python level.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list