Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 70deb3d

Browse files
committed
Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Patch by Matt Joiner.
1 parent 6211b88 commit 70deb3d

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

Lib/socket.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ def close(self):
197197
if self._io_refs <= 0:
198198
self._real_close()
199199

200+
def detach(self):
201+
"""detach() -> file descriptor
202+
203+
Close the socket object without closing the underlying file descriptor.
204+
The object cannot be used after this call, but the file descriptor
205+
can be reused for other purposes. The file descriptor is returned.
206+
"""
207+
self._closed = True
208+
return super().detach()
209+
210+
200211
def fromfd(fd, family, type, proto=0):
201212
""" fromfd(fd, family, type[, proto]) -> socket object
202213

Lib/test/test_socket.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ def testDetach(self):
951951
f = self.cli_conn.detach()
952952
self.assertEqual(f, fileno)
953953
# cli_conn cannot be used anymore...
954+
self.assertTrue(self.cli_conn._closed)
954955
self.assertRaises(socket.error, self.cli_conn.recv, 1024)
955956
self.cli_conn.close()
956957
# ...but we can create another socket using the (still open)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ Core and Builtins
3131
Library
3232
-------
3333

34+
- Issue #13872: socket.detach() now marks the socket closed (as mirrored
35+
in the socket repr()). Patch by Matt Joiner.
36+
3437
- Issue #14406: Fix a race condition when using ``concurrent.futures.wait(
3538
return_when=ALL_COMPLETED)``. Patch by Matt Joiner.
3639

0 commit comments

Comments
 (0)