-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-122356: restore the position of a file-like object after zipfile.is_zipfile
#122397
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
gh-122356: restore the position of a file-like object after zipfile.is_zipfile
#122397
Conversation
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.
What is the behavior for unseekable file before and after this change?
|
Everything is wrapped in a huge cpython/Lib/zipfile/__init__.py Lines 236 to 250 in 612ac28
OTOH, cpython/Lib/zipfile/__init__.py Lines 228 to 234 in 612ac28
And cpython/Lib/zipfile/__init__.py Lines 295 to 303 in 612ac28
So technically, before the exception would be raised from Do you prefer |
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.
I did not though that this is important, but tarfile.is_tarfile() restores the file position (although this is a recent behavior). And imghdr.what() restored it. So it is safer to follow.
LGTM.
| pos = filename.tell() | ||
| result = _check_zipfile(fp=filename) | ||
| filename.seek(pos) |
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.
I'm guessing that a user will never call is_zipfile on a non-seekable stream (e.g. a web response stream)... but if they did, this change would break those cases. Oh, I just checked, and the current check already relies on a seekable fp, so this change has no effect on that behavior.
…pfile.is_zipfile` (python#122397)
Not sure if you want it to be backported to 3.12 and 3.13, since the corresponding PR (#26488) for
tarfilewas not backported.