gh-122356: restore the position of a file-like object after zipfile.is_zipfile#122397
gh-122356: restore the position of a file-like object after zipfile.is_zipfile#122397jaraco merged 4 commits intopython:mainfrom
zipfile.is_zipfile#122397Conversation
serhiy-storchaka
left a comment
There was a problem hiding this comment.
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 |
serhiy-storchaka
left a comment
There was a problem hiding this comment.
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.
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.