Skip to content

Commit 540c82f

Browse files
committed
pythongh-127847: Fix position in the special-cased zipfile seek
1 parent ba2d2fd commit 540c82f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-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+
# Issue 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+
a.read(1)
2345+
b.seek(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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ def seek(self, offset, whence=os.SEEK_SET):
11671167
self._expected_crc = None
11681168
# seek actual file taking already buffered data into account
11691169
read_offset -= len(self._readbuffer) - self._offset
1170-
self._fileobj.seek(read_offset, os.SEEK_CUR)
1170+
self._fileobj.seek(self._orig_compress_start + read_offset)
11711171
self._left -= read_offset
11721172
read_offset = 0
11731173
# flush read buffer

0 commit comments

Comments
 (0)