]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-122356: restore the position of a file-like object after `zipfile.is_zipfile`...
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>
Sun, 24 Nov 2024 16:36:15 +0000 (17:36 +0100)
committerGitHub <noreply@github.com>
Sun, 24 Nov 2024 16:36:15 +0000 (11:36 -0500)
Lib/test/test_zipfile/test_core.py
Lib/zipfile/__init__.py
Misc/NEWS.d/next/Library/2024-07-29-15-20-30.gh-issue-122356.wKCmFx.rst [new file with mode: 0644]

index 36f7f54287289707abf0d5bfb64d60d0726fd17c..c36228c033a414f8169021d5cbe8fa51580ddfc4 100644 (file)
@@ -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
index 08c83cfb760250145fe5b01c3d19329b02818945..6907ae6d5b7464f93061cc266c09f18c5ad21671 100644 (file)
@@ -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)
diff --git a/Misc/NEWS.d/next/Library/2024-07-29-15-20-30.gh-issue-122356.wKCmFx.rst b/Misc/NEWS.d/next/Library/2024-07-29-15-20-30.gh-issue-122356.wKCmFx.rst
new file mode 100644 (file)
index 0000000..0a4632c
--- /dev/null
@@ -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.