-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
bpo-27340: Use memoryview in SSLSocket.sendall() #3384
Conversation
fd5b2fa
to
9d442ed
Compare
Lib/ssl.py
Outdated
@@ -961,8 +961,9 @@ def sendall(self, data, flags=0): | |||
self.__class__) | |||
amount = len(data) | |||
count = 0 | |||
view = memoryview(data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 write
The 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
@@ -0,0 +1,3 @@ | |||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 <[email protected]>
Signed-off-by: Christian Heimes <[email protected]>
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 <[email protected]> * Cast view to bytes, fix typo Signed-off-by: Christian Heimes <[email protected]>. (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 <[email protected]> * Cast view to bytes, fix typo Signed-off-by: Christian Heimes <[email protected]>. (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 [email protected]
https://bugs.python.org/issue27340