]> granicus.if.org Git - python/commitdiff
Issue #8524: Add a forget() method to socket objects, so as to put the
authorAntoine Pitrou <solipsis@pitrou.net>
Sun, 8 Aug 2010 23:24:50 +0000 (23:24 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Sun, 8 Aug 2010 23:24:50 +0000 (23:24 +0000)
socket into the closed state without closing the underlying file
descriptor.

Doc/library/socket.rst
Doc/whatsnew/3.2.rst
Lib/ssl.py
Lib/test/test_socket.py
Misc/NEWS
Modules/socketmodule.c

index 75d86b7cf7100256c8213a6a9aa844dbcf1a5355..2b43266ae2daf1166a8e092d6162d06b3be0d8e2 100644 (file)
@@ -548,6 +548,14 @@ correspond to Unix system calls applicable to sockets.
    this limitation.
 
 
+.. method:: socket.forget()
+
+   Put the socket object into closed state without actually closing the
+   underlying file descriptor.  This allows the latter to be reused.
+
+   .. versionadded:: 3.2
+
+
 .. method:: socket.getpeername()
 
    Return the remote address to which the socket is connected.  This is useful to
index f4802a0bf7bbb048b308482c3f449fde80efaf71..487298fb68936c1e298faa0bac9870abed22de12 100644 (file)
@@ -136,6 +136,12 @@ New, Improved, and Deprecated Modules
 
   (Contributed by Tarek Ziadé.)
 
+* Socket objects now have a :meth:`~socket.socket.forget()` method which
+  puts the socket into closed state without actually closing the underlying
+  file descriptor.  The latter can then be reused for other purposes.
+
+  (Added by Antoine Pitrou; :issue:`8524`.)
+
 * The *sqlite3* module has some new features:
 
   * XXX *enable_load_extension*
index 585105d1cc4f00df2e5abc115c148c95b57298d2..7bcc67e9a2707e867029dddc46003498e91e51a4 100644 (file)
@@ -79,7 +79,6 @@ from _ssl import (
 
 from socket import getnameinfo as _getnameinfo
 from socket import error as socket_error
-from socket import dup as _dup
 from socket import socket, AF_INET, SOCK_STREAM
 import base64        # for DER-to-PEM translation
 import traceback
@@ -148,7 +147,7 @@ class SSLSocket(socket):
                             family=sock.family,
                             type=sock.type,
                             proto=sock.proto,
-                            fileno=_dup(sock.fileno()))
+                            fileno=sock.fileno())
             self.settimeout(sock.gettimeout())
             # see if it's connected
             try:
@@ -158,7 +157,7 @@ class SSLSocket(socket):
                     raise
             else:
                 connected = True
-            sock.close()
+            sock.forget()
         elif fileno is not None:
             socket.__init__(self, fileno=fileno)
         else:
index 25025ddc3ed3307fb21cef02ef3ec7b86bbe2189..ae34c11492390544b6fbc3efa6bf3434dcb2772f 100644 (file)
@@ -655,6 +655,19 @@ class BasicTCPTest(SocketConnectedTest):
         self.serv_conn.send(MSG)
         self.serv_conn.shutdown(2)
 
+    def testForget(self):
+        # Testing forget()
+        f = self.cli_conn.fileno()
+        self.cli_conn.forget()
+        self.assertRaises(socket.error, self.cli_conn.recv, 1024)
+        self.cli_conn.close()
+        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, fileno=f)
+        msg = sock.recv(1024)
+        self.assertEqual(msg, MSG)
+
+    def _testForget(self):
+        self.serv_conn.send(MSG)
+
 @unittest.skipUnless(thread, 'Threading required for this test.')
 class BasicUDPTest(ThreadedUDPSocketTest):
 
index 46ddfef855c2cfca2e348c51e8aa4750d4cf8388..00188d4f946208a6e8cf8138d1f9be1d43bd3e1c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -30,6 +30,10 @@ Core and Builtins
 Extensions
 ----------
 
+- Issue #8524: Add a forget() method to socket objects, so as to put the
+  socket into the closed state without closing the underlying file
+  descriptor.
+
 - Issue #477863: Print a warning at shutdown if gc.garbage is not empty.
 
 - Issue #6869: Fix a refcount problem in the _ctypes extension.
index 563bdeaa114b0e1dd9180790133fdcfb50489934..fc671e06648a1bf6042ebcdea02a22d451f5342b 100644 (file)
@@ -1869,6 +1869,21 @@ PyDoc_STRVAR(close_doc,
 \n\
 Close the socket.  It cannot be used after this call.");
 
+static PyObject *
+sock_forget(PySocketSockObject *s)
+{
+    s->sock_fd = -1;
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+PyDoc_STRVAR(forget_doc,
+"forget()\n\
+\n\
+Close the socket object without closing the underlying file descriptor.\
+The object cannot be used after this call, but the file descriptor\
+can be reused for other purposes.");
+
 static int
 internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
                  int *timeoutp)
@@ -2759,6 +2774,8 @@ static PyMethodDef sock_methods[] = {
                       connect_ex_doc},
     {"fileno",            (PyCFunction)sock_fileno, METH_NOARGS,
                       fileno_doc},
+    {"forget",            (PyCFunction)sock_forget, METH_NOARGS,
+                      forget_doc},
 #ifdef HAVE_GETPEERNAME
     {"getpeername",       (PyCFunction)sock_getpeername,
                       METH_NOARGS, getpeername_doc},