]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-127847: Fix position in the special-cased zipfile seek (GH-127856) (#128226)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 24 Dec 2024 16:15:45 +0000 (17:15 +0100)
committerGitHub <noreply@github.com>
Tue, 24 Dec 2024 16:15:45 +0000 (16:15 +0000)
gh-127847: Fix position in the special-cased zipfile seek (GH-127856)

---------
(cherry picked from commit 7ed6c5c6961d0849f163d4d449fb36bae312b6bc)

Co-authored-by: Dima Ryazanov <dima@bucket.xxx>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
Lib/test/test_zipfile/test_core.py
Lib/zipfile/__init__.py
Misc/NEWS.d/next/Library/2024-12-12-07-27-51.gh-issue-127847.ksfNKM.rst [new file with mode: 0644]

index 86efccd26768a101f9b415c68fee9c1149071e4f..adb74345486b65465f70a22cfb6856f550bb5a17 100644 (file)
@@ -2324,6 +2324,18 @@ class OtherTests(unittest.TestCase):
                 fp.seek(1, os.SEEK_CUR)
                 self.assertEqual(fp.read(-1), b'men!')
 
+    def test_uncompressed_interleaved_seek_read(self):
+        # gh-127847: Make sure the position in the archive is correct
+        # in the special case of seeking in a ZIP_STORED entry.
+        with zipfile.ZipFile(TESTFN, "w") as zipf:
+            zipf.writestr("a.txt", "123")
+            zipf.writestr("b.txt", "456")
+        with zipfile.ZipFile(TESTFN, "r") as zipf:
+            with zipf.open("a.txt", "r") as a, zipf.open("b.txt", "r") as b:
+                self.assertEqual(a.read(1), b"1")
+                self.assertEqual(b.seek(1), 1)
+                self.assertEqual(b.read(1), b"5")
+
     @requires_bz2()
     def test_decompress_without_3rd_party_library(self):
         data = b'PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
index cf71c6dba2b836986eeab60c057642d3202a175f..3e1cca472865b5d62e9cc1a6eff0be40a7a3769e 100644 (file)
@@ -794,7 +794,10 @@ class _SharedFile:
                 raise ValueError("Can't reposition in the ZIP file while "
                         "there is an open writing handle on it. "
                         "Close the writing handle before trying to read.")
-            self._file.seek(offset, whence)
+            if whence == os.SEEK_CUR:
+                self._file.seek(self._pos + offset)
+            else:
+                self._file.seek(offset, whence)
             self._pos = self._file.tell()
             return self._pos
 
diff --git a/Misc/NEWS.d/next/Library/2024-12-12-07-27-51.gh-issue-127847.ksfNKM.rst b/Misc/NEWS.d/next/Library/2024-12-12-07-27-51.gh-issue-127847.ksfNKM.rst
new file mode 100644 (file)
index 0000000..3d6e36f
--- /dev/null
@@ -0,0 +1 @@
+Fix the position when doing interleaved seeks and reads in uncompressed, unencrypted zip files returned by :meth:`zipfile.ZipFile.open`.