mirror of
https://github.com/python/cpython.git
synced 2025-08-30 05:35:08 +00:00
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:
parent
9cbdd75ec5
commit
e033e06db0
10 changed files with 189 additions and 25 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue