@@ -364,7 +364,7 @@ PyImport_GetModule(PyObject *name)
364
364
}
365
365
else {
366
366
m = PyObject_GetItem (modules , name );
367
- if (PyErr_ExceptionMatches (PyExc_KeyError )) {
367
+ if (m == NULL && PyErr_ExceptionMatches (PyExc_KeyError )) {
368
368
PyErr_Clear ();
369
369
}
370
370
}
@@ -416,20 +416,26 @@ PyImport_Cleanup(void)
416
416
417
417
if (Py_VerboseFlag )
418
418
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
+ }
420
422
421
423
for (p = sys_deletes ; * p != NULL ; p ++ ) {
422
424
if (Py_VerboseFlag )
423
425
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
+ }
425
429
}
426
430
for (p = sys_files ; * p != NULL ; p += 2 ) {
427
431
if (Py_VerboseFlag )
428
432
PySys_WriteStderr ("# restore sys.%s\n" , * p );
429
433
value = PyDict_GetItemString (interp -> sysdict , * (p + 1 ));
430
434
if (value == NULL )
431
435
value = Py_None ;
432
- PyDict_SetItemString (interp -> sysdict , * p , value );
436
+ if (PyDict_SetItemString (interp -> sysdict , * p , value ) < 0 ) {
437
+ PyErr_Clear ();
438
+ }
433
439
}
434
440
435
441
/* We prepare a list which will receive (name, weakref) tuples of
@@ -443,21 +449,26 @@ PyImport_Cleanup(void)
443
449
#define STORE_MODULE_WEAKREF (name , mod ) \
444
450
if (weaklist != NULL) { \
445
451
PyObject *wr = PyWeakref_NewRef(mod, NULL); \
446
- if (name && wr) { \
452
+ if (wr) { \
447
453
PyObject *tup = PyTuple_Pack(2, name, wr); \
448
- PyList_Append(weaklist, tup); \
454
+ if (!tup || PyList_Append(weaklist, tup) < 0) { \
455
+ PyErr_Clear(); \
456
+ } \
449
457
Py_XDECREF(tup); \
458
+ Py_DECREF(wr); \
450
459
} \
451
- Py_XDECREF(wr); \
452
- if (PyErr_Occurred()) \
460
+ else { \
453
461
PyErr_Clear(); \
462
+ } \
454
463
}
455
464
#define CLEAR_MODULE (name , mod ) \
456
465
if (PyModule_Check(mod)) { \
457
466
if (Py_VerboseFlag && PyUnicode_Check(name)) \
458
467
PySys_FormatStderr("# cleanup[2] removing %U\n", name); \
459
468
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
+ } \
461
472
}
462
473
463
474
/* Remove all modules from sys.modules, hoping that garbage collection
@@ -484,6 +495,9 @@ PyImport_Cleanup(void)
484
495
Py_DECREF (value );
485
496
Py_DECREF (key );
486
497
}
498
+ if (PyErr_Occurred ()) {
499
+ PyErr_Clear ();
500
+ }
487
501
Py_DECREF (iterator );
488
502
}
489
503
}
@@ -564,6 +578,7 @@ PyImport_Cleanup(void)
564
578
/* Once more */
565
579
_PyGC_CollectNoFail ();
566
580
581
+ #undef CLEAR_MODULE
567
582
#undef STORE_MODULE_WEAKREF
568
583
}
569
584
0 commit comments