bpo-27340: Use memoryview in SSLSocket.sendall()#3384
Conversation
fd5b2fa to
9d442ed
Compare
There was a problem hiding this comment.
You should cast the memoryview to a bytes unit, otherwise slicing may work in non-byte units. See e.g. BufferedIOBase.readinto at https://github.com/python/cpython/blob/master/Lib/_pyio.py#L695
vadmium
left a comment
There was a problem hiding this comment.
My understanding is that “bytes-like object” is supposed to cover objects without any array dimensions, and arrays of things other than bytes. But naively slicing a memoryview doesn’t work in those cases, and len may not be supported. Test cases:
>>> nonarray = ctypes.c_int()
>>> h.request("POST", "/", body=nonarray)
File "/usr/lib/python3.5/ssl.py", line 883, in sendall
amount = len(data)
TypeError: object of type 'c_long' has no len()
>>> nonbyte_array = array.array("I", (0,)) * int(10e6)
>>> # I expect the server may not receive all 40+ MB if a “send” call does a short writeThe way I handle this is with memoryview.cast:
with memoryview(byteslike) as view, view.cast("B") as bytes_view:
...E.g. https://github.com/python/cpython/blob/v3.6.2/Lib/_compression.py#L66
There was a problem hiding this comment.
This fixes . . . bytes-like objects.
8987de2 to
2a4e458
Compare
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@python.org>
Signed-off-by: Christian Heimes <christian@python.org>
2a4e458 to
1dae12f
Compare
|
2.7 is not affected. |
* 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@python.org> * Cast view to bytes, fix typo Signed-off-by: Christian Heimes <christian@python.org>. (cherry picked from commit 888bbdc)
|
Sorry, @tiran and @tiran, I could not cleanly backport this to |
|
GH-3434 is a backport of this pull request to the 3.6 branch. |
* 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@python.org> * Cast view to bytes, fix typo Signed-off-by: Christian Heimes <christian@python.org>. (cherry picked from commit 888bbdc)
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@python.org
https://bugs.python.org/issue27340