summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_zipfile/test_core.py12
-rw-r--r--Lib/zipfile/__init__.py5
2 files changed, 16 insertions, 1 deletions
diff --git a/Lib/test/test_zipfile/test_core.py b/Lib/test/test_zipfile/test_core.py
index c36228c..124e088 100644
--- a/Lib/test/test_zipfile/test_core.py
+++ b/Lib/test/test_zipfile/test_core.py
@@ -2333,6 +2333,18 @@ class OtherTests(unittest.TestCase):
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'
diff --git a/Lib/zipfile/__init__.py b/Lib/zipfile/__init__.py
index 6907ae6..f4d396a 100644
--- a/Lib/zipfile/__init__.py
+++ b/Lib/zipfile/__init__.py
@@ -819,7 +819,10 @@ class _SharedFile:
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