Skip to content

Commit e1a0c40

Browse files
miss-islingtondimaryazblurb-it[bot]ZeroIntensityjaraco
authored
[3.13] gh-127847: Fix position in the special-cased zipfile seek (GH-127856) (#128225)
gh-127847: Fix position in the special-cased zipfile seek (GH-127856) --------- (cherry picked from commit 7ed6c5c) Co-authored-by: Dima Ryazanov <[email protected]> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Peter Bierma <[email protected]> Co-authored-by: Jason R. Coombs <[email protected]>
1 parent 40c46f0 commit e1a0c40

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

Lib/test/test_zipfile/test_core.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,6 +2327,18 @@ def test_read_after_seek(self):
23272327
fp.seek(1, os.SEEK_CUR)
23282328
self.assertEqual(fp.read(-1), b'men!')
23292329

2330+
def test_uncompressed_interleaved_seek_read(self):
2331+
# gh-127847: Make sure the position in the archive is correct
2332+
# in the special case of seeking in a ZIP_STORED entry.
2333+
with zipfile.ZipFile(TESTFN, "w") as zipf:
2334+
zipf.writestr("a.txt", "123")
2335+
zipf.writestr("b.txt", "456")
2336+
with zipfile.ZipFile(TESTFN, "r") as zipf:
2337+
with zipf.open("a.txt", "r") as a, zipf.open("b.txt", "r") as b:
2338+
self.assertEqual(a.read(1), b"1")
2339+
self.assertEqual(b.seek(1), 1)
2340+
self.assertEqual(b.read(1), b"5")
2341+
23302342
@requires_bz2()
23312343
def test_decompress_without_3rd_party_library(self):
23322344
data = b'PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

Lib/zipfile/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,10 @@ def seek(self, offset, whence=0):
817817
raise ValueError("Can't reposition in the ZIP file while "
818818
"there is an open writing handle on it. "
819819
"Close the writing handle before trying to read.")
820-
self._file.seek(offset, whence)
820+
if whence == os.SEEK_CUR:
821+
self._file.seek(self._pos + offset)
822+
else:
823+
self._file.seek(offset, whence)
821824
self._pos = self._file.tell()
822825
return self._pos
823826

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix the position when doing interleaved seeks and reads in uncompressed, unencrypted zip files returned by :meth:`zipfile.ZipFile.open`.

0 commit comments

Comments
 (0)