@@ -354,20 +354,26 @@ PyImport_Cleanup(void)
354
354
355
355
if (Py_VerboseFlag )
356
356
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
+ }
358
360
359
361
for (p = sys_deletes ; * p != NULL ; p ++ ) {
360
362
if (Py_VerboseFlag )
361
363
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
+ }
363
367
}
364
368
for (p = sys_files ; * p != NULL ; p += 2 ) {
365
369
if (Py_VerboseFlag )
366
370
PySys_WriteStderr ("# restore sys.%s\n" , * p );
367
371
value = PyDict_GetItemString (interp -> sysdict , * (p + 1 ));
368
372
if (value == NULL )
369
373
value = Py_None ;
370
- PyDict_SetItemString (interp -> sysdict , * p , value );
374
+ if (PyDict_SetItemString (interp -> sysdict , * p , value ) < 0 ) {
375
+ PyErr_Clear ();
376
+ }
371
377
}
372
378
373
379
/* We prepare a list which will receive (name, weakref) tuples of
@@ -381,14 +387,17 @@ PyImport_Cleanup(void)
381
387
#define STORE_MODULE_WEAKREF (name , mod ) \
382
388
if (weaklist != NULL) { \
383
389
PyObject *wr = PyWeakref_NewRef(mod, NULL); \
384
- if (name && wr) { \
390
+ if (wr) { \
385
391
PyObject *tup = PyTuple_Pack(2, name, wr); \
386
- PyList_Append(weaklist, tup); \
392
+ if (!tup || PyList_Append(weaklist, tup) < 0) { \
393
+ PyErr_Clear(); \
394
+ } \
387
395
Py_XDECREF(tup); \
396
+ Py_DECREF(wr); \
388
397
} \
389
- Py_XDECREF(wr); \
390
- if (PyErr_Occurred()) \
398
+ else { \
391
399
PyErr_Clear(); \
400
+ } \
392
401
}
393
402
394
403
/* Remove all modules from sys.modules, hoping that garbage collection
@@ -399,7 +408,9 @@ PyImport_Cleanup(void)
399
408
if (Py_VerboseFlag && PyUnicode_Check (key ))
400
409
PySys_FormatStderr ("# cleanup[2] removing %U\n" , key );
401
410
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
+ }
403
414
}
404
415
}
405
416
@@ -472,6 +483,7 @@ PyImport_Cleanup(void)
472
483
/* Once more */
473
484
_PyGC_CollectNoFail ();
474
485
486
+ #undef CLEAR_MODULE
475
487
#undef STORE_MODULE_WEAKREF
476
488
}
477
489
0 commit comments