diff options
author | Victor Stinner <vstinner@redhat.com> | 2019-05-22 23:00:58 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-22 23:00:58 (GMT) |
commit | df22c03b93ea4620fdf4a0b3cbbbfa7c645af783 (patch) | |
tree | 87a86340ef143d19350e9f52b18d4f28f0a982b7 /Python/errors.c | |
parent | 5edcf263581c70f6a6c2206db679e51e9418bb38 (diff) | |
download | cpython-df22c03b93ea4620fdf4a0b3cbbbfa7c645af783.zip cpython-df22c03b93ea4620fdf4a0b3cbbbfa7c645af783.tar.gz cpython-df22c03b93ea4620fdf4a0b3cbbbfa7c645af783.tar.bz2 |
bpo-36829: PyErr_WriteUnraisable() normalizes exception (GH-13507)
PyErr_WriteUnraisable() now creates a traceback object if there is no
current traceback. Moreover, call PyErr_NormalizeException() and
PyException_SetTraceback() to normalize the exception value. Ignore
silently any error.
Diffstat (limited to 'Python/errors.c')
-rw-r--r-- | Python/errors.c | 22 |
1 files changed, 21 insertions, 1 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) { |