[New-bugs-announce] [issue42618] Enhancement request for importing stacktraces from foreign sources

Karl Nelson report at bugs.python.org
Thu Dec 10 16:49:26 EST 2020

New submission from Karl Nelson <nelson85 at llnl.gov>:

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;


components: C API
messages: 382852
nosy: Thrameos
priority: normal
severity: normal
status: open
title: Enhancement request for importing stacktraces from foreign sources
type: enhancement

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list