Skip to content
Open
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
2 changes: 1 addition & 1 deletion Doc/library/gzip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Note that additional file formats which can be decompressed by the
The module defines the following items:


.. function:: open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)
.. function:: open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None, *, mtime=None)

Open a gzip-compressed file in binary or text mode, returning a :term:`file
object`.
Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,13 @@ glob
shell-style wildcards to a regular expression.
(Contributed by Barney Gale in :gh:`72904`.)

gzip
---

* :func:`gzip.open` now accepts an optional argument ``mtime``
which is passed on to the init constructor of the GzipFile class.
(Contributed by Marin Misur in :gh:`91372`.)

io
--

Expand Down
6 changes: 3 additions & 3 deletions Lib/gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_BEST,
encoding=None, errors=None, newline=None):
encoding=None, errors=None, newline=None, *, mtime=None):
"""Open a gzip-compressed file in binary or text mode.

The filename argument can be an actual filename (a str or bytes object), or
Expand Down Expand Up @@ -58,9 +58,9 @@ def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_BEST,

gz_mode = mode.replace("t", "")
if isinstance(filename, (str, bytes, os.PathLike)):
binary_file = GzipFile(filename, gz_mode, compresslevel)
binary_file = GzipFile(filename, gz_mode, compresslevel, mtime=mtime)
elif hasattr(filename, "read") or hasattr(filename, "write"):
binary_file = GzipFile(None, gz_mode, compresslevel, filename)
binary_file = GzipFile(None, gz_mode, compresslevel, filename, mtime=mtime)
else:
raise TypeError("filename must be a str or bytes object, or a file")

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 @@ -316,6 +316,17 @@ def test_mtime(self):
self.assertEqual(dataRead, data1)
self.assertEqual(fRead.mtime, mtime)

def test_mtime_with_open(self):
mtime = 123456789
with gzip.open(self.filename, "wb", mtime=mtime) as fWrite:
fWrite.write(data1)
with gzip.open(self.filename, "rb") as fRead:
self.assertTrue(hasattr(fRead, 'mtime'))
self.assertIsNone(fRead.mtime)
dataRead = fRead.read()
self.assertEqual(dataRead, data1)
self.assertEqual(fRead.mtime, mtime)

def test_metadata(self):
mtime = 123456789

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added a mtime option to gzip open().
open() didn't have it, while init of class GzipFile has mtime as an optional argument.