-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
hashlib.file_digest()
can't handle non-blocking I/O
#122179
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
Comments
The solution could be as easy as adding |
I suspect you want |
This wouldn't help, as when using non blocking IO it's likely that no new data will arrive until another task running on this thread gets executed |
FWIW, I do not think it is reasonable to expect that APIs taking a file object as input in order to read or write data from it to be expected to handle non-blocking IO unless they explicitly document themselves as doing so. It isn't the default expectation for what a "file object" is.
We should add the word "blocking" to the file_digest documentation to make the implied constraint more clear. The proposed PR as is would make it explicitly raise an error when a file read did return None internally implying it was in non-blocking mode. It's at least a nicer error to read than trying to figure out why something raised TypeError due to None being used when bytes was expected. |
Good catch. I'll amend the docs tomorrow, unless someone beats me to it. (I think all Python maintainers can commit to this PR, and should feel free to do so.) |
It's not clear to me why socket.makefile() has this defensive coding around non blocking sockets when they're documented as not supported. Checking blame it was added due to #54063 |
* Fix hashlib.file_digest and non-blocking I/O * Add documentation around this behavior * Add versionchanged
thanks! |
…2787) gh-122179: Fix hashlib.file_digest and non-blocking I/O (GH-122183) * Fix hashlib.file_digest and non-blocking I/O * Add documentation around this behavior * Add versionchanged (cherry picked from commit 2b47f46) Co-authored-by: Sebastian Rittau <[email protected]>
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
This came up in python/typeshed#12414.
The current implementation of
file_digest()
does not check the return value offileobj.readinto()
forNone
:cpython/Lib/hashlib.py
Lines 232 to 236 in 2a5d1eb
While buffered file objects can't return
None
, unbuffered ones can when they are doing non-blocking I/O. Specifically,file_digest()
is documented to takeSocketIO
objects, which can very much returnNone
:cpython/Lib/socket.py
Lines 694 to 714 in 2a5d1eb
CPython versions tested on:
CPython main branch
Operating systems tested on:
Other
Linked PRs
The text was updated successfully, but these errors were encountered: