[Python-Dev] sock.close() not closing?
Amaury Forgeot d'Arc
amauryfa at gmail.com
Wed May 7 13:37:31 CEST 2008
Hello,
2008/5/7 Sjoerd Mullender <sjoerd at acm.org>:
> Why does sock.close() not actually close sock?
>
> If I run the code
>
> import socket
> sock = socket.socket()
> ...
> sock.close()
>
> I would expect that a system call is done to actually close the socket and
> free the file descriptor. But that does not happen. Look at the code in
> socket.py. It merely replaces the socket instance with a dummy instance so
> that all subsequent calls on the sock object fail, but it does nothing else!
It does close the socket:
In socket.py, when self._sock is replaced, its __del__ method will be called.
This __del__ is implemented in C, in socketmodule.c:
static void
sock_dealloc(PySocketSockObject *s)
{
if (s->sock_fd != -1)
(void) SOCKETCLOSE(s->sock_fd);
Py_TYPE(s)->tp_free((PyObject *)s);
}
Of course, if you call sock.dup() or sock.makefile(),
there is another reference to the underlying _sock, and you must
close() all these objects.
--
Amaury Forgeot d'Arc
More information about the Python-Dev
mailing list