Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-127847: Fix position in the special-cased zipfile seek #127856

Merged
merged 7 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_zipfile/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2333,6 +2333,18 @@ def test_read_after_seek(self):
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'
Expand Down
5 changes: 4 additions & 1 deletion Lib/zipfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,10 @@ def seek(self, offset, whence=0):
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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the position when doing interleaved seeks and reads in uncompressed, unencrypted zip files returned by :meth:`zipfile.ZipFile.open`.
Loading