[Python-checkins] cpython (merge 3.2 -> default): Issue #13872: socket.detach() now marks the socket closed (as mirrored in the

antoine.pitrou python-checkins at python.org
Sun Apr 1 01:08:02 CEST 2012


http://hg.python.org/cpython/rev/d2f0c3eb1eed
changeset:   76034:d2f0c3eb1eed
parent:      76031:9a5ae3f37d06
parent:      76033:3a220feafa15
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sun Apr 01 01:00:55 2012 +0200
summary:
  Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Patch by Matt Joiner.

files:
  Lib/socket.py           |  11 +++++++++++
  Lib/test/test_socket.py |   1 +
  Misc/NEWS               |   3 +++
  3 files changed, 15 insertions(+), 0 deletions(-)


diff --git a/Lib/socket.py b/Lib/socket.py
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -199,6 +199,17 @@
         if self._io_refs <= 0:
             self._real_close()
 
+    def detach(self):
+        """detach() -> file descriptor
+
+        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.  The file descriptor is returned.
+        """
+        self._closed = True
+        return super().detach()
+
+
 def fromfd(fd, family, type, proto=0):
     """ fromfd(fd, family, type[, proto]) -> socket object
 
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -1574,6 +1574,7 @@
         f = self.cli_conn.detach()
         self.assertEqual(f, fileno)
         # cli_conn cannot be used anymore...
+        self.assertTrue(self.cli_conn._closed)
         self.assertRaises(socket.error, self.cli_conn.recv, 1024)
         self.cli_conn.close()
         # ...but we can create another socket using the (still open)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -37,6 +37,9 @@
 Library
 -------
 
+- Issue #13872: socket.detach() now marks the socket closed (as mirrored
+  in the socket repr()).  Patch by Matt Joiner.
+
 - Issue #14406: Fix a race condition when using ``concurrent.futures.wait(
   return_when=ALL_COMPLETED)``.  Patch by Matt Joiner.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list