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/Modules/gcmodule.c b/Modules/gcmodule.c
index a95bec7..3f96c42 100644
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -1368,11 +1368,16 @@
 {
     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 @@
             Py_XDECREF(repr);
             Py_XDECREF(bytes);
         }
-        else {
-            PySys_WriteStderr(
-                "    Use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them.\n"
-                );
-        }
     }
 }