diff Modules/socketmodule.c @ 65707:cff78ffb932a

Issue #10093: ResourceWarnings are now issued when files and sockets are deallocated without explicit closing. These warnings are silenced by default, except in pydebug mode.
author Antoine Pitrou <solipsis@pitrou.net>
date Fri, 29 Oct 2010 10:38:18 +0000
parents 214b9051841e
children a4fd3dc74299
line wrap: on
line diff
--- a/Modules/socketmodule.c	Fri Oct 29 09:08:19 2010 +0000
+++ b/Modules/socketmodule.c	Fri Oct 29 10:38:18 2010 +0000
@@ -2941,8 +2941,20 @@
 static void
 sock_dealloc(PySocketSockObject *s)
 {
-    if (s->sock_fd != -1)
+    if (s->sock_fd != -1) {
+        PyObject *exc, *val, *tb;
+        Py_ssize_t old_refcount = Py_REFCNT(s);
+        ++Py_REFCNT(s);
+        PyErr_Fetch(&exc, &val, &tb);
+        if (PyErr_WarnFormat(PyExc_ResourceWarning, 1,
+                             "unclosed %R", s))
+            /* Spurious errors can appear at shutdown */
+            if (PyErr_ExceptionMatches(PyExc_Warning))
+                PyErr_WriteUnraisable((PyObject *) s);
+        PyErr_Restore(exc, val, tb);
         (void) SOCKETCLOSE(s->sock_fd);
+        Py_REFCNT(s) = old_refcount;
+    }
     Py_TYPE(s)->tp_free((PyObject *)s);
 }