Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 77b112c

Browse files
Marcel Plchvstinner
authored andcommitted
bpo-34097: Polish API design (GH-8725)
Move strict_timestamps to constructor.
1 parent 1b5f9c9 commit 77b112c

3 files changed

Lines changed: 19 additions & 18 deletions

File tree

Doc/library/zipfile.rst

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ ZipFile Objects
131131

132132

133133
.. class:: ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, \
134-
compresslevel=None)
134+
compresslevel=None, *, strict_timestamps=True)
135135
136136
Open a ZIP file, where *file* can be a path to a file (a string), a
137137
file-like object or a :term:`path-like object`.
@@ -169,6 +169,12 @@ ZipFile Objects
169169
When using :const:`ZIP_BZIP2` integers ``1`` through ``9`` are accepted
170170
(see :class:`bz2 <bz2.BZ2File>` for more information).
171171

172+
The *strict_timestamps* argument, when set to ``False``, allows to
173+
zip files older than 1980-01-01 at the cost of setting the
174+
timestamp to 1980-01-01.
175+
Similar behavior occurs with files newer than 2107-12-31,
176+
the timestamp is also set to the limit.
177+
172178
If the file is created with mode ``'w'``, ``'x'`` or ``'a'`` and then
173179
:meth:`closed <close>` without adding any files to the archive, the appropriate
174180
ZIP structures for an empty archive will be written to the file.
@@ -203,6 +209,9 @@ ZipFile Objects
203209
.. versionchanged:: 3.7
204210
Add the *compresslevel* parameter.
205211

212+
.. versionadded:: 3.8
213+
The *strict_timestamps* keyword-only argument
214+
206215

207216
.. method:: ZipFile.close()
208217

@@ -368,7 +377,7 @@ ZipFile Objects
368377

369378

370379
.. method:: ZipFile.write(filename, arcname=None, compress_type=None, \
371-
compresslevel=None, *, strict_timestamps=True)
380+
compresslevel=None)
372381

373382
Write the file named *filename* to the archive, giving it the archive name
374383
*arcname* (by default, this will be the same as *filename*, but without a drive
@@ -377,11 +386,6 @@ ZipFile Objects
377386
the new entry. Similarly, *compresslevel* will override the constructor if
378387
given.
379388
The archive must be open with mode ``'w'``, ``'x'`` or ``'a'``.
380-
The *strict_timestamps* argument, when set to ``False``, allows to
381-
zip files older than 1980-01-01 at the cost of setting the
382-
timestamp to 1980-01-01.
383-
Similar behavior occurs with files newer than 2107-12-31,
384-
the timestamp is also set to the limit.
385389

386390
.. note::
387391

@@ -405,9 +409,6 @@ ZipFile Objects
405409
a closed ZipFile will raise a :exc:`ValueError`. Previously,
406410
a :exc:`RuntimeError` was raised.
407411

408-
.. versionadded:: 3.8
409-
The *strict_timestamps* keyword-only argument
410-
411412

412413
.. method:: ZipFile.writestr(zinfo_or_arcname, data, compress_type=None, \
413414
compresslevel=None)

Lib/test/test_zipfile.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,8 @@ def test_add_file_before_1980(self):
549549
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
550550
self.assertRaises(ValueError, zipfp.write, TESTFN)
551551

552-
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
553-
zipfp.write(TESTFN, strict_timestamps=False)
552+
with zipfile.ZipFile(TESTFN2, "w", strict_timestamps=False) as zipfp:
553+
zipfp.write(TESTFN)
554554
zinfo = zipfp.getinfo(TESTFN)
555555
self.assertEqual(zinfo.date_time, (1980, 1, 1, 0, 0, 0))
556556

@@ -564,8 +564,8 @@ def test_add_file_after_2107(self):
564564
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
565565
self.assertRaises(struct.error, zipfp.write, TESTFN)
566566

567-
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
568-
zipfp.write(TESTFN, strict_timestamps=False)
567+
with zipfile.ZipFile(TESTFN2, "w", strict_timestamps=False) as zipfp:
568+
zipfp.write(TESTFN)
569569
zinfo = zipfp.getinfo(TESTFN)
570570
self.assertEqual(zinfo.date_time, (2107, 12, 31, 23, 59, 59))
571571

Lib/zipfile.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ class ZipFile:
11511151
_windows_illegal_name_trans_table = None
11521152

11531153
def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True,
1154-
compresslevel=None):
1154+
compresslevel=None, *, strict_timestamps=True):
11551155
"""Open the ZIP file with mode read 'r', write 'w', exclusive create 'x',
11561156
or append 'a'."""
11571157
if mode not in ('r', 'w', 'x', 'a'):
@@ -1169,6 +1169,7 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True,
11691169
self.mode = mode
11701170
self.pwd = None
11711171
self._comment = b''
1172+
self._strict_timestamps = strict_timestamps
11721173

11731174
# Check if we were passed a file-like object
11741175
if isinstance(file, os.PathLike):
@@ -1677,8 +1678,7 @@ def _writecheck(self, zinfo):
16771678
" would require ZIP64 extensions")
16781679

16791680
def write(self, filename, arcname=None,
1680-
compress_type=None, compresslevel=None, *,
1681-
strict_timestamps=True):
1681+
compress_type=None, compresslevel=None):
16821682
"""Put the bytes from filename into the archive under the name
16831683
arcname."""
16841684
if not self.fp:
@@ -1690,7 +1690,7 @@ def write(self, filename, arcname=None,
16901690
)
16911691

16921692
zinfo = ZipInfo.from_file(filename, arcname,
1693-
strict_timestamps=strict_timestamps)
1693+
strict_timestamps=self._strict_timestamps)
16941694

16951695
if zinfo.is_dir():
16961696
zinfo.compress_size = 0

0 commit comments

Comments
 (0)