https://github.com/python/cpython/commit/69b7d357ed9fedf4aaf55bb7be2ea7317344da18
commit: 69b7d357ed9fedf4aaf55bb7be2ea7317344da18
branch: 3.15
author: Gregory P. Smith <[email protected]>
committer: gpshead <[email protected]>
date: 2026-11-01T19:25:13Z
summary:
[3.15] gh-161086: Honor --fast and --best for file arguments in gzip CLI
(GH-161087) (#161118)
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)
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 a478329aef77f7..72a442f4c8f89f 100644
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -762,7 +762,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 f1f5f6069f8ad9..4b249ca3ea2c21 100644
--- a/Lib/test/test_gzip.py
+++ b/Lib/test/test_gzip.py
@@ -1218,7 +1218,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'
@@ -1232,6 +1233,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]