mirror of
https://github.com/python/cpython.git
synced 2025-07-31 23:23:11 +00:00
Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
writing into objects supporting the new buffer API, for example bytearrays or memoryviews.
This commit is contained in:
parent
2227251a4e
commit
d7b731d160
3 changed files with 73 additions and 21 deletions
|
@ -1226,28 +1226,64 @@ class BufferIOTest(SocketConnectedTest):
|
|||
def __init__(self, methodName='runTest'):
|
||||
SocketConnectedTest.__init__(self, methodName=methodName)
|
||||
|
||||
def testRecvInto(self):
|
||||
def testRecvIntoArray(self):
|
||||
buf = array.array('c', ' '*1024)
|
||||
nbytes = self.cli_conn.recv_into(buf)
|
||||
self.assertEqual(nbytes, len(MSG))
|
||||
msg = buf.tostring()[:len(MSG)]
|
||||
self.assertEqual(msg, MSG)
|
||||
|
||||
def _testRecvInto(self):
|
||||
def _testRecvIntoArray(self):
|
||||
buf = buffer(MSG)
|
||||
self.serv_conn.send(buf)
|
||||
|
||||
def testRecvFromInto(self):
|
||||
def testRecvIntoBytearray(self):
|
||||
buf = bytearray(1024)
|
||||
nbytes = self.cli_conn.recv_into(buf)
|
||||
self.assertEqual(nbytes, len(MSG))
|
||||
msg = buf[:len(MSG)]
|
||||
self.assertEqual(msg, MSG)
|
||||
|
||||
_testRecvIntoBytearray = _testRecvIntoArray
|
||||
|
||||
def testRecvIntoMemoryview(self):
|
||||
buf = bytearray(1024)
|
||||
nbytes = self.cli_conn.recv_into(memoryview(buf))
|
||||
self.assertEqual(nbytes, len(MSG))
|
||||
msg = buf[:len(MSG)]
|
||||
self.assertEqual(msg, MSG)
|
||||
|
||||
_testRecvIntoMemoryview = _testRecvIntoArray
|
||||
|
||||
def testRecvFromIntoArray(self):
|
||||
buf = array.array('c', ' '*1024)
|
||||
nbytes, addr = self.cli_conn.recvfrom_into(buf)
|
||||
self.assertEqual(nbytes, len(MSG))
|
||||
msg = buf.tostring()[:len(MSG)]
|
||||
self.assertEqual(msg, MSG)
|
||||
|
||||
def _testRecvFromInto(self):
|
||||
def _testRecvFromIntoArray(self):
|
||||
buf = buffer(MSG)
|
||||
self.serv_conn.send(buf)
|
||||
|
||||
def testRecvFromIntoBytearray(self):
|
||||
buf = bytearray(1024)
|
||||
nbytes, addr = self.cli_conn.recvfrom_into(buf)
|
||||
self.assertEqual(nbytes, len(MSG))
|
||||
msg = buf[:len(MSG)]
|
||||
self.assertEqual(msg, MSG)
|
||||
|
||||
_testRecvFromIntoBytearray = _testRecvFromIntoArray
|
||||
|
||||
def testRecvFromIntoMemoryview(self):
|
||||
buf = bytearray(1024)
|
||||
nbytes, addr = self.cli_conn.recvfrom_into(memoryview(buf))
|
||||
self.assertEqual(nbytes, len(MSG))
|
||||
msg = buf[:len(MSG)]
|
||||
self.assertEqual(msg, MSG)
|
||||
|
||||
_testRecvFromIntoMemoryview = _testRecvFromIntoArray
|
||||
|
||||
|
||||
TIPC_STYPE = 2000
|
||||
TIPC_LOWER = 200
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue