[Python-checkins] r83869 - in python/branches/py3k: Doc/library/socket.rst Doc/whatsnew/3.2.rst Lib/ssl.py Lib/test/test_socket.py Misc/NEWS Modules/socketmodule.c

antoine.pitrou python-checkins at python.org
Mon Aug 9 01:24:50 CEST 2010


Author: antoine.pitrou
Date: Mon Aug  9 01:24:50 2010
New Revision: 83869

Log:
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.




Modified:
   python/branches/py3k/Doc/library/socket.rst
   python/branches/py3k/Doc/whatsnew/3.2.rst
   python/branches/py3k/Lib/ssl.py
   python/branches/py3k/Lib/test/test_socket.py
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Modules/socketmodule.c

Modified: python/branches/py3k/Doc/library/socket.rst
==============================================================================
--- python/branches/py3k/Doc/library/socket.rst	(original)
+++ python/branches/py3k/Doc/library/socket.rst	Mon Aug  9 01:24:50 2010
@@ -548,6 +548,14 @@
    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

Modified: python/branches/py3k/Doc/whatsnew/3.2.rst
==============================================================================
--- python/branches/py3k/Doc/whatsnew/3.2.rst	(original)
+++ python/branches/py3k/Doc/whatsnew/3.2.rst	Mon Aug  9 01:24:50 2010
@@ -136,6 +136,12 @@
 
   (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*

Modified: python/branches/py3k/Lib/ssl.py
==============================================================================
--- python/branches/py3k/Lib/ssl.py	(original)
+++ python/branches/py3k/Lib/ssl.py	Mon Aug  9 01:24:50 2010
@@ -79,7 +79,6 @@
 
 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 @@
                             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 @@
                     raise
             else:
                 connected = True
-            sock.close()
+            sock.forget()
         elif fileno is not None:
             socket.__init__(self, fileno=fileno)
         else:

Modified: python/branches/py3k/Lib/test/test_socket.py
==============================================================================
--- python/branches/py3k/Lib/test/test_socket.py	(original)
+++ python/branches/py3k/Lib/test/test_socket.py	Mon Aug  9 01:24:50 2010
@@ -655,6 +655,19 @@
         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):
 

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Mon Aug  9 01:24:50 2010
@@ -30,6 +30,10 @@
 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.

Modified: python/branches/py3k/Modules/socketmodule.c
==============================================================================
--- python/branches/py3k/Modules/socketmodule.c	(original)
+++ python/branches/py3k/Modules/socketmodule.c	Mon Aug  9 01:24:50 2010
@@ -1869,6 +1869,21 @@
 \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 @@
                       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},


More information about the Python-checkins mailing list