Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Lib/test/test_zipfile/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1969,10 +1969,16 @@ def test_is_zip_valid_file(self):
zip_contents = fp.read()
# - passing a file-like object
fp = io.BytesIO()
fp.write(zip_contents)
end = fp.write(zip_contents)
self.assertEqual(fp.tell(), end)
mid = end // 2
fp.seek(mid, 0)
self.assertTrue(zipfile.is_zipfile(fp))
fp.seek(0, 0)
# check that the position is left unchanged after the call
# see: https://github.com/python/cpython/issues/122356
self.assertEqual(fp.tell(), mid)
self.assertTrue(zipfile.is_zipfile(fp))
self.assertEqual(fp.tell(), mid)

def test_non_existent_file_raises_OSError(self):
# make sure we don't raise an AttributeError when a partially-constructed
Expand Down
2 changes: 2 additions & 0 deletions Lib/zipfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ def is_zipfile(filename):
result = False
try:
if hasattr(filename, "read"):
pos = filename.tell()
result = _check_zipfile(fp=filename)
filename.seek(pos)
Comment on lines +244 to +246
Copy link
Member

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.

else:
with open(filename, "rb") as fp:
result = _check_zipfile(fp)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Guarantee that the position of a file-like object passed to
:func:`zipfile.is_zipfile` is left untouched after the call.
Patch by Bénédikt Tran.
Loading