8000 [3.11] gh-84443: SSLSocket.recv_into() now support buffer protocol with itemsize != 1 (GH-20310) by serhiy-storchaka · Pull Request #112459 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.11] gh-84443: SSLSocket.recv_into() 8000 now support buffer protocol with itemsize != 1 (GH-20310) #112459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Lib/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1299,10 +1299,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(
Expand Down
22 changes: 22 additions & 0 deletions Lib/test/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from test.support import socket_helper
from test.support import threading_helper
from test.support import warnings_helper
import array
import re
import socket
import select
Expand Down Expand Up @@ -3765,6 +3766,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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
0