Skip to content

Commit caef946

Browse files
miss-islingtonemmatypinggpshead
authored
[3.14] gh-136170: Revert adding ZipFile.data_offset (GH-136950) (#136955)
gh-136170: Revert adding `ZipFile.data_offset` (GH-136950) * Revert "gh-84481: Make ZipFile.data_offset more robust (GH-132178)" This reverts commit 6cd1d6c. * Revert "gh-84481: Add ZipFile.data_offset attribute (GH-132165)" This reverts commit 0788948. --------- (cherry picked from commit 6bf1c0a) Co-authored-by: Emma Smith <[email protected]> Co-authored-by: Gregory P. Smith <[email protected]>
1 parent 4af1b72 commit caef946

File tree

4 files changed

+3
-75
lines changed

4 files changed

+3
-75
lines changed

Doc/library/zipfile.rst

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -558,14 +558,6 @@ The following data attributes are also available:
558558
it should be no longer than 65535 bytes. Comments longer than this will be
559559
truncated.
560560

561-
.. attribute:: ZipFile.data_offset
562-
563-
The offset to the start of ZIP data from the beginning of the file. When the
564-
:class:`ZipFile` is opened in either mode ``'w'`` or ``'x'`` and the
565-
underlying file does not support ``tell()``, the value will be ``None``
566-
instead.
567-
568-
.. versionadded:: 3.14
569561

570562
.. _path-objects:
571563

Lib/test/test_zipfile/test_core.py

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3470,60 +3470,6 @@ def test_execute_zip64(self):
34703470
self.assertIn(b'number in executable: 5', output)
34713471

34723472

3473-
class TestDataOffsetPrependedZip(unittest.TestCase):
3474-
"""Test .data_offset on reading zip files with an executable prepended."""
3475-
3476-
def setUp(self):
3477-
self.exe_zip = findfile('exe_with_zip', subdir='archivetestdata')
3478-
self.exe_zip64 = findfile('exe_with_z64', subdir='archivetestdata')
3479-
3480-
def _test_data_offset(self, name):
3481-
with zipfile.ZipFile(name) as zipfp:
3482-
self.assertEqual(zipfp.data_offset, 713)
3483-
3484-
def test_data_offset_with_exe_prepended(self):
3485-
self._test_data_offset(self.exe_zip)
3486-
3487-
def test_data_offset_with_exe_prepended_zip64(self):
3488-
self._test_data_offset(self.exe_zip64)
3489-
3490-
class TestDataOffsetZipWrite(unittest.TestCase):
3491-
"""Test .data_offset for ZipFile opened in write mode."""
3492-
3493-
def setUp(self):
3494-
os.mkdir(TESTFNDIR)
3495-
self.addCleanup(rmtree, TESTFNDIR)
3496-
self.test_path = os.path.join(TESTFNDIR, 'testoffset.zip')
3497-
3498-
def test_data_offset_write_no_prefix(self):
3499-
with io.BytesIO() as fp:
3500-
with zipfile.ZipFile(fp, "w") as zipfp:
3501-
self.assertEqual(zipfp.data_offset, 0)
3502-
3503-
def test_data_offset_write_with_prefix(self):
3504-
with io.BytesIO() as fp:
3505-
fp.write(b"this is a prefix")
3506-
with zipfile.ZipFile(fp, "w") as zipfp:
3507-
self.assertEqual(zipfp.data_offset, 16)
3508-
3509-
def test_data_offset_append_with_bad_zip(self):
3510-
with io.BytesIO() as fp:
3511-
fp.write(b"this is a prefix")
3512-
with zipfile.ZipFile(fp, "a") as zipfp:
3513-
self.assertEqual(zipfp.data_offset, 16)
3514-
3515-
def test_data_offset_write_no_tell(self):
3516-
# The initializer in ZipFile checks if tell raises AttributeError or
3517-
# OSError when creating a file in write mode when deducing the offset
3518-
# of the beginning of zip data
3519-
class NoTellBytesIO(io.BytesIO):
3520-
def tell(self):
3521-
raise OSError("Unimplemented!")
3522-
with NoTellBytesIO() as fp:
3523-
with zipfile.ZipFile(fp, "w") as zipfp:
3524-
self.assertIsNone(zipfp.data_offset)
3525-
3526-
35273473
class EncodedMetadataTests(unittest.TestCase):
35283474
file_names = ['\u4e00', '\u4e8c', '\u4e09'] # Han 'one', 'two', 'three'
35293475
file_content = [

Lib/zipfile/__init__.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,6 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True,
14521452
self._lock = threading.RLock()
14531453
self._seekable = True
14541454
self._writing = False
1455-
self._data_offset = None
14561455

14571456
try:
14581457
if mode == 'r':
@@ -1463,7 +1462,6 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True,
14631462
self._didModify = True
14641463
try:
14651464
self.start_dir = self.fp.tell()
1466-
self._data_offset = self.start_dir
14671465
except (AttributeError, OSError):
14681466
self.fp = _Tellable(self.fp)
14691467
self.start_dir = 0
@@ -1488,7 +1486,6 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True,
14881486
# even if no files are added to the archive
14891487
self._didModify = True
14901488
self.start_dir = self.fp.tell()
1491-
self._data_offset = self.start_dir
14921489
else:
14931490
raise ValueError("Mode must be 'r', 'w', 'x', or 'a'")
14941491
except:
@@ -1535,10 +1532,6 @@ def _RealGetContents(self):
15351532
# self.start_dir: Position of start of central directory
15361533
self.start_dir = offset_cd + concat
15371534

1538-
# store the offset to the beginning of data for the
1539-
# .data_offset property
1540-
self._data_offset = concat
1541-
15421535
if self.start_dir < 0:
15431536
raise BadZipFile("Bad offset for central directory")
15441537
fp.seek(self.start_dir, 0)
@@ -1599,12 +1592,6 @@ def _RealGetContents(self):
15991592
zinfo._end_offset = end_offset
16001593
end_offset = zinfo.header_offset
16011594

1602-
@property
1603-
def data_offset(self):
1604-
"""The offset to the start of zip data in the file or None if
1605-
unavailable."""
1606-
return self._data_offset
1607-
16081595
def namelist(self):
16091596
"""Return a list of file names in the archive."""
16101597
return [data.filename for data in self.filelist]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Removed the unreleased ``zipfile.ZipFile.data_offset`` property added in 3.14.0a7
2+
as it wasn't fully clear which behavior it should have in some situations so
3+
the result was not always what a user might expect.

0 commit comments

Comments
 (0)