This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Enhancement request for importing stacktraces from foreign sources
Type: enhancement Stage:
Components: C API Versions:
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Thrameos
Priority: normal Keywords:

Created on 2020-12-10 21:49 by Thrameos, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg382852 - (view) Author: Karl Nelson (Thrameos) Date: 2020-12-10 21:49
In JPype, I am transfer stack information from Java into Python for diagnostics and display purposed.  Unfortunately, as the exception system is directly accessing traceback structure elements this cannot be replicated without creating traceback structures in C.   I have thus been forced to create new methods that create this using internal Python structures.  It would be much better if there were C API method to allow importing a foreign stack trace into Python.

Here is an example of the code used in JPype for reference.

// Transfer list of filenames, functions and lines to Python.
PyObject* PyTrace_FromJPStackTrace(JPStackTrace& trace)
        PyTracebackObject *last_traceback = NULL;
        PyObject *dict = PyModule_GetDict(PyJPModule);
        for (JPStackTrace::iterator iter = trace.begin(); iter != trace.end(); ++iter)
                last_traceback = tb_create(last_traceback, dict,  iter->getFile(),
                                iter->getFunction(), iter->getLine());
        if (last_traceback == NULL)
        return (PyObject*) last_traceback;

PyTracebackObject *tb_create(                                                                                                                                                               PyTracebackObject *last_traceback,
                PyObject *dict,                                                                                                                                                             const char* filename,
                const char* funcname,
                int linenum)
        // Create a code for this frame.
        PyCodeObject *code = PyCode_NewEmpty(filename, funcname, linenum);
        // Create a frame for the traceback.
        PyFrameObject *frame = (PyFrameObject*) PyFrame_Type.tp_alloc(&PyFrame_Type, 0);
        frame->f_back = NULL;
        if (last_traceback != NULL)
                frame->f_back = last_traceback->tb_frame;
        frame->f_builtins = dict;
        frame->f_code = (PyCodeObject*) code;
        frame->f_executing = 0;
        frame->f_gen = NULL;
        frame->f_globals = dict;
        frame->f_iblock = 0;
        frame->f_lasti = 0;
        frame->f_lineno = 0;
        frame->f_locals = NULL;
        frame->f_localsplus[0] = 0;
        frame->f_stacktop = NULL;
        frame->f_trace = NULL;
        frame->f_valuestack = 0;
#if PY_VERSION_HEX>=0x03070000
        frame->f_trace_lines = 0;
        frame->f_trace_opcodes = 0;
        // Create a traceback
        PyTracebackObject *traceback = (PyTracebackObject*)
                        PyTraceBack_Type.tp_alloc(&PyTraceBack_Type, 0);
        traceback->tb_frame = frame;
        traceback->tb_lasti = frame->f_lasti;
        traceback->tb_lineno = linenum;
        traceback->tb_next = last_traceback;
        return traceback;

Date User Action Args
2022-04-11 14:59:39adminsetgithub: 86784
2020-12-10 21:49:26Thrameoscreate