mirror of
https://github.com/python/cpython.git
synced 2025-08-01 23:53:15 +00:00
bpo-34097: Polish API design (GH-8725)
Move strict_timestamps to constructor.
This commit is contained in:
parent
1b5f9c9653
commit
77b112cd56
3 changed files with 19 additions and 18 deletions
|
@ -131,7 +131,7 @@ ZipFile Objects
|
||||||
|
|
||||||
|
|
||||||
.. class:: ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, \
|
.. class:: ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, \
|
||||||
compresslevel=None)
|
compresslevel=None, *, strict_timestamps=True)
|
||||||
|
|
||||||
Open a ZIP file, where *file* can be a path to a file (a string), a
|
Open a ZIP file, where *file* can be a path to a file (a string), a
|
||||||
file-like object or a :term:`path-like object`.
|
file-like object or a :term:`path-like object`.
|
||||||
|
@ -169,6 +169,12 @@ ZipFile Objects
|
||||||
When using :const:`ZIP_BZIP2` integers ``1`` through ``9`` are accepted
|
When using :const:`ZIP_BZIP2` integers ``1`` through ``9`` are accepted
|
||||||
(see :class:`bz2 <bz2.BZ2File>` for more information).
|
(see :class:`bz2 <bz2.BZ2File>` for more information).
|
||||||
|
|
||||||
|
The *strict_timestamps* argument, when set to ``False``, allows to
|
||||||
|
zip files older than 1980-01-01 at the cost of setting the
|
||||||
|
timestamp to 1980-01-01.
|
||||||
|
Similar behavior occurs with files newer than 2107-12-31,
|
||||||
|
the timestamp is also set to the limit.
|
||||||
|
|
||||||
If the file is created with mode ``'w'``, ``'x'`` or ``'a'`` and then
|
If the file is created with mode ``'w'``, ``'x'`` or ``'a'`` and then
|
||||||
:meth:`closed <close>` without adding any files to the archive, the appropriate
|
:meth:`closed <close>` without adding any files to the archive, the appropriate
|
||||||
ZIP structures for an empty archive will be written to the file.
|
ZIP structures for an empty archive will be written to the file.
|
||||||
|
@ -203,6 +209,9 @@ ZipFile Objects
|
||||||
.. versionchanged:: 3.7
|
.. versionchanged:: 3.7
|
||||||
Add the *compresslevel* parameter.
|
Add the *compresslevel* parameter.
|
||||||
|
|
||||||
|
.. versionadded:: 3.8
|
||||||
|
The *strict_timestamps* keyword-only argument
|
||||||
|
|
||||||
|
|
||||||
.. method:: ZipFile.close()
|
.. method:: ZipFile.close()
|
||||||
|
|
||||||
|
@ -368,7 +377,7 @@ ZipFile Objects
|
||||||
|
|
||||||
|
|
||||||
.. method:: ZipFile.write(filename, arcname=None, compress_type=None, \
|
.. method:: ZipFile.write(filename, arcname=None, compress_type=None, \
|
||||||
compresslevel=None, *, strict_timestamps=True)
|
compresslevel=None)
|
||||||
|
|
||||||
Write the file named *filename* to the archive, giving it the archive name
|
Write the file named *filename* to the archive, giving it the archive name
|
||||||
*arcname* (by default, this will be the same as *filename*, but without a drive
|
*arcname* (by default, this will be the same as *filename*, but without a drive
|
||||||
|
@ -377,11 +386,6 @@ ZipFile Objects
|
||||||
the new entry. Similarly, *compresslevel* will override the constructor if
|
the new entry. Similarly, *compresslevel* will override the constructor if
|
||||||
given.
|
given.
|
||||||
The archive must be open with mode ``'w'``, ``'x'`` or ``'a'``.
|
The archive must be open with mode ``'w'``, ``'x'`` or ``'a'``.
|
||||||
The *strict_timestamps* argument, when set to ``False``, allows to
|
|
||||||
zip files older than 1980-01-01 at the cost of setting the
|
|
||||||
timestamp to 1980-01-01.
|
|
||||||
Similar behavior occurs with files newer than 2107-12-31,
|
|
||||||
the timestamp is also set to the limit.
|
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
|
@ -405,9 +409,6 @@ ZipFile Objects
|
||||||
a closed ZipFile will raise a :exc:`ValueError`. Previously,
|
a closed ZipFile will raise a :exc:`ValueError`. Previously,
|
||||||
a :exc:`RuntimeError` was raised.
|
a :exc:`RuntimeError` was raised.
|
||||||
|
|
||||||
.. versionadded:: 3.8
|
|
||||||
The *strict_timestamps* keyword-only argument
|
|
||||||
|
|
||||||
|
|
||||||
.. method:: ZipFile.writestr(zinfo_or_arcname, data, compress_type=None, \
|
.. method:: ZipFile.writestr(zinfo_or_arcname, data, compress_type=None, \
|
||||||
compresslevel=None)
|
compresslevel=None)
|
||||||
|
|
|
@ -549,8 +549,8 @@ class StoredTestsWithSourceFile(AbstractTestsWithSourceFile,
|
||||||
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
|
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
|
||||||
self.assertRaises(ValueError, zipfp.write, TESTFN)
|
self.assertRaises(ValueError, zipfp.write, TESTFN)
|
||||||
|
|
||||||
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
|
with zipfile.ZipFile(TESTFN2, "w", strict_timestamps=False) as zipfp:
|
||||||
zipfp.write(TESTFN, strict_timestamps=False)
|
zipfp.write(TESTFN)
|
||||||
zinfo = zipfp.getinfo(TESTFN)
|
zinfo = zipfp.getinfo(TESTFN)
|
||||||
self.assertEqual(zinfo.date_time, (1980, 1, 1, 0, 0, 0))
|
self.assertEqual(zinfo.date_time, (1980, 1, 1, 0, 0, 0))
|
||||||
|
|
||||||
|
@ -564,8 +564,8 @@ class StoredTestsWithSourceFile(AbstractTestsWithSourceFile,
|
||||||
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
|
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
|
||||||
self.assertRaises(struct.error, zipfp.write, TESTFN)
|
self.assertRaises(struct.error, zipfp.write, TESTFN)
|
||||||
|
|
||||||
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
|
with zipfile.ZipFile(TESTFN2, "w", strict_timestamps=False) as zipfp:
|
||||||
zipfp.write(TESTFN, strict_timestamps=False)
|
zipfp.write(TESTFN)
|
||||||
zinfo = zipfp.getinfo(TESTFN)
|
zinfo = zipfp.getinfo(TESTFN)
|
||||||
self.assertEqual(zinfo.date_time, (2107, 12, 31, 23, 59, 59))
|
self.assertEqual(zinfo.date_time, (2107, 12, 31, 23, 59, 59))
|
||||||
|
|
||||||
|
|
|
@ -1151,7 +1151,7 @@ class ZipFile:
|
||||||
_windows_illegal_name_trans_table = None
|
_windows_illegal_name_trans_table = None
|
||||||
|
|
||||||
def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True,
|
def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True,
|
||||||
compresslevel=None):
|
compresslevel=None, *, strict_timestamps=True):
|
||||||
"""Open the ZIP file with mode read 'r', write 'w', exclusive create 'x',
|
"""Open the ZIP file with mode read 'r', write 'w', exclusive create 'x',
|
||||||
or append 'a'."""
|
or append 'a'."""
|
||||||
if mode not in ('r', 'w', 'x', 'a'):
|
if mode not in ('r', 'w', 'x', 'a'):
|
||||||
|
@ -1169,6 +1169,7 @@ class ZipFile:
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
self.pwd = None
|
self.pwd = None
|
||||||
self._comment = b''
|
self._comment = b''
|
||||||
|
self._strict_timestamps = strict_timestamps
|
||||||
|
|
||||||
# Check if we were passed a file-like object
|
# Check if we were passed a file-like object
|
||||||
if isinstance(file, os.PathLike):
|
if isinstance(file, os.PathLike):
|
||||||
|
@ -1677,8 +1678,7 @@ class ZipFile:
|
||||||
" would require ZIP64 extensions")
|
" would require ZIP64 extensions")
|
||||||
|
|
||||||
def write(self, filename, arcname=None,
|
def write(self, filename, arcname=None,
|
||||||
compress_type=None, compresslevel=None, *,
|
compress_type=None, compresslevel=None):
|
||||||
strict_timestamps=True):
|
|
||||||
"""Put the bytes from filename into the archive under the name
|
"""Put the bytes from filename into the archive under the name
|
||||||
arcname."""
|
arcname."""
|
||||||
if not self.fp:
|
if not self.fp:
|
||||||
|
@ -1690,7 +1690,7 @@ class ZipFile:
|
||||||
)
|
)
|
||||||
|
|
||||||
zinfo = ZipInfo.from_file(filename, arcname,
|
zinfo = ZipInfo.from_file(filename, arcname,
|
||||||
strict_timestamps=strict_timestamps)
|
strict_timestamps=self._strict_timestamps)
|
||||||
|
|
||||||
if zinfo.is_dir():
|
if zinfo.is_dir():
|
||||||
zinfo.compress_size = 0
|
zinfo.compress_size = 0
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue