From 7d8287472df728e8fe26170a0fb869ee38cf9ce4 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" <68491+gpshead@users.noreply.github.com> Date: Sun, 1 Nov 2026 10:11:30 -0800 Subject: [PATCH] [3.14] gh-161086: Honor --fast and --best for file arguments in gzip CLI (GH-161087) The file branch of gzip.main() did not pass the compression level to gzip.open(), so only stdin input was affected by --fast/--best. (cherry picked from commit 8a316fff5f1093d1d877a8e2f38084f74e5008b0) --- Lib/gzip.py | 2 +- Lib/test/test_gzip.py | 5 ++++- .../Library/2026-10-22-05-24-34.gh-issue-161086.FGPjqA.rst | 2 ++ 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-10-22-05-24-34.gh-issue-161086.FGPjqA.rst diff --git a/Lib/gzip.py b/Lib/gzip.py index 2c6051a4aa52dc4..721c6e2f49eab9d 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -732,7 +732,7 @@ def main(): compresslevel=compresslevel) else: f = builtins.open(arg, "rb") - g = open(arg + ".gz", "wb") + g = open(arg + ".gz", "wb", compresslevel=compresslevel) while True: chunk = f.read(READ_BUFFER_SIZE) if not chunk: diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index 24922a5a72eecad..6aff3b2212d7e25 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -1189,7 +1189,8 @@ def test_compress_infile_outfile_default(self): @create_and_remove_directory(TEMPDIR) def test_compress_infile_outfile(self): - for compress_level in ('--fast', '--best'): + # The XFL header byte records the compression level used. + for compress_level, xfl in (('--fast', 4), ('--best', 2)): with self.subTest(compress_level=compress_level): local_testgzip = os.path.join(TEMPDIR, 'testgzip') gzipname = local_testgzip + '.gz' @@ -1203,6 +1204,8 @@ def test_compress_infile_outfile(self): self.assertTrue(os.path.exists(gzipname)) self.assertEqual(out, b'') self.assertEqual(err, b'') + with open(gzipname, 'rb') as fp: + self.assertEqual(fp.read()[8], xfl) os.remove(gzipname) self.assertFalse(os.path.exists(gzipname)) diff --git a/Misc/NEWS.d/next/Library/2026-10-22-05-24-34.gh-issue-161086.FGPjqA.rst b/Misc/NEWS.d/next/Library/2026-10-22-05-24-34.gh-issue-161086.FGPjqA.rst new file mode 100644 index 000000000000000..d94fadf8f8a6528 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-22-05-24-34.gh-issue-161086.FGPjqA.rst @@ -0,0 +1,2 @@ +Fix the :mod:`gzip` command-line interface ignoring :option:`gzip --fast` +and :option:`gzip --best` when compressing files given as arguments.