gh-84443: SSLSocket.recv_into() now support buffer protocol with itemsize != 1 (GH-20310)

It is also no longer use __len__().

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Zackery Spytz 2023-11-27 09:15:39 -08:00 committed by GitHub
parent 22e411e1d1
commit 812360fddd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 4 deletions

View File

@ -1270,10 +1270,14 @@ class SSLSocket(socket):
def recv_into(self, buffer, nbytes=None, flags=0):
self._checkClosed()
if buffer and (nbytes is None):
nbytes = len(buffer)
elif nbytes is None:
nbytes = 1024
if nbytes is None:
if buffer is not None:
with memoryview(buffer) as view:
nbytes = view.nbytes
if not nbytes:
nbytes = 1024
else:
nbytes = 1024
if self._sslobj is not None:
if flags != 0:
raise ValueError(

View File

@ -10,6 +10,7 @@ from test.support import socket_helper
from test.support import threading_helper
from test.support import warnings_helper
from test.support import asyncore
import array
import re
import socket
import select
@ -3517,6 +3518,27 @@ class ThreadedTests(unittest.TestCase):
self.assertEqual(s.recv(0), b"")
self.assertEqual(s.recv_into(bytearray()), 0)
def test_recv_into_buffer_protocol_len(self):
server = ThreadedEchoServer(CERTFILE)
self.enterContext(server)
s = socket.create_connection((HOST, server.port))
self.addCleanup(s.close)
s = test_wrap_socket(s, suppress_ragged_eofs=False)
self.addCleanup(s.close)
s.send(b"data")
buf = array.array('I', [0, 0])
self.assertEqual(s.recv_into(buf), 4)
self.assertEqual(bytes(buf)[:4], b"data")
class B(bytearray):
def __len__(self):
1/0
s.send(b"data")
buf = B(6)
self.assertEqual(s.recv_into(buf), 4)
self.assertEqual(bytes(buf), b"data\0\0")
def test_nonblocking_send(self):
server = ThreadedEchoServer(CERTFILE,
certreqs=ssl.CERT_NONE,

View File

@ -0,0 +1,2 @@
The :meth:`ssl.SSLSocket.recv_into` method no longer requires the *buffer*
argument to implement ``__len__`` and supports buffers with arbitrary item size.