Skip to content

Commit

Permalink
[3.6] bpo-27340: Use memoryview in SSLSocket.sendall() (pythonGH-3384)
Browse files Browse the repository at this point in the history
* bpo-27340: Use memoryview in SSLSocket.sendall()

SSLSocket.sendall() now uses memoryview to create slices of data. This fix
support for all bytes-like object. It is also more efficient and avoids
costly copies.

Signed-off-by: Christian Heimes <[email protected]>

* Cast view to bytes, fix typo

Signed-off-by: Christian Heimes <[email protected]>.
(cherry picked from commit 888bbdc)
  • Loading branch information
tiran committed Sep 7, 2017
1 parent e89b35d commit 0b9c29f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
9 changes: 5 additions & 4 deletions Lib/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,11 +959,12 @@ def sendall(self, data, flags=0):
raise ValueError(
"non-zero flags not allowed in calls to sendall() on %s" %
self.__class__)
amount = len(data)
count = 0
while (count < amount):
v = self.send(data[count:])
count += v
with memoryview(data) as view, view.cast("B") as byte_view:
amount = len(byte_view)
while count < amount:
v = self.send(byte_view[count:])
count += v
else:
return socket.sendall(self, data, flags)

Expand Down
12 changes: 11 additions & 1 deletion Lib/test/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import weakref
import platform
import functools
try:
import ctypes
except ImportError:
ctypes = None

ssl = support.import_module("ssl")

Expand Down Expand Up @@ -2882,7 +2886,13 @@ def _recvfrom_into():
s.send(data)
buffer = bytearray(len(data))
self.assertEqual(s.read(-1, buffer), len(data))
self.assertEqual(buffer, data)
self.assertEqual(buffer, data) # sendall accepts bytes-like objects

if ctypes is not None:
ubyte = ctypes.c_ubyte * len(data)
byteslike = ubyte.from_buffer_copy(data)
s.sendall(byteslike)
self.assertEqual(s.read(), data)

# Make sure sendmsg et al are disallowed to avoid
# inadvertent disclosure of data and/or corruption
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SSLSocket.sendall() now uses memoryview to create slices of data. This fixes
support for all bytes-like object. It is also more efficient and avoids
costly copies.

0 comments on commit 0b9c29f

Please sign in to comment.