mirror of
https://github.com/python/cpython
synced 2024-09-05 08:17:45 +00:00
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:
parent
22e411e1d1
commit
812360fddd
12
Lib/ssl.py
12
Lib/ssl.py
|
@ -1270,10 +1270,14 @@ def recv(self, buflen=1024, flags=0):
|
|||
|
||||
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(
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
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 @@ def test_recv_zero(self):
|
|||
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,
|
||||
|
|
|
@ -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.
|
Loading…
Reference in a new issue