If I do (e.g.)
open("/snafu/fnord")
in Python (and the file does not exist), I get a traceback and the message
IOError: [Errno 2] No such file or directory: '/snafu/fnord'
I would like to get the above string with Python's C API (i.e., a Python interpreter embedded in a C program). I need it as a string, not output to the console.
With PyErr_Fetch()
I can get the type object of the exception and the value. For the above example, the value is a tuple:
(2, 'No such file or directory', '/snafu/fnord')
Is there an easy way from the information I get from PyErr_Fetch()
to the string the Python interpreter shows? (One that does not involve to construct such strings for each exception type yourself.)