The module defines the following items:
-.. function:: open(filename, mode='rb', compresslevel=6, encoding=None, errors=None, newline=None)
+.. function:: open(filename, mode='rb', compresslevel=6, encoding=None, errors=None, newline=None, *, mtime=None)
Open a gzip-compressed file in binary or text mode, returning a :term:`file
object`.
The *compresslevel* argument is an integer from 0 to 9, as for the
:class:`GzipFile` constructor.
+ The keyword-only argument *mtime* represents a Unix timestamp.
+
For binary mode, this function is equivalent to the :class:`GzipFile`
- constructor: ``GzipFile(filename, mode, compresslevel)``. In this case, the
- *encoding*, *errors* and *newline* arguments must not be provided.
+ constructor: ``GzipFile(filename, mode, compresslevel, mtime=mtime)``.
+ In this case, the *encoding*, *errors* and *newline* arguments must not
+ be provided.
For text mode, a :class:`GzipFile` object is created, and wrapped in an
:class:`io.TextIOWrapper` instance with the specified encoding, error
It is the default level used by most compression tools and a better
tradeoff between speed and performance.
+ .. versionchanged:: next
+ Added keyword-only argument *mtime* which is passed to the class
+ constructor of :class:`~gzip.GzipFile`.
+
.. exception:: BadGzipFile
An exception raised for invalid gzip files. It inherits from :exc:`OSError`.
Improved modules
================
+
+gzip
+----
+
+* :func:`gzip.open` now accepts an optional argument ``mtime``
+ which is passed on to the constructor of the :class:`~gzip.GzipFile` class.
+ (Contributed by Marin Misur in :gh:`91372`.)
+
os
--
def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_TRADEOFF,
- 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
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")
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_mtime_out_of_range(self):
for mtime in (-1, 2**32):
with gzip.GzipFile(self.filename, 'w', mtime=mtime) as fWrite:
--- /dev/null
+Added *mtime* option to :func:`gzip.open`, which will be passed
+to the constructor of :class:`~gzip.GzipFile`.