@@ -354,20 +354,26 @@ PyImport_Cleanup(void)
354354
355355 if (Py_VerboseFlag )
356356 PySys_WriteStderr ("# clear builtins._\n" );
357- PyDict_SetItemString (interp -> builtins , "_" , Py_None );
357+ if (PyDict_SetItemString (interp -> builtins , "_" , Py_None ) < 0 ) {
358+ PyErr_Clear ();
359+ }
358360
359361 for (p = sys_deletes ; * p != NULL ; p ++ ) {
360362 if (Py_VerboseFlag )
361363 PySys_WriteStderr ("# clear sys.%s\n" , * p );
362- PyDict_SetItemString (interp -> sysdict , * p , Py_None );
364+ if (PyDict_SetItemString (interp -> sysdict , * p , Py_None ) < 0 ) {
365+ PyErr_Clear ();
366+ }
363367 }
364368 for (p = sys_files ; * p != NULL ; p += 2 ) {
365369 if (Py_VerboseFlag )
366370 PySys_WriteStderr ("# restore sys.%s\n" , * p );
367371 value = PyDict_GetItemString (interp -> sysdict , * (p + 1 ));
368372 if (value == NULL )
369373 value = Py_None ;
370- PyDict_SetItemString (interp -> sysdict , * p , value );
374+ if (PyDict_SetItemString (interp -> sysdict , * p , value ) < 0 ) {
375+ PyErr_Clear ();
376+ }
371377 }
372378
373379 /* We prepare a list which will receive (name, weakref) tuples of
@@ -381,14 +387,17 @@ PyImport_Cleanup(void)
381387#define STORE_MODULE_WEAKREF (name , mod ) \
382388 if (weaklist != NULL) { \
383389 PyObject *wr = PyWeakref_NewRef(mod, NULL); \
384- if (name && wr) { \
390+ if (wr) { \
385391 PyObject *tup = PyTuple_Pack(2, name, wr); \
386- PyList_Append(weaklist, tup); \
392+ if (!tup || PyList_Append(weaklist, tup) < 0) { \
393+ PyErr_Clear(); \
394+ } \
387395 Py_XDECREF(tup); \
396+ Py_DECREF(wr); \
388397 } \
389- Py_XDECREF(wr); \
390- if (PyErr_Occurred()) \
398+ else { \
391399 PyErr_Clear(); \
400+ } \
392401 }
393402
394403 /* Remove all modules from sys.modules, hoping that garbage collection
@@ -399,7 +408,9 @@ PyImport_Cleanup(void)
399408 if (Py_VerboseFlag && PyUnicode_Check (key ))
400409 PySys_FormatStderr ("# cleanup[2] removing %U\n" , key );
401410 STORE_MODULE_WEAKREF (key , value );
402- PyDict_SetItem (modules , key , Py_None );
411+ if (PyDict_SetItem (modules , key , Py_None ) < 0 ) {
412+ PyErr_Clear ();
413+ }
403414 }
404415 }
405416
@@ -472,6 +483,7 @@ PyImport_Cleanup(void)
472483 /* Once more */
473484 _PyGC_CollectNoFail ();
474485
486+ #undef CLEAR_MODULE
475487#undef STORE_MODULE_WEAKREF
476488}
477489
0 commit comments