From e6c5a9f6346771cb91d865efdbd995819fb3076a Mon Sep 17 00:00:00 2001 From: Emma Harper Smith Date: Sun, 6 Apr 2025 15:45:31 -0700 Subject: [PATCH 1/2] gh-84481: Make ZipFile._data_offset more robust --- Lib/test/test_zipfile/test_core.py | 6 ++++++ Lib/zipfile/__init__.py | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_zipfile/test_core.py b/Lib/test/test_zipfile/test_core.py index 94c0a44f3758d2..89eeef54598d11 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 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: From 46abb2cae3a3d99bdbc7b04428cccf83fff0b8ae Mon Sep 17 00:00:00 2001 From: Emma Harper Smith Date: Mon, 7 Apr 2025 18:28:55 -0700 Subject: [PATCH 2/2] Use self.assertIsNone() --- Lib/test/test_zipfile/test_core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_zipfile/test_core.py b/Lib/test/test_zipfile/test_core.py index 89eeef54598d11..2a4e1acf2195ca 100644 --- a/Lib/test/test_zipfile/test_core.py +++ b/Lib/test/test_zipfile/test_core.py @@ -3363,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): 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