Skip to content

Commit 7ed6c5c

Browse files
dimaryazblurb-it[bot]ZeroIntensityjaraco
authored
pythongh-127847: Fix position in the special-cased zipfile seek (python#127856)
--------- 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 9fce906 commit 7ed6c5c

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
@@ -2333,6 +2333,18 @@ def test_read_after_seek(self):
23332333
fp.seek(1, os.SEEK_CUR)
23342334
self.assertEqual(fp.read(-1), b'men!')
23352335

2336+
def test_uncompressed_interleaved_seek_read(self):
2337+
# gh-127847: Make sure the position in the archive is correct
2338+
# in the special case of seeking in a ZIP_STORED entry.
2339+
with zipfile.ZipFile(TESTFN, "w") as zipf:
2340+
zipf.writestr("a.txt", "123")
2341+
zipf.writestr("b.txt", "456")
2342+
with zipfile.ZipFile(TESTFN, "r") as zipf:
2343+
with zipf.open("a.txt", "r") as a, zipf.open("b.txt", "r") as b:
2344+
self.assertEqual(a.read(1), b"1")
2345+
self.assertEqual(b.seek(1), 1)
2346+
self.assertEqual(b.read(1), b"5")
2347+
23362348
@requires_bz2()
23372349
def test_decompress_without_3rd_party_library(self):
23382350
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
@@ -819,7 +819,10 @@ def seek(self, offset, whence=0):
819819
raise ValueError("Can't reposition in the ZIP file while "
820820
"there is an open writing handle on it. "
821821
"Close the writing handle before trying to read.")
822-
self._file.seek(offset, whence)
822+
if whence == os.SEEK_CUR:
823+
self._file.seek(self._pos + offset)
824+
else:
825+
self._file.seek(offset, whence)
823826
self._pos = self._file.tell()
824827
return self._pos
825828

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)