Add a new warning gategory, ResourceWarning, as discussed on python-dev. It is silent by default,

except when configured --with-pydebug.

Emit this warning from the GC shutdown procedure, rather than just printing to stderr.
This commit is contained in:
Georg Brandl 2010-10-24 15:11:22 +00:00
parent 872a702bbd
commit 08be72d0aa
12 changed files with 80 additions and 30 deletions

View file

@ -1368,11 +1368,16 @@ _PyGC_Fini(void)
{
if (!(debug & DEBUG_SAVEALL)
&& garbage != NULL && PyList_GET_SIZE(garbage) > 0) {
PySys_WriteStderr(
"gc: "
"%" PY_FORMAT_SIZE_T "d uncollectable objects at shutdown:\n",
PyList_GET_SIZE(garbage)
);
char *message;
if (debug & DEBUG_UNCOLLECTABLE)
message = "gc: %" PY_FORMAT_SIZE_T "d uncollectable objects at " \
"shutdown";
else
message = "gc: %" PY_FORMAT_SIZE_T "d uncollectable objects at " \
"shutdown; use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them";
if (PyErr_WarnFormat(PyExc_ResourceWarning, 0, message,
PyList_GET_SIZE(garbage)) < 0)
PyErr_WriteUnraisable(NULL);
if (debug & DEBUG_UNCOLLECTABLE) {
PyObject *repr = NULL, *bytes = NULL;
repr = PyObject_Repr(garbage);
@ -1387,11 +1392,6 @@ _PyGC_Fini(void)
Py_XDECREF(repr);
Py_XDECREF(bytes);
}
else {
PySys_WriteStderr(
" Use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them.\n"
);
}
}
}