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.
This commit is contained in:
Antoine Pitrou 2010-10-29 10:38:18 +00:00
parent 9cbdd75ec5
commit e033e06db0
10 changed files with 189 additions and 25 deletions

View file

@ -2941,8 +2941,20 @@ static PyMemberDef sock_memberlist[] = {
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);
}