-
-
Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-155419: honor the file object's current position in hashlib.file_digest
#155422
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
Open
picnixz
wants to merge
1
commit into
python:main
Choose a base branch
from
picnixz:meta/crypto/py/hashlib/file-digest-honor-position-155419
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+49
−29
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2026-08-09-13-23-12.gh-issue-155419.J6dSFJ.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Ensure that :func:`hashlib.file_digest` honors the current position of the | ||
| file object when given an :class:`io.BytesIO` object. Patch by Bénédikt | ||
| Tran. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Duck typing this is just checking "does it have a getbuffer" method. I could see
getbufferimplementations which only return the data which will show up in read vs. what BytesIO does (the whole allocated data). I don't think there is a formalized "what doesgetbufferdo" with this case covered.For new feature 3.16+ I think this approach is reasonable / if people have other
getbufferimplementations they'll let us know. As a bugfix backport it seems like a subtle breaking change to performance sensitive code. To backport I think should explicitly checkisinstance(fileobj, io.BytesIO).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.
How can it be a breaking change? it was already like that for the past years
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.
And fileobj.getbuffer() returns a view on the buffer and buf[fileobj.tell():] returns a view as well, so 0-copy is still ensure, or am I missing something?
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.
FTR, we document file_digest as follows:
So if people are doing something else, they're on their own.
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.
context: the file_digest code was shipped in 3.11 and has had few changes since: https://github.com/python/cpython/pull/31930/changes.
While the comment asserts "this is a BytesIO" custom I/O stacks for Python exist. Searching there are quite a few custom
getbufferimplementations outside of io.BytesIO (Many for display buffers, but some I/O buffers). Those pass this code and the typeshed stub protocol (https://github.com/python/typeshed/blob/6875aaf17c8322b00499f79276340fec4cc87451/stdlib/hashlib.pyi#L97-L109) which just assert "getbuffer method required". Custom optimized I/O stacks would likely implement agetbufferwhich produces the result the codebase cares about. That might pay attention to the file offset, dropping already read bytes or might contain all bytes ever written.The change here:
getbuffer()the object must support atell()method unconditionallytellresult is required to go from "all data" to the "data not yet read".Neither of those are "required" to me from the typeshed protocol or duck typing. The added
tellrequirement would break code which runs fine today and so is a breaking change.If we "narrow" this to just BytesIO explicitly then requiring
tellisn't too big of a hurdle and to me it does fix what feels like unintended behavior. Adding a requirement fortellis changing the API meaningfully hence 3.16+.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.
Ah I see what you mean now. Ok this makes sense. I will use an isinstance check for the fast path in all versions and let the slow path orherwise. Custom implementations without tell() would fall back to the slow path.