From 9bebf8dee79f677bddee2387b77e1db401fdb175 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Thu, 22 Oct 2026 05:25:16 +0000 Subject: [PATCH] gh-161086: Honor --fast and --best for file arguments in gzip CLI The file branch of gzip.main() did not pass the compression level to gzip.open(), so only stdin input was affected by --fast/--best. --- 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 95c05fb98e47e7..7e6a5197234ccc 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -763,7 +763,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 8a0e82ea53ea2d..1d37ba2f6817f7 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -1229,7 +1229,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' @@ -1243,6 +1244,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 00000000000000..d94fadf8f8a652 --- /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.