Accessing a Python traceback from the C API
<p>I'm having some trouble figuring out the proper way to walk a Python traceback using the C API.  I'm writing an application that embeds the Python interpreter.  I want to be able to execute arbitrary Python code, and if it raises an exception, to translate it to my own application-specific C++ exception.  For now, it is sufficient to extract just the file name and line number where the Python exception was raised.  This is what I have so far:</p>

<pre><code>PyObject* pyresult = PyObject_CallObject(someCallablePythonObject, someArgs);
if (!pyresult)
{
    PyObject* excType, *excValue, *excTraceback;
    PyErr_Fetch(&amp;excType, &amp;excValue, &amp;excTraceback);
    PyErr_NormalizeException(&amp;excType, &amp;excValue, &amp;excTraceback);

    PyTracebackObject* traceback = (PyTracebackObject*)traceback;
    // Advance to the last frame (python puts the most-recent call at the end)
    while (traceback-&gt;tb_next != NULL)
        traceback = traceback-&gt;tb_next;

    // At this point I have access to the line number via traceback-&gt;tb_lineno,
    // but where do I get the file name from?

    // ...       
}
</code></pre>

<p>Digging around in the Python source code, I see they access both the filename and module name of the current frame via the <code>_frame</code> structure, which looks like it is a privately-defined struct.  My next idea was to programmatically load the Python 'traceback' module and call its functions with the C API.  Is this sane? Is there a better way to access a Python traceback from C?</p>
