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.
diff --git a/Python/_warnings.c b/Python/_warnings.c
index a4e9d48..87755e1 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -835,6 +835,7 @@
static PyObject *ignore_str = NULL;
static PyObject *error_str = NULL;
static PyObject *default_str = NULL;
+ static PyObject *always_str = NULL;
PyObject *action_obj = NULL;
PyObject *lineno, *result;
@@ -862,6 +863,14 @@
}
action_obj = default_str;
}
+ else if (!strcmp(action, "always")) {
+ if (always_str == NULL) {
+ always_str = PyUnicode_InternFromString("always");
+ if (always_str == NULL)
+ return NULL;
+ }
+ action_obj = always_str;
+ }
else {
Py_FatalError("unknown action");
}
@@ -879,10 +888,10 @@
init_filters(void)
{
/* Don't silence DeprecationWarning if -3 was used. */
- PyObject *filters = PyList_New(4);
+ PyObject *filters = PyList_New(5);
unsigned int pos = 0; /* Post-incremented in each use. */
unsigned int x;
- const char *bytes_action;
+ const char *bytes_action, *resource_action;
if (filters == NULL)
return NULL;
@@ -901,7 +910,14 @@
bytes_action = "ignore";
PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
bytes_action));
-
+ /* resource usage warnings are enabled by default in pydebug mode */
+#ifdef Py_DEBUG
+ resource_action = "always";
+#else
+ resource_action = "ignore";
+#endif
+ PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
+ resource_action));
for (x = 0; x < pos; x += 1) {
if (PyList_GET_ITEM(filters, x) == NULL) {
Py_DECREF(filters);
diff --git a/Python/errors.c b/Python/errors.c
index e3486b9..0490614 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -767,8 +767,10 @@
}
Py_XDECREF(moduleName);
}
- PyFile_WriteString(" in ", f);
- PyFile_WriteObject(obj, f, 0);
+ if (obj) {
+ PyFile_WriteString(" in ", f);
+ PyFile_WriteObject(obj, f, 0);
+ }
PyFile_WriteString(" ignored\n", f);
PyErr_Clear(); /* Just in case */
}