Skip to content

Commit 3750071

Browse files
committed
gh-125260: gzip: let compress mtime default to 0
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.
1 parent 01fc3b3 commit 3750071

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

Doc/library/gzip.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ The module defines the following items:
184184
attribute instead.
185185

186186

187-
.. function:: compress(data, compresslevel=9, *, mtime=None)
187+
.. function:: compress(data, compresslevel=9, *, mtime=0)
188188

189189
Compress the *data*, returning a :class:`bytes` object containing
190190
the compressed data. *compresslevel* and *mtime* have the same meaning as in
@@ -203,6 +203,8 @@ The module defines the following items:
203203
.. versionchanged:: 3.13
204204
The gzip header OS byte is guaranteed to be set to 255 when this function
205205
is used as was the case in 3.10 and earlier.
206+
.. versionchanged:: 3.14
207+
The *mtime* parameter now defaults to 0 for reproducible output.
206208

207209
.. function:: decompress(data)
208210

Lib/gzip.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,7 @@ def _rewind(self):
579579
super()._rewind()
580580
self._new_member = True
581581

582-
583-
def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=None):
582+
def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=0):
584583
"""Compress data in one shot and return the compressed string.
585584
586585
compresslevel sets the compression level in range of 0-9.

Lib/test/test_gzip.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,10 @@ def test_compress_correct_level(self):
717717
for mtime in (0, 42):
718718
with self.subTest(mtime=mtime):
719719
nocompress = gzip.compress(data1, compresslevel=0, mtime=mtime)
720+
if mtime == 0:
721+
# test for gh-125260
722+
nocompressnomtime = gzip.compress(data1, compresslevel=0)
723+
self.assertEqual(nocompress, nocompressnomtime)
720724
yescompress = gzip.compress(data1, compresslevel=1, mtime=mtime)
721725
self.assertIn(data1, nocompress)
722726
self.assertNotIn(data1, yescompress)

0 commit comments

Comments
 (0)