mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
gh-122356: restore the position of a file-like object after zipfile.is_zipfile
(#122397)
This commit is contained in:
parent
3d8ac48aed
commit
e0ef08f5b4
3 changed files with 13 additions and 2 deletions
|
@ -1969,10 +1969,16 @@ class OtherTests(unittest.TestCase):
|
|||
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
|
||||
|
|
|
@ -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)
|
||||
else:
|
||||
with open(filename, "rb") as fp:
|
||||
result = _check_zipfile(fp)
|
||||
|
|
|
@ -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…
Add table
Add a link
Reference in a new issue