[Python-checkins] [3.6] bpo-27340: Use memoryview in SSLSocket.sendall() (GH-3384) (#3434)
Christian Heimes
webhook-mailer at python.org
Thu Sep 7 19:59:22 EDT 2017
https://github.com/python/cpython/commit/9423f5d68874ff2e500cbe072c25f883cf754be8
commit: 9423f5d68874ff2e500cbe072c25f883cf754be8
branch: 3.6
author: Christian Heimes <christian at python.org>
committer: GitHub <noreply at github.com>
date: 2017-09-07T16:59:17-07:00
summary:
[3.6] bpo-27340: Use memoryview in SSLSocket.sendall() (GH-3384) (#3434)
* 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 <christian at python.org>
* Cast view to bytes, fix typo
Signed-off-by: Christian Heimes <christian at python.org>.
(cherry picked from commit 888bbdc192ec4db888a294ef758cf5510442dc9a)
files:
A Misc/NEWS.d/next/Library/2017-09-06-06-50-41.bpo-27340.GgekV5.rst
M Lib/ssl.py
M Lib/test/test_ssl.py
diff --git a/Lib/ssl.py b/Lib/ssl.py
index 8ad4a339a93..7a574dcb2b1 100644
--- a/Lib/ssl.py
+++ b/Lib/ssl.py
@@ -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)
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
index 4191d9036e4..157e6ced01b 100644
--- a/Lib/test/test_ssl.py
+++ b/Lib/test/test_ssl.py
@@ -18,6 +18,10 @@
import weakref
import platform
import functools
+try:
+ import ctypes
+except ImportError:
+ ctypes = None
ssl = support.import_module("ssl")
@@ -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
diff --git a/Misc/NEWS.d/next/Library/2017-09-06-06-50-41.bpo-27340.GgekV5.rst b/Misc/NEWS.d/next/Library/2017-09-06-06-50-41.bpo-27340.GgekV5.rst
new file mode 100644
index 00000000000..2d05e10fc14
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-09-06-06-50-41.bpo-27340.GgekV5.rst
@@ -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.
More information about the Python-checkins
mailing list