Skip to content

Commit cf5c8c5

Browse files
authored
gh-91372: Add mtime to gzip.open() (GH-32310)
1 parent 9604fa8 commit cf5c8c5

5 files changed

Lines changed: 35 additions & 6 deletions

File tree

Doc/library/gzip.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Note that additional file formats which can be decompressed by the
2828
The module defines the following items:
2929

3030

31-
.. function:: open(filename, mode='rb', compresslevel=6, encoding=None, errors=None, newline=None)
31+
.. function:: open(filename, mode='rb', compresslevel=6, encoding=None, errors=None, newline=None, *, mtime=None)
3232

3333
Open a gzip-compressed file in binary or text mode, returning a :term:`file
3434
object`.
@@ -43,9 +43,12 @@ The module defines the following items:
4343
The *compresslevel* argument is an integer from 0 to 9, as for the
4444
:class:`GzipFile` constructor.
4545

46+
The keyword-only argument *mtime* represents a Unix timestamp.
47+
4648
For binary mode, this function is equivalent to the :class:`GzipFile`
47-
constructor: ``GzipFile(filename, mode, compresslevel)``. In this case, the
48-
*encoding*, *errors* and *newline* arguments must not be provided.
49+
constructor: ``GzipFile(filename, mode, compresslevel, mtime=mtime)``.
50+
In this case, the *encoding*, *errors* and *newline* arguments must not
51+
be provided.
4952

5053
For text mode, a :class:`GzipFile` object is created, and wrapped in an
5154
:class:`io.TextIOWrapper` instance with the specified encoding, error
@@ -66,6 +69,10 @@ The module defines the following items:
6669
It is the default level used by most compression tools and a better
6770
tradeoff between speed and performance.
6871

72+
.. versionchanged:: next
73+
Added keyword-only argument *mtime* which is passed to the class
74+
constructor of :class:`~gzip.GzipFile`.
75+
6976
.. exception:: BadGzipFile
7077

7178
An exception raised for invalid gzip files. It inherits from :exc:`OSError`.

Doc/whatsnew/3.16.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ New modules
8686
Improved modules
8787
================
8888

89+
90+
gzip
91+
----
92+
93+
* :func:`gzip.open` now accepts an optional argument ``mtime``
94+
which is passed on to the constructor of the :class:`~gzip.GzipFile` class.
95+
(Contributed by Marin Misur in :gh:`91372`.)
96+
8997
os
9098
--
9199

Lib/gzip.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232

3333
def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_TRADEOFF,
34-
encoding=None, errors=None, newline=None):
34+
encoding=None, errors=None, newline=None, *, mtime=None):
3535
"""Open a gzip-compressed file in binary or text mode.
3636
3737
The filename argument can be an actual filename (a str or bytes object), or
@@ -63,9 +63,10 @@ def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_TRADEOFF,
6363

6464
gz_mode = mode.replace("t", "")
6565
if isinstance(filename, (str, bytes, os.PathLike)):
66-
binary_file = GzipFile(filename, gz_mode, compresslevel)
66+
binary_file = GzipFile(filename, gz_mode, compresslevel, mtime=mtime)
6767
elif hasattr(filename, "read") or hasattr(filename, "write"):
68-
binary_file = GzipFile(None, gz_mode, compresslevel, filename)
68+
binary_file = GzipFile(None, gz_mode, compresslevel, filename,
69+
mtime=mtime)
6970
else:
7071
raise TypeError("filename must be a str or bytes object, or a file")
7172

Lib/test/test_gzip.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,17 @@ def test_mtime(self):
351351
self.assertEqual(dataRead, data1)
352352
self.assertEqual(fRead.mtime, mtime)
353353

354+
def test_mtime_with_open(self):
355+
mtime = 123456789
356+
with gzip.open(self.filename, "wb", mtime=mtime) as fWrite:
357+
fWrite.write(data1)
358+
with gzip.open(self.filename, "rb") as fRead:
359+
self.assertTrue(hasattr(fRead, 'mtime'))
360+
self.assertIsNone(fRead.mtime)
361+
dataRead = fRead.read()
362+
self.assertEqual(dataRead, data1)
363+
self.assertEqual(fRead.mtime, mtime)
364+
354365
def test_mtime_out_of_range(self):
355366
for mtime in (-1, 2**32):
356367
with gzip.GzipFile(self.filename, 'w', mtime=mtime) as fWrite:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added *mtime* option to :func:`gzip.open`, which will be passed
2+
to the constructor of :class:`~gzip.GzipFile`.

0 commit comments

Comments
 (0)