Skip to content

Commit e1484f1

Browse files
authored
[3.14] gh-161086: Honor --fast and --best for file arguments in gzip CLI (GH-161087) (#161119)
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 f11ed23 commit e1484f1

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
@@ -732,7 +732,7 @@ def main():
732732
compresslevel=compresslevel)
733733
else:
734734
f = builtins.open(arg, "rb")
735-
g = open(arg + ".gz", "wb")
735+
g = open(arg + ".gz", "wb", compresslevel=compresslevel)
736736
while True:
737737
chunk = f.read(READ_BUFFER_SIZE)
738738
if not chunk:

‎Lib/test/test_gzip.py‎

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

11901190
@create_and_remove_directory(TEMPDIR)
11911191
def test_compress_infile_outfile(self):
1192-
for compress_level in ('--fast', '--best'):
1192+
# The XFL header byte records the compression level used.
1193+
for compress_level, xfl in (('--fast', 4), ('--best', 2)):
11931194
with self.subTest(compress_level=compress_level):
11941195
local_testgzip = os.path.join(TEMPDIR, 'testgzip')
11951196
gzipname = local_testgzip + '.gz'
@@ -1203,6 +1204,8 @@ def test_compress_infile_outfile(self):
12031204
self.assertTrue(os.path.exists(gzipname))
12041205
self.assertEqual(out, b'')
12051206
self.assertEqual(err, b'')
1207+
with open(gzipname, 'rb') as fp:
1208+
self.assertEqual(fp.read()[8], xfl)
12061209
os.remove(gzipname)
12071210
self.assertFalse(os.path.exists(gzipname))
12081211

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)