Skip to content

Commit 291c9d4

Browse files
bpo-33330: Improve error handling in PyImport_Cleanup(). (GH-6564)
(cherry picked from commit e9d9494) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 53d36cc commit 291c9d4

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

Python/import.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ PyImport_GetModule(PyObject *name)
364364
}
365365
else {
366366
m = PyObject_GetItem(modules, name);
367-
if (PyErr_ExceptionMatches(PyExc_KeyError)) {
367+
if (m == NULL && PyErr_ExceptionMatches(PyExc_KeyError)) {
368368
PyErr_Clear();
369369
}
370370
}
@@ -416,20 +416,26 @@ PyImport_Cleanup(void)
416416

417417
if (Py_VerboseFlag)
418418
PySys_WriteStderr("# clear builtins._\n");
419-
PyDict_SetItemString(interp->builtins, "_", Py_None);
419+
if (PyDict_SetItemString(interp->builtins, "_", Py_None) < 0) {
420+
PyErr_Clear();
421+
}
420422

421423
for (p = sys_deletes; *p != NULL; p++) {
422424
if (Py_VerboseFlag)
423425
PySys_WriteStderr("# clear sys.%s\n", *p);
424-
PyDict_SetItemString(interp->sysdict, *p, Py_None);
426+
if (PyDict_SetItemString(interp->sysdict, *p, Py_None) < 0) {
427+
PyErr_Clear();
428+
}
425429
}
426430
for (p = sys_files; *p != NULL; p+=2) {
427431
if (Py_VerboseFlag)
428432
PySys_WriteStderr("# restore sys.%s\n", *p);
429433
value = PyDict_GetItemString(interp->sysdict, *(p+1));
430434
if (value == NULL)
431435
value = Py_None;
432-
PyDict_SetItemString(interp->sysdict, *p, value);
436+
if (PyDict_SetItemString(interp->sysdict, *p, value) < 0) {
437+
PyErr_Clear();
438+
}
433439
}
434440

435441
/* We prepare a list which will receive (name, weakref) tuples of
@@ -443,21 +449,26 @@ PyImport_Cleanup(void)
443449
#define STORE_MODULE_WEAKREF(name, mod) \
444450
if (weaklist != NULL) { \
445451
PyObject *wr = PyWeakref_NewRef(mod, NULL); \
446-
if (name && wr) { \
452+
if (wr) { \
447453
PyObject *tup = PyTuple_Pack(2, name, wr); \
448-
PyList_Append(weaklist, tup); \
454+
if (!tup || PyList_Append(weaklist, tup) < 0) { \
455+
PyErr_Clear(); \
456+
} \
449457
Py_XDECREF(tup); \
458+
Py_DECREF(wr); \
450459
} \
451-
Py_XDECREF(wr); \
452-
if (PyErr_Occurred()) \
460+
else { \
453461
PyErr_Clear(); \
462+
} \
454463
}
455464
#define CLEAR_MODULE(name, mod) \
456465
if (PyModule_Check(mod)) { \
457466
if (Py_VerboseFlag && PyUnicode_Check(name)) \
458467
PySys_FormatStderr("# cleanup[2] removing %U\n", name); \
459468
STORE_MODULE_WEAKREF(name, mod); \
460-
PyObject_SetItem(modules, name, Py_None); \
469+
if (PyObject_SetItem(modules, name, Py_None) < 0) { \
470+
PyErr_Clear(); \
471+
} \
461472
}
462473

463474
/* Remove all modules from sys.modules, hoping that garbage collection
@@ -484,6 +495,9 @@ PyImport_Cleanup(void)
484495
Py_DECREF(value);
485496
Py_DECREF(key);
486497
}
498+
if (PyErr_Occurred()) {
499+
PyErr_Clear();
500+
}
487501
Py_DECREF(iterator);
488502
}
489503
}
@@ -564,6 +578,7 @@ PyImport_Cleanup(void)
564578
/* Once more */
565579
_PyGC_CollectNoFail();
566580

581+
#undef CLEAR_MODULE
567582
#undef STORE_MODULE_WEAKREF
568583
}
569584

0 commit comments

Comments
 (0)