https://github.com/python/cpython/commit/6cd1d6c6b142697fb72f422b7b448c27ebc30534
commit: 6cd1d6c6b142697fb72f422b7b448c27ebc30534
branch: main
author: Emma Smith <e...@emmatyping.dev>
committer: hugovk <1324225+hug...@users.noreply.github.com>
date: 2025-04-08T10:43:14+03:00
summary:

gh-84481: Make ZipFile.data_offset more robust (#132178)

files:
M Lib/test/test_zipfile/test_core.py
M Lib/zipfile/__init__.py

diff --git a/Lib/test/test_zipfile/test_core.py 
b/Lib/test/test_zipfile/test_core.py
index 94c0a44f3758d2..2a4e1acf2195ca 100644
--- a/Lib/test/test_zipfile/test_core.py
+++ b/Lib/test/test_zipfile/test_core.py
@@ -3348,6 +3348,12 @@ def test_data_offset_write_with_prefix(self):
             with zipfile.ZipFile(fp, "w") as zipfp:
                 self.assertEqual(zipfp.data_offset, 16)
 
+    def test_data_offset_append_with_bad_zip(self):
+        with io.BytesIO() as fp:
+            fp.write(b"this is a prefix")
+            with zipfile.ZipFile(fp, "a") as zipfp:
+                self.assertEqual(zipfp.data_offset, 16)
+
     def test_data_offset_write_no_tell(self):
         # The initializer in ZipFile checks if tell raises AttributeError or
         # OSError when creating a file in write mode when deducing the offset
@@ -3357,7 +3363,7 @@ def tell(self):
                 raise OSError("Unimplemented!")
         with NoTellBytesIO() as fp:
             with zipfile.ZipFile(fp, "w") as zipfp:
-                self.assertIs(zipfp.data_offset, None)
+                self.assertIsNone(zipfp.data_offset)
 
 
 class EncodedMetadataTests(unittest.TestCase):
diff --git a/Lib/zipfile/__init__.py b/Lib/zipfile/__init__.py
index b061691ac6f8b9..e3a94215bd6700 100644
--- a/Lib/zipfile/__init__.py
+++ b/Lib/zipfile/__init__.py
@@ -1403,6 +1403,7 @@ def __init__(self, file, mode="r", 
compression=ZIP_STORED, allowZip64=True,
         self._lock = threading.RLock()
         self._seekable = True
         self._writing = False
+        self._data_offset = None
 
         try:
             if mode == 'r':
@@ -1418,7 +1419,6 @@ def __init__(self, file, mode="r", 
compression=ZIP_STORED, allowZip64=True,
                     self.fp = _Tellable(self.fp)
                     self.start_dir = 0
                     self._seekable = False
-                    self._data_offset = None
                 else:
                     # Some file-like objects can provide tell() but not seek()
                     try:
@@ -1439,6 +1439,7 @@ def __init__(self, file, mode="r", 
compression=ZIP_STORED, allowZip64=True,
                     # even if no files are added to the archive
                     self._didModify = True
                     self.start_dir = self.fp.tell()
+                    self._data_offset = self.start_dir
             else:
                 raise ValueError("Mode must be 'r', 'w', 'x', or 'a'")
         except:

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to