bpo-33330: Write exceptions occurred in PyImport_Cleanup() to stderr. (GH-6606)

They where silenced before.
This commit is contained in:
Serhiy Storchaka 2018-04-29 22:16:30 +03:00 committed by GitHub
parent 55edd0c185
commit c1a6832f50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 20 deletions

View File

@ -603,8 +603,9 @@ _PyModule_ClearDict(PyObject *d)
else else
PyErr_Clear(); PyErr_Clear();
} }
if (PyDict_SetItem(d, key, Py_None) != 0) if (PyDict_SetItem(d, key, Py_None) != 0) {
PyErr_Clear(); PyErr_WriteUnraisable(NULL);
}
} }
} }
} }
@ -623,8 +624,9 @@ _PyModule_ClearDict(PyObject *d)
else else
PyErr_Clear(); PyErr_Clear();
} }
if (PyDict_SetItem(d, key, Py_None) != 0) if (PyDict_SetItem(d, key, Py_None) != 0) {
PyErr_Clear(); PyErr_WriteUnraisable(NULL);
}
} }
} }
} }

View File

@ -417,14 +417,14 @@ PyImport_Cleanup(void)
if (Py_VerboseFlag) if (Py_VerboseFlag)
PySys_WriteStderr("# clear builtins._\n"); PySys_WriteStderr("# clear builtins._\n");
if (PyDict_SetItemString(interp->builtins, "_", Py_None) < 0) { if (PyDict_SetItemString(interp->builtins, "_", Py_None) < 0) {
PyErr_Clear(); PyErr_WriteUnraisable(NULL);
} }
for (p = sys_deletes; *p != NULL; p++) { for (p = sys_deletes; *p != NULL; p++) {
if (Py_VerboseFlag) if (Py_VerboseFlag)
PySys_WriteStderr("# clear sys.%s\n", *p); PySys_WriteStderr("# clear sys.%s\n", *p);
if (PyDict_SetItemString(interp->sysdict, *p, Py_None) < 0) { if (PyDict_SetItemString(interp->sysdict, *p, Py_None) < 0) {
PyErr_Clear(); PyErr_WriteUnraisable(NULL);
} }
} }
for (p = sys_files; *p != NULL; p+=2) { for (p = sys_files; *p != NULL; p+=2) {
@ -434,7 +434,7 @@ PyImport_Cleanup(void)
if (value == NULL) if (value == NULL)
value = Py_None; value = Py_None;
if (PyDict_SetItemString(interp->sysdict, *p, value) < 0) { if (PyDict_SetItemString(interp->sysdict, *p, value) < 0) {
PyErr_Clear(); PyErr_WriteUnraisable(NULL);
} }
} }
@ -443,8 +443,9 @@ PyImport_Cleanup(void)
for diagnosis messages (in verbose mode), while the weakref helps for diagnosis messages (in verbose mode), while the weakref helps
detect those modules which have been held alive. */ detect those modules which have been held alive. */
weaklist = PyList_New(0); weaklist = PyList_New(0);
if (weaklist == NULL) if (weaklist == NULL) {
PyErr_Clear(); PyErr_WriteUnraisable(NULL);
}
#define STORE_MODULE_WEAKREF(name, mod) \ #define STORE_MODULE_WEAKREF(name, mod) \
if (weaklist != NULL) { \ if (weaklist != NULL) { \
@ -452,13 +453,13 @@ PyImport_Cleanup(void)
if (wr) { \ if (wr) { \
PyObject *tup = PyTuple_Pack(2, name, wr); \ PyObject *tup = PyTuple_Pack(2, name, wr); \
if (!tup || PyList_Append(weaklist, tup) < 0) { \ if (!tup || PyList_Append(weaklist, tup) < 0) { \
PyErr_Clear(); \ PyErr_WriteUnraisable(NULL); \
} \ } \
Py_XDECREF(tup); \ Py_XDECREF(tup); \
Py_DECREF(wr); \ Py_DECREF(wr); \
} \ } \
else { \ else { \
PyErr_Clear(); \ PyErr_WriteUnraisable(NULL); \
} \ } \
} }
#define CLEAR_MODULE(name, mod) \ #define CLEAR_MODULE(name, mod) \
@ -467,7 +468,7 @@ PyImport_Cleanup(void)
PySys_FormatStderr("# cleanup[2] removing %U\n", name); \ PySys_FormatStderr("# cleanup[2] removing %U\n", name); \
STORE_MODULE_WEAKREF(name, mod); \ STORE_MODULE_WEAKREF(name, mod); \
if (PyObject_SetItem(modules, name, Py_None) < 0) { \ if (PyObject_SetItem(modules, name, Py_None) < 0) { \
PyErr_Clear(); \ PyErr_WriteUnraisable(NULL); \
} \ } \
} }
@ -482,13 +483,13 @@ PyImport_Cleanup(void)
else { else {
PyObject *iterator = PyObject_GetIter(modules); PyObject *iterator = PyObject_GetIter(modules);
if (iterator == NULL) { if (iterator == NULL) {
PyErr_Clear(); PyErr_WriteUnraisable(NULL);
} }
else { else {
while ((key = PyIter_Next(iterator))) { while ((key = PyIter_Next(iterator))) {
value = PyObject_GetItem(modules, key); value = PyObject_GetItem(modules, key);
if (value == NULL) { if (value == NULL) {
PyErr_Clear(); PyErr_WriteUnraisable(NULL);
continue; continue;
} }
CLEAR_MODULE(key, value); CLEAR_MODULE(key, value);
@ -496,7 +497,7 @@ PyImport_Cleanup(void)
Py_DECREF(key); Py_DECREF(key);
} }
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
PyErr_Clear(); PyErr_WriteUnraisable(NULL);
} }
Py_DECREF(iterator); Py_DECREF(iterator);
} }
@ -508,17 +509,20 @@ PyImport_Cleanup(void)
} }
else { else {
_Py_IDENTIFIER(clear); _Py_IDENTIFIER(clear);
if (_PyObject_CallMethodId(modules, &PyId_clear, "") == NULL) if (_PyObject_CallMethodId(modules, &PyId_clear, "") == NULL) {
PyErr_Clear(); PyErr_WriteUnraisable(NULL);
}
} }
/* Restore the original builtins dict, to ensure that any /* Restore the original builtins dict, to ensure that any
user data gets cleared. */ user data gets cleared. */
dict = PyDict_Copy(interp->builtins); dict = PyDict_Copy(interp->builtins);
if (dict == NULL) if (dict == NULL) {
PyErr_Clear(); PyErr_WriteUnraisable(NULL);
}
PyDict_Clear(interp->builtins); PyDict_Clear(interp->builtins);
if (PyDict_Update(interp->builtins, interp->builtins_copy)) if (PyDict_Update(interp->builtins, interp->builtins_copy)) {
PyErr_Clear(); PyErr_Clear();
}
Py_XDECREF(dict); Py_XDECREF(dict);
/* Clear module dict copies stored in the interpreter state */ /* Clear module dict copies stored in the interpreter state */
_PyState_ClearModules(); _PyState_ClearModules();