Skip to content

Commit fe41537

Browse files
miss-islingtondimaryazblurb-it[bot]ZeroIntensityjaraco
authored
[3.12] gh-127847: Fix position in the special-cased zipfile seek (GH-127856) (#128226)
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 e7f8ba7 commit fe41537

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
@@ -2324,6 +2324,18 @@ def test_read_after_seek(self):
23242324
fp.seek(1, os.SEEK_CUR)
23252325
self.assertEqual(fp.read(-1), b'men!')
23262326

2327+
def test_uncompressed_interleaved_seek_read(self):
2328+
# gh-127847: Make sure the position in the archive is correct
2329+
# in the special case of seeking in a ZIP_STORED entry.
2330+
with zipfile.ZipFile(TESTFN, "w") as zipf:
2331+
zipf.writestr("a.txt", "123")
2332+
zipf.writestr("b.txt", "456")
2333+
with zipfile.ZipFile(TESTFN, "r") as zipf:
2334+
with zipf.open("a.txt", "r") as a, zipf.open("b.txt", "r") as b:
2335+
self.assertEqual(a.read(1), b"1")
2336+
self.assertEqual(b.seek(1), 1)
2337+
self.assertEqual(b.read(1), b"5")
2338+
23272339
@requires_bz2()
23282340
def test_decompress_without_3rd_party_library(self):
23292341
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
@@ -794,7 +794,10 @@ def seek(self, offset, whence=0):
794794
raise ValueError("Can't reposition in the ZIP file while "
795795
"there is an open writing handle on it. "
796796
"Close the writing handle before trying to read.")
797-
self._file.seek(offset, whence)
797+
if whence == os.SEEK_CUR:
798+
self._file.seek(self._pos + offset)
799+
else:
800+
self._file.seek(offset, whence)
798801
self._pos = self._file.tell()
799802
return self._pos
800803

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)