Issue #477863: Print a warning at shutdown if gc.garbage is not empty.

This commit is contained in:
Antoine Pitrou 2010-08-08 22:18:46 +00:00
parent 2e5f1178ac
commit 696e03553b
7 changed files with 99 additions and 10 deletions

View file

@ -1295,17 +1295,16 @@ static PyMethodDef GcMethods[] = {
static struct PyModuleDef gcmodule = {
PyModuleDef_HEAD_INIT,
"gc",
gc__doc__,
-1,
GcMethods,
NULL,
NULL,
NULL,
NULL
"gc", /* m_name */
gc__doc__, /* m_doc */
-1, /* m_size */
GcMethods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL /* m_free */
};
PyMODINIT_FUNC
PyInit_gc(void)
{
@ -1364,6 +1363,37 @@ PyGC_Collect(void)
return n;
}
void
_PyGC_Fini(void)
{
if (garbage != NULL && PyList_GET_SIZE(garbage) > 0) {
PySys_WriteStderr(
"gc: "
"%" PY_FORMAT_SIZE_T "d uncollectable objects at shutdown:\n",
PyList_GET_SIZE(garbage)
);
if (debug & DEBUG_UNCOLLECTABLE) {
PyObject *repr = NULL, *bytes = NULL;
repr = PyObject_Repr(garbage);
if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr)))
PyErr_WriteUnraisable(garbage);
else {
PySys_WriteStderr(
" %s\n",
PyBytes_AS_STRING(bytes)
);
}
Py_XDECREF(repr);
Py_XDECREF(bytes);
}
else {
PySys_WriteStderr(
" Use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them.\n"
);
}
}
}
/* for debugging */
void
_PyGC_Dump(PyGC_Head *g)