Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Merged
tiran merged 2 commits into
python:masterpython/cpython:masterfrom
tiran:bpo-27340-ssl-sendalltiran/cpython:bpo-27340-ssl-sendallCopy head branch name to clipboard
Sep 7, 2017
Merged

bpo-27340: Use memoryview in SSLSocket.sendall()#3384
tiran merged 2 commits into
python:masterpython/cpython:masterfrom
tiran:bpo-27340-ssl-sendalltiran/cpython:bpo-27340-ssl-sendallCopy head branch name to clipboard

Conversation

@tiran

@tiran tiran commented Sep 6, 2017

Copy link
Copy Markdown
Member

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

@tiran tiran added needs backport to 2.7 type-bug An unexpected behavior, bug, or error labels Sep 6, 2017
@tiran tiran force-pushed the bpo-27340-ssl-sendall branch from fd5b2fa to 9d442ed Compare September 6, 2017 13:56
Comment thread Lib/ssl.py Outdated

Copy link
Copy Markdown
Member

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

@vadmium vadmium left a comment

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

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.

@tiran

tiran commented Sep 6, 2017

Copy link
Copy Markdown
Member Author

Thanks @vadmium and @pitrou

I have updated my PR.

@tiran tiran force-pushed the bpo-27340-ssl-sendall branch from 8987de2 to 2a4e458 Compare September 6, 2017 23:35
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>
@tiran tiran force-pushed the bpo-27340-ssl-sendall branch from 2a4e458 to 1dae12f Compare September 7, 2017 18:04
@tiran tiran merged commit 888bbdc into python:master Sep 7, 2017
@tiran tiran deleted the bpo-27340-ssl-sendall branch September 7, 2017 21:18
@tiran

tiran commented Sep 7, 2017

Copy link
Copy Markdown
Member Author

2.7 is not affected.

tiran added a commit to tiran/cpython that referenced this pull request Sep 7, 2017
* 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)
@miss-islington

Copy link
Copy Markdown
Contributor

🐍🍒⛏🤖 Thanks @tiran for the PR, and @tiran for merging it 🌮🎉.I'm working now to backport this PR to: 3.6.

@miss-islington

Copy link
Copy Markdown
Contributor

Sorry, @tiran and @tiran, I could not cleanly backport this to 3.6 due to a conflict.
Please backport using cherry_picker on command line.

@bedevere-bot

Copy link
Copy Markdown

GH-3434 is a backport of this pull request to the 3.6 branch.

tiran added a commit that referenced this pull request Sep 7, 2017
* 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-bug An unexpected behavior, bug, or error

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.