Skip to content

bpo-27340: Use memoryview in SSLSocket.sendall() #3384

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 2 commits into from
Sep 7, 2017
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
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 @@ -2891,14 +2895,20 @@ def _recvfrom_into():
self.assertEqual(s.read(-1, buffer), len(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
# of the encrypted data stream
self.assertRaises(NotImplementedError, s.sendmsg, [b"data"])
self.assertRaises(NotImplementedError, s.recvmsg, 100)
self.assertRaises(NotImplementedError,
s.recvmsg_into, bytearray(100))

s.write(b"over\n")

self.assertRaises(ValueError, s.recv, -1)
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.