Formatting Traceback info in the Python/C API

Tim Peters tim.one at comcast.net
Sat Mar 1 18:45:32 EST 2003


[NG]
> Hi everyone, I have a question. I want to format and print traceback
> information using the C API. The reason I do not want to use
> PyErr_Print() is because on Windows I want to capture the text
> formatted traceback and put it up as a dialog box. I have looked over
> the entire C API but cannot find a way to do it.
>
> I want to do a
>
> if (PyErr_Occurred()) {
> PyObject *exception, *value, *traceback;
> PyErr_Fetch(&exception,&value,&traceback);
> /* Now I want to do something like what the traceback module does */
> string = PyErr_FormatTraceback(traceback);
> GuiDisplay(string);
> }
>
> Any help would be appreciated. I thought of importing the traceback
> module in the C API and then do a PyCallObject(...) etc but that seems
> like a very roundabout way of doing things.

To the contrary, it's the most straightforward way of doing it:  reuse code
that already does what you want.  You probably want PyObject_CallMethod(),
though, which works for module.attribute calls as well as object.method
calls.  If you want to reproduce all the logic in C instead, you deserve all
the pain you're experiencing <wink>.






More information about the Python-list mailing list