Skip to content

Commit 69b7d35

Browse files
authored
[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 8a316ff)
1 parent ae13f1b commit 69b7d35

3 files changed

Lines changed: 7 additions & 2 deletions

File tree

‎Lib/gzip.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ def main():
762762
compresslevel=compresslevel)
763763
else:
764764
f = builtins.open(arg, "rb")
765-
g = open(arg + ".gz", "wb")
765+
g = open(arg + ".gz", "wb", compresslevel=compresslevel)
766766
while True:
767767
chunk = f.read(READ_BUFFER_SIZE)
768768
if not chunk:

‎Lib/test/test_gzip.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,8 @@ def test_compress_infile_outfile_default(self):
12181218

12191219
@create_and_remove_directory(TEMPDIR)
12201220
def test_compress_infile_outfile(self):
1221-
for compress_level in ('--fast', '--best'):
1221+
# The XFL header byte records the compression level used.
1222+
for compress_level, xfl in (('--fast', 4), ('--best', 2)):
12221223
with self.subTest(compress_level=compress_level):
12231224
local_testgzip = os.path.join(TEMPDIR, 'testgzip')
12241225
gzipname = local_testgzip + '.gz'
@@ -1232,6 +1233,8 @@ def test_compress_infile_outfile(self):
12321233
self.assertTrue(os.path.exists(gzipname))
12331234
self.assertEqual(out, b'')
12341235
self.assertEqual(err, b'')
1236+
with open(gzipname, 'rb') as fp:
1237+
self.assertEqual(fp.read()[8], xfl)
12351238
os.remove(gzipname)
12361239
self.assertFalse(os.path.exists(gzipname))
12371240

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the :mod:`gzip` command-line interface ignoring :option:`gzip --fast`
2+
and :option:`gzip --best` when compressing files given as arguments.

0 commit comments

Comments
 (0)