@@ -417,14 +417,14 @@ PyImport_Cleanup(void)
417
417
if (Py_VerboseFlag )
418
418
PySys_WriteStderr ("# clear builtins._\n" );
419
419
if (PyDict_SetItemString (interp -> builtins , "_" , Py_None ) < 0 ) {
420
- PyErr_Clear ( );
420
+ PyErr_WriteUnraisable ( NULL );
421
421
}
422
422
423
423
for (p = sys_deletes ; * p != NULL ; p ++ ) {
424
424
if (Py_VerboseFlag )
425
425
PySys_WriteStderr ("# clear sys.%s\n" , * p );
426
426
if (PyDict_SetItemString (interp -> sysdict , * p , Py_None ) < 0 ) {
427
- PyErr_Clear ( );
427
+ PyErr_WriteUnraisable ( NULL );
428
428
}
429
429
}
430
430
for (p = sys_files ; * p != NULL ; p += 2 ) {
@@ -434,7 +434,7 @@ PyImport_Cleanup(void)
434
434
if (value == NULL )
435
435
value = Py_None ;
436
436
if (PyDict_SetItemString (interp -> sysdict , * p , value ) < 0 ) {
437
- PyErr_Clear ( );
437
+ PyErr_WriteUnraisable ( NULL );
438
438
}
439
439
}
440
440
@@ -443,22 +443,23 @@ PyImport_Cleanup(void)
443
443
for diagnosis messages (in verbose mode), while the weakref helps
444
444
detect those modules which have been held alive. */
445
445
weaklist = PyList_New (0 );
446
- if (weaklist == NULL )
447
- PyErr_Clear ();
446
+ if (weaklist == NULL ) {
447
+ PyErr_WriteUnraisable (NULL );
448
+ }
448
449
449
450
#define STORE_MODULE_WEAKREF (name , mod ) \
450
451
if (weaklist != NULL) { \
451
452
PyObject *wr = PyWeakref_NewRef(mod, NULL); \
452
453
if (wr) { \
453
454
PyObject *tup = PyTuple_Pack(2, name, wr); \
454
455
if (!tup || PyList_Append(weaklist, tup) < 0) { \
455
- PyErr_Clear( ); \
456
+ PyErr_WriteUnraisable(NULL ); \
456
457
} \
457
458
Py_XDECREF(tup); \
458
459
Py_DECREF(wr); \
459
460
} \
460
461
else { \
461
- PyErr_Clear( ); \
462
+ PyErr_WriteUnraisable(NULL ); \
462
463
} \
463
464
}
464
465
#define CLEAR_MODULE (name , mod ) \
@@ -467,7 +468,7 @@ PyImport_Cleanup(void)
467
468
PySys_FormatStderr("# cleanup[2] removing %U\n", name); \
468
469
STORE_MODULE_WEAKREF(name, mod); \
469
470
if (PyObject_SetItem(modules, name, Py_None) < 0) { \
470
- PyErr_Clear( ); \
471
+ PyErr_WriteUnraisable(NULL ); \
471
472
} \
472
473
}
473
474
@@ -482,21 +483,21 @@ PyImport_Cleanup(void)
482
483
else {
483
484
PyObject * iterator = PyObject_GetIter (modules );
484
485
if (iterator == NULL ) {
485
- PyErr_Clear ( );
486
+ PyErr_WriteUnraisable ( NULL );
486
487
}
487
488
else {
488
489
while ((key = PyIter_Next (iterator ))) {
489
490
value = PyObject_GetItem (modules , key );
490
491
if (value == NULL ) {
491
- PyErr_Clear ( );
492
+ PyErr_WriteUnraisable ( NULL );
492
493
continue ;
493
494
}
494
495
CLEAR_MODULE (key , value );
495
496
Py_DECREF (value );
496
497
Py_DECREF (key );
497
498
}
498
499
if (PyErr_Occurred ()) {
499
- PyErr_Clear ( );
500
+ PyErr_WriteUnraisable ( NULL );
500
501
}
501
502
Py_DECREF (iterator );
502
503
}
@@ -508,17 +509,20 @@ PyImport_Cleanup(void)
508
509
}
509
510
else {
510
511
_Py_IDENTIFIER (clear );
511
- if (_PyObject_CallMethodId (modules , & PyId_clear , "" ) == NULL )
512
- PyErr_Clear ();
512
+ if (_PyObject_CallMethodId (modules , & PyId_clear , "" ) == NULL ) {
513
+ PyErr_WriteUnraisable (NULL );
514
+ }
513
515
}
514
516
/* Restore the original builtins dict, to ensure that any
515
517
user data gets cleared. */
516
518
dict = PyDict_Copy (interp -> builtins );
517
- if (dict == NULL )
518
- PyErr_Clear ();
519
+ if (dict == NULL ) {
520
+ PyErr_WriteUnraisable (NULL );
521
+ }
519
522
PyDict_Clear (interp -> builtins );
520
- if (PyDict_Update (interp -> builtins , interp -> builtins_copy ))
523
+ if (PyDict_Update (interp -> builtins , interp -> builtins_copy )) {
521
524
PyErr_Clear ();
525
+ }
522
526
Py_XDECREF (dict );
523
527
/* Clear module dict copies stored in the interpreter state */
524
528
_PyState_ClearModules ();
0 commit comments