diff options
Diffstat (limited to 'Lib')
| -rw-r--r-- | Lib/gzip.py | 4 | ||||
| -rw-r--r-- | Lib/test/test_gzip.py | 11 |
2 files changed, 13 insertions, 2 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py index 151ff1405b..948fec293e 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -520,12 +520,12 @@ class _GzipReader(_compression.DecompressReader): super()._rewind() self._new_member = True -def compress(data, compresslevel=_COMPRESS_LEVEL_BEST): +def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=None): """Compress data in one shot and return the compressed string. Optional argument is the compression level, in range of 0-9. """ buf = io.BytesIO() - with GzipFile(fileobj=buf, mode='wb', compresslevel=compresslevel) as f: + with GzipFile(fileobj=buf, mode='wb', compresslevel=compresslevel, mtime=mtime) as f: f.write(data) return buf.getvalue() diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index 1e8b41f07b..2c8f854c64 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -499,6 +499,17 @@ class TestGzip(BaseTest): with gzip.GzipFile(fileobj=io.BytesIO(datac), mode="rb") as f: self.assertEqual(f.read(), data) + def test_compress_mtime(self): + mtime = 123456789 + for data in [data1, data2]: + for args in [(), (1,), (6,), (9,)]: + with self.subTest(data=data, args=args): + datac = gzip.compress(data, *args, mtime=mtime) + self.assertEqual(type(datac), bytes) + with gzip.GzipFile(fileobj=io.BytesIO(datac), mode="rb") as f: + f.read(1) # to set mtime attribute + self.assertEqual(f.mtime, mtime) + def test_decompress(self): for data in (data1, data2): buf = io.BytesIO() |
