diff -r 4c5f7b61b6c5 Python/traceback.c --- a/Python/traceback.c Sun Jun 19 18:32:07 2016 +0300 +++ b/Python/traceback.c Sun Jun 19 20:07:54 2016 +0300 @@ -132,44 +132,51 @@ newtracebackobject(PyTracebackObject *ne int PyTraceBack_Here(PyFrameObject *frame) { - PyThreadState *tstate = PyThreadState_GET(); - PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback; - PyTracebackObject *tb = newtracebackobject(oldtb, frame); - if (tb == NULL) + PyObject *exc, *val, *tb, *newtb; + PyErr_Fetch(&exc, &val, &tb); + newtb = (PyObject *)newtracebackobject((PyTracebackObject *)tb, frame); + if (newtb == NULL) { + _PyErr_ChainExceptions(exc, val, tb); return -1; - tstate->curexc_traceback = (PyObject *)tb; - Py_XDECREF(oldtb); + } + PyErr_Restore(exc, val, newtb); + Py_XDECREF(tb); return 0; } /* Insert a frame into the traceback for (funcname, filename, lineno). */ void _PyTraceback_Add(const char *funcname, const char *filename, int lineno) { - PyObject *globals = NULL; + PyObject *globals; PyCodeObject *code = NULL; PyFrameObject *frame = NULL; - PyObject *exception, *value, *tb; + PyObject *exc, *val, *tb; /* Save and clear the current exception. Python functions must not be called with an exception set. Calling Python functions happens when the codec of the filesystem encoding is implemented in pure Python. */ - PyErr_Fetch(&exception, &value, &tb); + PyErr_Fetch(&exc, &val, &tb); globals = PyDict_New(); if (!globals) - goto done; + goto error; code = PyCode_NewEmpty(filename, funcname, lineno); if (!code) - goto done; + goto error; frame = PyFrame_New(PyThreadState_Get(), code, globals, NULL); if (!frame) - goto done; + goto error; frame->f_lineno = lineno; - PyErr_Restore(exception, value, tb); + PyErr_Restore(exc, val, tb); PyTraceBack_Here(frame); + Py_DECREF(globals); + Py_DECREF(code); + Py_DECREF(frame); + return; -done: +error: + _PyErr_ChainExceptions(exc, val, tb); Py_XDECREF(globals); Py_XDECREF(code); Py_XDECREF(frame);