From d5461ddd27e2cad46c3681c8336a21c42f676049 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Tue, 16 Jan 2024 14:03:32 +0100 Subject: [PATCH 1/7] Do not use a separate codepath for gzip.compress --- Lib/gzip.py | 4 ---- Lib/test/test_gzip.py | 9 +++------ 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/Lib/gzip.py b/Lib/gzip.py index 177f9080dc5af8..b3b50cf5f309a1 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -606,10 +606,6 @@ def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=None): mtime can be used to set the modification time. The modification time is set to the current time by default. """ - if mtime == 0: - # Use zlib as it creates the header with 0 mtime by default. - # This is faster and with less overhead. - return zlib.compress(data, level=compresslevel, wbits=31) header = _create_simple_gzip_header(compresslevel, mtime) trailer = struct.pack(" Date: Tue, 16 Jan 2024 14:08:41 +0100 Subject: [PATCH 2/7] Remove separate codepath from the documentation --- Doc/library/gzip.rst | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index 50cde09fa10a9d..ad963a32a09495 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -184,17 +184,15 @@ The module defines the following items: Compress the *data*, returning a :class:`bytes` object containing the compressed data. *compresslevel* and *mtime* have the same meaning as in - the :class:`GzipFile` constructor above. When *mtime* is set to ``0``, this - function is equivalent to :func:`zlib.compress` with *wbits* set to ``31``. - The zlib function is faster. + the :class:`GzipFile` constructor above. + :func:`zlib.compress` with *wbits* set to ``31`` is faster. .. versionadded:: 3.2 .. versionchanged:: 3.8 Added the *mtime* parameter for reproducible output. .. versionchanged:: 3.11 Speed is improved by compressing all data at once instead of in a - streamed fashion. Calls with *mtime* set to ``0`` are delegated to - :func:`zlib.compress` for better speed. + streamed fashion. .. function:: decompress(data) From aafaa3de80e953930d41ceb1f7b32b97a27a584b Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Tue, 16 Jan 2024 14:15:58 +0100 Subject: [PATCH 3/7] Return compress correct level test --- Lib/test/test_gzip.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index cd4c96378a4050..2d2164f62d8404 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -553,6 +553,15 @@ def test_compress_mtime(self): f.read(1) # to set mtime attribute self.assertEqual(f.mtime, mtime) + def test_compress_correct_level(self): + for mtime in (0, 42): + with self.subTest(mtime=mtime): + nocompress = gzip.compress(data1, compresslevel=0, mtime=mtime) + yescompress = gzip.compress(data1, compresslevel=1, + mtime=mtime) + self.assertIn(data1, nocompress) + self.assertNotIn(data1, yescompress) + def test_compress_os_byte_set(self): for mtime in (0, 42): with self.subTest(mtime=mtime): From ac72e3cba56b3b392a3d934db8e323c36f2656b4 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Tue, 16 Jan 2024 14:18:41 +0100 Subject: [PATCH 4/7] Also test os byte --- Lib/test/test_gzip.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index 2d2164f62d8404..0278aa88fac077 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -562,11 +562,12 @@ def test_compress_correct_level(self): self.assertIn(data1, nocompress) self.assertNotIn(data1, yescompress) - def test_compress_os_byte_set(self): + def test_issue112346(self): + # The OS byte should be 255, this should not change between Python versions. for mtime in (0, 42): with self.subTest(mtime=mtime): compress = gzip.compress(data1, compresslevel=1, mtime=mtime) - assert struct.unpack("I", compress[4:8]) == (mtime,) + assert struct.unpack("IxB", compress[4:10]) == (mtime, 255) def test_decompress(self): for data in (data1, data2): From 2c165b18c374390a6efbd05e5c52a3afd482a8ad Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Mon, 15 Apr 2024 13:42:08 +0200 Subject: [PATCH 5/7] Always use little endian when unpacking gzip header --- Lib/test/test_gzip.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index 0278aa88fac077..f9dcef2570e238 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -567,7 +567,7 @@ def test_issue112346(self): for mtime in (0, 42): with self.subTest(mtime=mtime): compress = gzip.compress(data1, compresslevel=1, mtime=mtime) - assert struct.unpack("IxB", compress[4:10]) == (mtime, 255) + assert struct.unpack(" Date: Wed, 12 Jun 2024 09:58:37 +0200 Subject: [PATCH 6/7] Use proper assertEqual call --- Lib/test/test_gzip.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index f9dcef2570e238..a169002884b46f 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -567,7 +567,11 @@ def test_issue112346(self): for mtime in (0, 42): with self.subTest(mtime=mtime): compress = gzip.compress(data1, compresslevel=1, mtime=mtime) - assert struct.unpack(" Date: Wed, 12 Jun 2024 10:00:35 +0200 Subject: [PATCH 7/7] Add blurb for change. --- .../next/Library/2024-06-12-10-00-31.gh-issue-90425.5CfkKG.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-06-12-10-00-31.gh-issue-90425.5CfkKG.rst diff --git a/Misc/NEWS.d/next/Library/2024-06-12-10-00-31.gh-issue-90425.5CfkKG.rst b/Misc/NEWS.d/next/Library/2024-06-12-10-00-31.gh-issue-90425.5CfkKG.rst new file mode 100644 index 00000000000000..9490420e20b57a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-06-12-10-00-31.gh-issue-90425.5CfkKG.rst @@ -0,0 +1,2 @@ +The OS byte in gzip headers is now always set to 255 when using +gzip.compress.