Hello, 2008/5/7 Sjoerd Mullender <[EMAIL PROTECTED]>: > 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 _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com