https://github.com/python/cpython/commit/8a316fff5f1093d1d877a8e2f38084f74e5008b0
commit: 8a316fff5f1093d1d877a8e2f38084f74e5008b0
branch: main
author: Gregory P. Smith <[email protected]>
committer: gpshead <[email protected]>
date: 2026-11-01T18:11:30Z
summary:

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.

files:
A Misc/NEWS.d/next/Library/2026-10-22-05-24-34.gh-issue-161086.FGPjqA.rst
M Lib/gzip.py
M Lib/test/test_gzip.py

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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to