diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 3953fec0f5..f71cba6399 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -505,6 +505,9 @@ _Py_CoerceLegacyLocale(void) } +void +_PySys_SetLastException(PyObject *exception, PyObject *v, PyObject *tb); + /* Global initializations. Can be undone by Py_Finalize(). Don't call this twice without an intervening Py_Finalize() call. @@ -681,6 +684,8 @@ void _Py_InitializeCore(const _PyCoreConfig *config) PyDict_SetItemString(interp->sysdict, "modules", interp->modules); + _PySys_SetLastException(Py_None, Py_None, Py_None); + /* Set up a preliminary stderr printer until we have enough infrastructure for the io module in place. */ pstderr = PyFile_NewStdPrinter(fileno(stderr)); diff --git a/Python/pythonrun.c b/Python/pythonrun.c index f31b3ee5a5..c74670763f 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -606,6 +606,14 @@ handle_system_exit(void) } void +_PySys_SetLastException(PyObject *exception, PyObject *v, PyObject *tb) +{ + _PySys_SetObjectId(&PyId_last_type, exception); + _PySys_SetObjectId(&PyId_last_value, v); + _PySys_SetObjectId(&PyId_last_traceback, tb); +} + +void PyErr_PrintEx(int set_sys_last_vars) { PyObject *exception, *v, *tb, *hook; @@ -626,9 +634,7 @@ PyErr_PrintEx(int set_sys_last_vars) return; /* Now we know v != NULL too */ if (set_sys_last_vars) { - _PySys_SetObjectId(&PyId_last_type, exception); - _PySys_SetObjectId(&PyId_last_value, v); - _PySys_SetObjectId(&PyId_last_traceback, tb); + _PySys_SetLastException(exception, v, tb); } hook = _PySys_GetObjectId(&PyId_excepthook); if (hook) {