Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-125260: gzip: let compress mtime default to 0 #125261

Merged
merged 4 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Doc/library/gzip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,12 @@ The module defines the following items:
attribute instead.


.. function:: compress(data, compresslevel=9, *, mtime=None)
.. function:: compress(data, compresslevel=9, *, mtime=0)

Compress the *data*, returning a :class:`bytes` object containing
bmwiedemann marked this conversation as resolved.
Show resolved Hide resolved
the compressed data. *compresslevel* and *mtime* have the same meaning as in
the :class:`GzipFile` constructor above.
the :class:`GzipFile` constructor above,
but *mtime* defaults to 0 for reproducible output.

.. versionadded:: 3.2
.. versionchanged:: 3.8
Expand All @@ -203,6 +204,10 @@ The module defines the following items:
.. versionchanged:: 3.13
The gzip header OS byte is guaranteed to be set to 255 when this function
is used as was the case in 3.10 and earlier.
.. versionchanged:: 3.14
The *mtime* parameter now defaults to 0 for reproducible output.
bmwiedemann marked this conversation as resolved.
Show resolved Hide resolved
For the previous behaviour of using the current time,
pass ``None`` to *mtime*.

.. function:: decompress(data)

Expand Down
6 changes: 3 additions & 3 deletions Lib/gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,12 +580,12 @@ def _rewind(self):
self._new_member = True


def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=None):
def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=0):
bmwiedemann marked this conversation as resolved.
Show resolved Hide resolved
"""Compress data in one shot and return the compressed string.

compresslevel sets the compression level in range of 0-9.
mtime can be used to set the modification time. The modification time is
set to the current time by default.
mtime can be used to set the modification time.
The modification time is set to 0 by default, for reproducibility.
"""
# Wbits=31 automatically includes a gzip header and trailer.
gzip_data = zlib.compress(data, level=compresslevel, wbits=31)
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,17 @@ def test_compress_mtime(self):
f.read(1) # to set mtime attribute
self.assertEqual(f.mtime, mtime)

def test_compress_mtime_default(self):
# test for gh-125260
datac = gzip.compress(data1, mtime=0)
datac2 = gzip.compress(data1)
self.assertEqual(datac, datac2)
datac3 = gzip.compress(data1, mtime=None)
self.assertNotEqual(datac, datac3)
bmwiedemann marked this conversation as resolved.
Show resolved Hide resolved
with gzip.GzipFile(fileobj=io.BytesIO(datac3), mode="rb") as f:
f.read(1) # to set mtime attribute
self.assertGreater(f.mtime, 1)

def test_compress_correct_level(self):
for mtime in (0, 42):
with self.subTest(mtime=mtime):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The :func:`gzip.compress` *mtime* parameter now defaults to 0 for reproducible output.
bmwiedemann marked this conversation as resolved.
Show resolved Hide resolved
Patch by Bernhard M. Wiedemann and Adam Turner.
Loading