diff --git a/Include/objimpl.h b/Include/objimpl.h --- a/Include/objimpl.h +++ b/Include/objimpl.h @@ -232,6 +232,10 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewV /* C equivalent of gc.collect(). */ PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void); +#ifndef Py_LIMITED_API +PyAPI_FUNC(Py_ssize_t) _PyGC_CollectHard(void); +#endif + /* Test if a type has a GC head */ #define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC) diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c --- a/Modules/gcmodule.c +++ b/Modules/gcmodule.c @@ -850,7 +850,8 @@ get_time(void) /* This is the main function. Read this to understand how the * collection process works. */ static Py_ssize_t -collect(int generation, Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable) +collect(int generation, Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable, + int hard) { int i; Py_ssize_t m = 0; /* # objects collected */ @@ -997,10 +998,14 @@ collect(int generation, Py_ssize_t *n_co } if (PyErr_Occurred()) { - if (gc_str == NULL) - gc_str = PyUnicode_FromString("garbage collection"); - PyErr_WriteUnraisable(gc_str); - Py_FatalError("unexpected exception during garbage collection"); + if (hard) + PyErr_Clear(); + else { + if (gc_str == NULL) + gc_str = PyUnicode_FromString("garbage collection"); + PyErr_WriteUnraisable(gc_str); + Py_FatalError("unexpected exception during garbage collection"); + } } /* Update stats */ @@ -1059,7 +1064,7 @@ collect_with_callback(int generation) { Py_ssize_t result, collected, uncollectable; invoke_gc_callback("start", generation, 0, 0); - result = collect(generation, &collected, &uncollectable); + result = collect(generation, &collected, &uncollectable, 0); invoke_gc_callback("stop", generation, collected, uncollectable); return result; } @@ -1541,6 +1546,22 @@ PyGC_Collect(void) return n; } +Py_ssize_t +_PyGC_CollectHard(void) +{ + Py_ssize_t n; + + if (collecting) + n = 0; /* already collecting, don't do anything */ + else { + Py_ssize_t collected, uncollectable; + collecting = 1; + n = collect(NUM_GENERATIONS - 1, NULL, NULL, 1); + collecting = 0; + } + + return n; +} void _PyGC_DumpShutdownStats(void) diff --git a/Python/import.c b/Python/import.c --- a/Python/import.c +++ b/Python/import.c @@ -444,6 +444,7 @@ PyImport_Cleanup(void) /* Finally, clear and delete the modules directory */ PyDict_Clear(modules); + _PyGC_CollectHard(); interp->modules = NULL; Py_DECREF(modules); }