Skip to content

Commit

Permalink
gh-125260: gzip: let compress mtime default to 0
Browse files Browse the repository at this point in the history
this follows GNU gzip, which defaults to
store 0 as mtime for compressing stdin, where no file mtime is involved

This makes gzip.compress(str) output deterministic by default
and greatly helps reproducible builds.
  • Loading branch information
bmwiedemann committed Oct 11, 2024
1 parent 01fc3b3 commit 3750071
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Doc/library/gzip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ 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
the compressed data. *compresslevel* and *mtime* have the same meaning as in
Expand All @@ -203,6 +203,8 @@ 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.

.. function:: decompress(data)

Expand Down
3 changes: 1 addition & 2 deletions Lib/gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,7 @@ def _rewind(self):
super()._rewind()
self._new_member = True


def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=None):
def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=0):
"""Compress data in one shot and return the compressed string.
compresslevel sets the compression level in range of 0-9.
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,10 @@ def test_compress_correct_level(self):
for mtime in (0, 42):
with self.subTest(mtime=mtime):
nocompress = gzip.compress(data1, compresslevel=0, mtime=mtime)
if mtime == 0:
# test for gh-125260
nocompressnomtime = gzip.compress(data1, compresslevel=0)
self.assertEqual(nocompress, nocompressnomtime)
yescompress = gzip.compress(data1, compresslevel=1, mtime=mtime)
self.assertIn(data1, nocompress)
self.assertNotIn(data1, yescompress)
Expand Down

0 comments on commit 3750071

Please sign in to comment.