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: pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy