diff options
Diffstat (limited to 'Python')
-rw-r--r-- | Python/errors.c | 22 | ||||
-rw-r--r-- | Python/traceback.c | 15 |
2 files changed, 34 insertions, 3 deletions
diff --git a/Python/errors.c b/Python/errors.c index 9622b5a..1b8b7ee 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -4,6 +4,7 @@ #include "Python.h" #include "pycore_coreconfig.h" #include "pycore_pystate.h" +#include "pycore_traceback.h" #ifndef __STDC__ #ifndef MS_WINDOWS @@ -1048,7 +1049,7 @@ write_unraisable_exc_file(PyObject *exc_type, PyObject *exc_value, } } - if (!exc_type) { + if (exc_type == NULL || exc_type == Py_None) { return -1; } @@ -1106,6 +1107,7 @@ write_unraisable_exc_file(PyObject *exc_type, PyObject *exc_value, } } } + if (PyFile_WriteString("\n", file) < 0) { return -1; } @@ -1177,6 +1179,24 @@ PyErr_WriteUnraisable(PyObject *obj) goto default_hook; } + if (exc_tb == NULL) { + struct _frame *frame = _PyThreadState_GET()->frame; + if (frame != NULL) { + exc_tb = _PyTraceBack_FromFrame(NULL, frame); + if (exc_tb == NULL) { + PyErr_Clear(); + } + } + } + + PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb); + + if (exc_tb != NULL && exc_tb != Py_None && PyTraceBack_Check(exc_tb)) { + if (PyException_SetTraceback(exc_value, exc_tb) < 0) { + PyErr_Clear(); + } + } + _Py_IDENTIFIER(unraisablehook); PyObject *hook = _PySys_GetObjectId(&PyId_unraisablehook); if (hook != NULL && hook != Py_None) { diff --git a/Python/traceback.c b/Python/traceback.c index 18bd0bf..04b52ad 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -227,13 +227,24 @@ PyTypeObject PyTraceBack_Type = { tb_new, /* tp_new */ }; + +PyObject* +_PyTraceBack_FromFrame(PyObject *tb_next, PyFrameObject *frame) +{ + assert(tb_next == NULL || PyTraceBack_Check(tb_next)); + assert(frame != NULL); + + return tb_create_raw((PyTracebackObject *)tb_next, frame, frame->f_lasti, + PyFrame_GetLineNumber(frame)); +} + + int PyTraceBack_Here(PyFrameObject *frame) { PyObject *exc, *val, *tb, *newtb; PyErr_Fetch(&exc, &val, &tb); - newtb = tb_create_raw((PyTracebackObject *)tb, frame, frame->f_lasti, - PyFrame_GetLineNumber(frame)); + newtb = _PyTraceBack_FromFrame(tb, frame); if (newtb == NULL) { _PyErr_ChainExceptions(exc, val, tb); return -1; |