Skip to content

Commit 8a316ff

Browse files
authored
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.
1 parent 9348e13 commit 8a316ff

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
@@ -763,7 +763,7 @@ def main():
763763
compresslevel=compresslevel)
764764
else:
765765
f = builtins.open(arg, "rb")
766-
g = open(arg + ".gz", "wb")
766+
g = open(arg + ".gz", "wb", compresslevel=compresslevel)
767767
while True:
768768
chunk = f.read(READ_BUFFER_SIZE)
769769
if not chunk:

‎Lib/test/test_gzip.py‎

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

12301230
@create_and_remove_directory(TEMPDIR)
12311231
def test_compress_infile_outfile(self):
1232-
for compress_level in ('--fast', '--best'):
1232+
# The XFL header byte records the compression level used.
1233+
for compress_level, xfl in (('--fast', 4), ('--best', 2)):
12331234
with self.subTest(compress_level=compress_level):
12341235
local_testgzip = os.path.join(TEMPDIR, 'testgzip')
12351236
gzipname = local_testgzip + '.gz'
@@ -1243,6 +1244,8 @@ def test_compress_infile_outfile(self):
12431244
self.assertTrue(os.path.exists(gzipname))
12441245
self.assertEqual(out, b'')
12451246
self.assertEqual(err, b'')
1247+
with open(gzipname, 'rb') as fp:
1248+
self.assertEqual(fp.read()[8], xfl)
12461249
os.remove(gzipname)
12471250
self.assertFalse(os.path.exists(gzipname))
12481251

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)