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

Skip to content

Commit 9e0b864

Browse files
committed
Issue #1552: socket.socketpair() now returns regular socket.socket
objects supporting the whole socket API (rather than the "raw" _socket.socket objects).
1 parent 3861599 commit 9e0b864

4 files changed

Lines changed: 47 additions & 0 deletions

File tree

Doc/library/socket.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ The module :mod:`socket` exports the following constants and functions:
364364
if defined on the platform; otherwise, the default is :const:`AF_INET`.
365365
Availability: Unix.
366366

367+
.. versionchanged:: 3.2
368+
The returned socket objects now support the whole socket API, rather
369+
than a subset.
370+
367371

368372
.. function:: fromfd(fd, family, type[, proto])
369373

Lib/socket.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,27 @@ def fromfd(fd, family, type, proto=0):
199199
return socket(family, type, proto, nfd)
200200

201201

202+
if hasattr(_socket, "socketpair"):
203+
204+
def socketpair(family=None, type=SOCK_STREAM, proto=0):
205+
"""socketpair([family[, type[, proto]]]) -> (socket object, socket object)
206+
207+
Create a pair of socket objects from the sockets returned by the platform
208+
socketpair() function.
209+
The arguments are the same as for socket() except the default family is
210+
AF_UNIX if defined on the platform; otherwise, the default is AF_INET.
211+
"""
212+
if family is None:
213+
try:
214+
family = AF_UNIX
215+
except NameError:
216+
family = AF_INET
217+
a, b = _socket.socketpair(family, type, proto)
218+
a = socket(family, type, proto, a.detach())
219+
b = socket(family, type, proto, b.detach())
220+
return a, b
221+
222+
202223
class SocketIO(io.RawIOBase):
203224

204225
"""Raw I/O implementation for stream sockets.

Lib/test/test_socket.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@ def testFromFd(self):
714714
# Testing fromfd()
715715
fd = self.cli_conn.fileno()
716716
sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
717+
self.assertIsInstance(sock, socket.socket)
717718
msg = sock.recv(1024)
718719
self.assertEqual(msg, MSG)
719720

@@ -814,6 +815,23 @@ class BasicSocketPairTest(SocketPairTest):
814815
def __init__(self, methodName='runTest'):
815816
SocketPairTest.__init__(self, methodName=methodName)
816817

818+
def _testDefaults(self):
819+
pass
820+
821+
def testDefaults(self):
822+
self.assertIsInstance(self.cli, socket.socket)
823+
self.assertIsInstance(self.serv, socket.socket)
824+
if hasattr(socket, 'AF_UNIX'):
825+
self.assertEqual(self.cli.family, socket.AF_UNIX)
826+
self.assertEqual(self.serv.family, socket.AF_UNIX)
827+
else:
828+
self.assertEqual(self.cli.family, socket.AF_INET)
829+
self.assertEqual(self.serv.family, socket.AF_INET)
830+
self.assertEqual(self.cli.type, socket.SOCK_STREAM)
831+
self.assertEqual(self.serv.type, socket.SOCK_STREAM)
832+
self.assertEqual(self.cli.proto, 0)
833+
self.assertEqual(self.serv.proto, 0)
834+
817835
def testRecv(self):
818836
msg = self.serv.recv(1024)
819837
self.assertEqual(msg, MSG)

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ Core and Builtins
5252
Library
5353
-------
5454

55+
- Issue #1552: socket.socketpair() now returns regular socket.socket
56+
objects supporting the whole socket API (rather than the "raw"
57+
_socket.socket objects).
58+
5559
- Issue #9853: Fix the signature of SSLSocket.recvfrom() and
5660
SSLSocket.sendto() to match the corresponding socket methods.
5761

0 commit comments

Comments
 (0)