From 0cfe35069428c9760c749c28f4c7f04065cbda5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Fri, 12 Oct 2018 20:47:55 +0200 Subject: [PATCH 01/14] Add two mutually exclusive parameters for the command line of gzip. --fast and --best are the new parameters. They indicate to Gzip if we want to have a fast compression method or a slow method (best compression). --- Doc/library/gzip.rst | 19 +++++++++++++++++++ Lib/gzip.py | 25 ++++++++++++++++++++----- Lib/test/test_gzip.py | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 6 deletions(-) diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index b52dd1a11aa9f9..424cbb2f88d027 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -234,6 +234,25 @@ Command line options If *file* is not specified, read from :attr:`sys.stdin`. +.. cmdoption:: -1, --fast + + Indicates the fastest compression method (less compression) + + .. code-block:: shell-session + + $ python -m gzip -1 file + $ python -m gzip --fast file + +.. cmdoption:: -9, --best + + Indicates the slowest compression method (best compression). + This is the default method if you do not specify any flag. + + .. code-block:: shell-session + + $ python -m gzip -9 file + $ python -m gzip --best file + .. cmdoption:: -d, --decompress Decompress the given file diff --git a/Lib/gzip.py b/Lib/gzip.py index a34d01ae36e121..96364559ca0f4c 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -17,7 +17,11 @@ READ, WRITE = 1, 2 -def open(filename, mode="rb", compresslevel=9, +COMPRESS_LEVEL_FAST = 1 +COMPRESS_LEVEL_BEST = 9 + + +def open(filename, mode="rb", compresslevel=COMPRESS_LEVEL_BEST, encoding=None, errors=None, newline=None): """Open a gzip-compressed file in binary or text mode. @@ -121,7 +125,7 @@ class GzipFile(_compression.BaseStream): myfileobj = None def __init__(self, filename=None, mode=None, - compresslevel=9, fileobj=None, mtime=None): + compresslevel=COMPRESS_LEVEL_BEST, fileobj=None, mtime=None): """Constructor for the GzipFile class. At least one of fileobj and filename must be given a @@ -515,7 +519,7 @@ def _rewind(self): super()._rewind() self._new_member = True -def compress(data, compresslevel=9): +def compress(data, compresslevel=COMPRESS_LEVEL_BEST): """Compress data in one shot and return the compressed string. Optional argument is the compression level, in range of 0-9. """ @@ -539,12 +543,22 @@ def main(): "but do not delete the input file.") parser.add_argument("-d", "--decompress", action="store_true", help="act like gunzip instead of gzip") + group = parser.add_mutually_exclusive_group() + group.add_argument('-1', '--fast', action='store_true', help='compress faster') + group.add_argument('-9', '--best', action='store_true', help='compress bester') + parser.add_argument("args", nargs="*", default=["-"], metavar='file') args = parser.parse_args() + + compresslevel = COMPRESS_LEVEL_BEST + if args.fast: + compresslevel = COMPRESS_LEVEL_FAST + for arg in args.args: if args.decompress: if arg == "-": - f = GzipFile(filename="", mode="rb", fileobj=sys.stdin.buffer) + f = GzipFile(filename="", mode="rb", fileobj=sys.stdin.buffer, + compresslevel=compresslevel) g = sys.stdout.buffer else: if arg[-3:] != ".gz": @@ -555,7 +569,8 @@ def main(): else: if arg == "-": f = sys.stdin.buffer - g = GzipFile(filename="", mode="wb", fileobj=sys.stdout.buffer) + g = GzipFile(filename="", mode="wb", fileobj=sys.stdout.buffer, + compresslevel=compresslevel) else: f = builtins.open(arg, "rb") g = open(arg + ".gz", "wb") diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index b072ce4682c0fe..e009a621531d71 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -12,7 +12,7 @@ from subprocess import PIPE, Popen from test import support from test.support import _4G, bigmemtest -from test.support.script_helper import assert_python_ok +from test.support.script_helper import assert_python_ok, assert_python_failure gzip = support.import_module('gzip') @@ -687,6 +687,19 @@ def wrapper(*args, **kwargs): return decorator +def add_compress_level_flag(*compress_levels): + def decorator(function): + @functools.wraps(function) + def wrapper(*args, **kwargs): + self = args[0] + for compress_level in compress_levels: + with self.subTest(compress_level=compress_level): + kwargs.update(compress_level=compress_level) + function(*args, **kwargs) + return wrapper + return decorator + + class TestCommandLine(unittest.TestCase): data = b'This is a simple test with gzip' @@ -750,6 +763,30 @@ def test_compress_infile_outfile(self): self.assertEqual(out, b'') self.assertEqual(err, b'') + @add_compress_level_flag('-1', '--fast', '-9', '--best') + @create_and_remove_directory(TEMPDIR) + def test_compress_infile_outfile(self, compress_level): + local_testgzip = os.path.join(TEMPDIR, 'testgzip') + gzipname = local_testgzip + '.gz' + self.assertFalse(os.path.exists(gzipname)) + + with open(local_testgzip, 'wb') as fp: + fp.write(self.data) + + rc, out, err = assert_python_ok('-m', 'gzip', compress_level, local_testgzip) + + self.assertTrue(os.path.exists(gzipname)) + self.assertEqual(rc, 0) + self.assertEqual(out, b'') + self.assertEqual(err, b'') + + + def test_compress_fast_best_are_exclusive(self): + rc, out, err = assert_python_failure('-m', 'gzip', '-1', '-9') + self.assertIn(b"error: argument -9/--best: not allowed with argument -1/--fast", err) + self.assertGreater(rc, 0) + self.assertEqual(out, b'') + def test_main(verbose=None): support.run_unittest(TestGzip, TestOpen, TestCommandLine) From a0d6e0c94fe13c75d8b86891683523ad5e51ee73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Sat, 13 Oct 2018 07:47:02 +0200 Subject: [PATCH 02/14] Add the blurb entry with the bpo number --- .../next/Library/2018-10-13-07-46-50.bpo-34969.Mfnhjb.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2018-10-13-07-46-50.bpo-34969.Mfnhjb.rst diff --git a/Misc/NEWS.d/next/Library/2018-10-13-07-46-50.bpo-34969.Mfnhjb.rst b/Misc/NEWS.d/next/Library/2018-10-13-07-46-50.bpo-34969.Mfnhjb.rst new file mode 100644 index 00000000000000..35346bf6aa357e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-10-13-07-46-50.bpo-34969.Mfnhjb.rst @@ -0,0 +1,3 @@ +Add --fast, --best on the gzip CLI, these parameters will be used for the +fast compression method (quick) or the best method compress (slower, but +smaller file) From 4347fc80231618c3469ebfe0f0dac5af10ff312d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Sat, 13 Oct 2018 08:04:47 +0200 Subject: [PATCH 03/14] make patchcheck --- Lib/gzip.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/gzip.py b/Lib/gzip.py index 96364559ca0f4c..1c9275275d73ea 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -552,7 +552,7 @@ def main(): compresslevel = COMPRESS_LEVEL_BEST if args.fast: - compresslevel = COMPRESS_LEVEL_FAST + compresslevel = COMPRESS_LEVEL_FAST for arg in args.args: if args.decompress: From 6fde31307297d5b11bcb8cf45fd6d2d790cd1c61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Sat, 13 Oct 2018 10:47:49 +0200 Subject: [PATCH 04/14] by default, the compression level will be 6 (tradeoff), and not 9 (best) --- Doc/library/gzip.rst | 2 ++ Lib/gzip.py | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index 424cbb2f88d027..2276c0fc520d28 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -222,6 +222,8 @@ Once executed the :mod:`gzip` module keeps the input file(s). .. versionchanged:: 3.8 Add a new command line interface with a usage. + By default, when you will execute the CLI, the default compression level is 6, + it's a good tradeoff between the best and fast compression methods. Command line options ^^^^^^^^^^^^^^^^^^^^ diff --git a/Lib/gzip.py b/Lib/gzip.py index 1c9275275d73ea..348128afc90daa 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -18,6 +18,7 @@ READ, WRITE = 1, 2 COMPRESS_LEVEL_FAST = 1 +COMPRESS_LEVEL_TRADEOFF = 6 COMPRESS_LEVEL_BEST = 9 @@ -550,9 +551,11 @@ def main(): parser.add_argument("args", nargs="*", default=["-"], metavar='file') args = parser.parse_args() - compresslevel = COMPRESS_LEVEL_BEST + compresslevel = COMPRESS_LEVEL_TRADEOFF if args.fast: compresslevel = COMPRESS_LEVEL_FAST + elif args.best: + compresslevel = COMPRESS_LEVEL_BEST for arg in args.args: if args.decompress: From 737737c6a0253596c229e803dea4f1156924487f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Sat, 13 Oct 2018 12:25:49 +0200 Subject: [PATCH 05/14] does not allow the compresslevel for the decompress method --- Lib/gzip.py | 7 +++---- Lib/test/test_gzip.py | 7 ++++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Lib/gzip.py b/Lib/gzip.py index 348128afc90daa..5e0eb96a9db5a5 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -542,11 +542,11 @@ def main(): parser = ArgumentParser(description= "A simple command line interface for the gzip module: act like gzip, " "but do not delete the input file.") - parser.add_argument("-d", "--decompress", action="store_true", - help="act like gunzip instead of gzip") group = parser.add_mutually_exclusive_group() group.add_argument('-1', '--fast', action='store_true', help='compress faster') group.add_argument('-9', '--best', action='store_true', help='compress bester') + group.add_argument("-d", "--decompress", action="store_true", + help="act like gunzip instead of gzip") parser.add_argument("args", nargs="*", default=["-"], metavar='file') args = parser.parse_args() @@ -560,8 +560,7 @@ def main(): for arg in args.args: if args.decompress: if arg == "-": - f = GzipFile(filename="", mode="rb", fileobj=sys.stdin.buffer, - compresslevel=compresslevel) + f = GzipFile(filename="", mode="rb", fileobj=sys.stdin.buffer) g = sys.stdout.buffer else: if arg[-3:] != ".gz": diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index e009a621531d71..ca4731bd07fb8c 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -780,13 +780,18 @@ def test_compress_infile_outfile(self, compress_level): self.assertEqual(out, b'') self.assertEqual(err, b'') - def test_compress_fast_best_are_exclusive(self): rc, out, err = assert_python_failure('-m', 'gzip', '-1', '-9') self.assertIn(b"error: argument -9/--best: not allowed with argument -1/--fast", err) self.assertGreater(rc, 0) self.assertEqual(out, b'') + def test_decompress_cannot_have_flags_compression(self): + rc, out, err = assert_python_failure('-m', 'gzip', '-1', '-d') + self.assertIn(b'error: argument -d/--decompress: not allowed with argument -1/--fast', err) + self.assertGreater(rc, 0) + self.assertEqual(out, b'') + def test_main(verbose=None): support.run_unittest(TestGzip, TestOpen, TestCommandLine) From 6b2c0f299a306fe6a444919d396d03b17cf637d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Sat, 13 Oct 2018 12:35:49 +0200 Subject: [PATCH 06/14] remove the options '-1', '-9' to avoid a future implementation of -2, -3, -4, etc... --- Doc/library/gzip.rst | 6 ++---- Lib/gzip.py | 4 ++-- Lib/test/test_gzip.py | 10 +++++----- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index 2276c0fc520d28..675a98f67ceedc 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -236,23 +236,21 @@ Command line options If *file* is not specified, read from :attr:`sys.stdin`. -.. cmdoption:: -1, --fast +.. cmdoption:: --fast Indicates the fastest compression method (less compression) .. code-block:: shell-session - $ python -m gzip -1 file $ python -m gzip --fast file -.. cmdoption:: -9, --best +.. cmdoption:: --best Indicates the slowest compression method (best compression). This is the default method if you do not specify any flag. .. code-block:: shell-session - $ python -m gzip -9 file $ python -m gzip --best file .. cmdoption:: -d, --decompress diff --git a/Lib/gzip.py b/Lib/gzip.py index 5e0eb96a9db5a5..266f0dc8e17c2f 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -543,8 +543,8 @@ def main(): "A simple command line interface for the gzip module: act like gzip, " "but do not delete the input file.") group = parser.add_mutually_exclusive_group() - group.add_argument('-1', '--fast', action='store_true', help='compress faster') - group.add_argument('-9', '--best', action='store_true', help='compress bester') + group.add_argument('--fast', action='store_true', help='compress faster') + group.add_argument('--best', action='store_true', help='compress bester') group.add_argument("-d", "--decompress", action="store_true", help="act like gunzip instead of gzip") diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index ca4731bd07fb8c..fc1b5a9c9e3fd9 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -763,7 +763,7 @@ def test_compress_infile_outfile(self): self.assertEqual(out, b'') self.assertEqual(err, b'') - @add_compress_level_flag('-1', '--fast', '-9', '--best') + @add_compress_level_flag('--fast', '--best') @create_and_remove_directory(TEMPDIR) def test_compress_infile_outfile(self, compress_level): local_testgzip = os.path.join(TEMPDIR, 'testgzip') @@ -781,14 +781,14 @@ def test_compress_infile_outfile(self, compress_level): self.assertEqual(err, b'') def test_compress_fast_best_are_exclusive(self): - rc, out, err = assert_python_failure('-m', 'gzip', '-1', '-9') - self.assertIn(b"error: argument -9/--best: not allowed with argument -1/--fast", err) + rc, out, err = assert_python_failure('-m', 'gzip', '--fast', '--best') + self.assertIn(b"error: argument --best: not allowed with argument --fast", err) self.assertGreater(rc, 0) self.assertEqual(out, b'') def test_decompress_cannot_have_flags_compression(self): - rc, out, err = assert_python_failure('-m', 'gzip', '-1', '-d') - self.assertIn(b'error: argument -d/--decompress: not allowed with argument -1/--fast', err) + rc, out, err = assert_python_failure('-m', 'gzip', '--fast', '-d') + self.assertIn(b'error: argument -d/--decompress: not allowed with argument --fast', err) self.assertGreater(rc, 0) self.assertEqual(out, b'') From 2867cfbff7b3bdba1ec384f598fb2ada61561c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Sat, 13 Oct 2018 14:44:30 +0200 Subject: [PATCH 07/14] Fix typo, bester -> better --- Lib/gzip.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/gzip.py b/Lib/gzip.py index 266f0dc8e17c2f..9c7a810d50103a 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -544,7 +544,7 @@ def main(): "but do not delete the input file.") group = parser.add_mutually_exclusive_group() group.add_argument('--fast', action='store_true', help='compress faster') - group.add_argument('--best', action='store_true', help='compress bester') + group.add_argument('--best', action='store_true', help='compress better') group.add_argument("-d", "--decompress", action="store_true", help="act like gunzip instead of gzip") From 8b05c8936289ca9b21c62c7be07a4e007f2019f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Sat, 13 Oct 2018 16:18:00 +0200 Subject: [PATCH 08/14] Update with the recommendations of Julien --- Doc/library/gzip.rst | 4 ++-- .../next/Library/2018-10-13-07-46-50.bpo-34969.Mfnhjb.rst | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index 675a98f67ceedc..10ef55c7d5be50 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -238,7 +238,7 @@ Command line options .. cmdoption:: --fast - Indicates the fastest compression method (less compression) + Indicates the fastest compression method (less compression). .. code-block:: shell-session @@ -255,7 +255,7 @@ Command line options .. cmdoption:: -d, --decompress - Decompress the given file + Decompress the given file. .. code-block:: shell-session diff --git a/Misc/NEWS.d/next/Library/2018-10-13-07-46-50.bpo-34969.Mfnhjb.rst b/Misc/NEWS.d/next/Library/2018-10-13-07-46-50.bpo-34969.Mfnhjb.rst index 35346bf6aa357e..3028f6c742c3b4 100644 --- a/Misc/NEWS.d/next/Library/2018-10-13-07-46-50.bpo-34969.Mfnhjb.rst +++ b/Misc/NEWS.d/next/Library/2018-10-13-07-46-50.bpo-34969.Mfnhjb.rst @@ -1,3 +1,3 @@ Add --fast, --best on the gzip CLI, these parameters will be used for the fast compression method (quick) or the best method compress (slower, but -smaller file) +smaller file). Also, change the default compression level to 6 (tradeoff). From f38577afbc526c0e949ecb5fbe5cd509afee61da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Sat, 20 Oct 2018 11:53:39 +0200 Subject: [PATCH 09/14] Remove the examples from the documentation for the CLI of the module gzip. --- Doc/library/gzip.rst | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index 10ef55c7d5be50..0c4c1fd0c1e6e1 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -230,37 +230,20 @@ Command line options .. cmdoption:: file - .. code-block:: shell-session - - $ python -m gzip file - If *file* is not specified, read from :attr:`sys.stdin`. .. cmdoption:: --fast Indicates the fastest compression method (less compression). - .. code-block:: shell-session - - $ python -m gzip --fast file - .. cmdoption:: --best Indicates the slowest compression method (best compression). - This is the default method if you do not specify any flag. - - .. code-block:: shell-session - - $ python -m gzip --best file .. cmdoption:: -d, --decompress Decompress the given file. - .. code-block:: shell-session - - $ python -m gzip -d file.gz - .. cmdoption:: -h, --help Show the help message. From 7bcc2f1a607abe6cd98cef7c8f3dffb28ff1c269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Sat, 20 Oct 2018 11:54:19 +0200 Subject: [PATCH 10/14] Use private constants --- Lib/gzip.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Lib/gzip.py b/Lib/gzip.py index 9c7a810d50103a..151ff1405b164c 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -17,12 +17,12 @@ READ, WRITE = 1, 2 -COMPRESS_LEVEL_FAST = 1 -COMPRESS_LEVEL_TRADEOFF = 6 -COMPRESS_LEVEL_BEST = 9 +_COMPRESS_LEVEL_FAST = 1 +_COMPRESS_LEVEL_TRADEOFF = 6 +_COMPRESS_LEVEL_BEST = 9 -def open(filename, mode="rb", compresslevel=COMPRESS_LEVEL_BEST, +def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_BEST, encoding=None, errors=None, newline=None): """Open a gzip-compressed file in binary or text mode. @@ -126,7 +126,7 @@ class GzipFile(_compression.BaseStream): myfileobj = None def __init__(self, filename=None, mode=None, - compresslevel=COMPRESS_LEVEL_BEST, fileobj=None, mtime=None): + compresslevel=_COMPRESS_LEVEL_BEST, fileobj=None, mtime=None): """Constructor for the GzipFile class. At least one of fileobj and filename must be given a @@ -520,7 +520,7 @@ def _rewind(self): super()._rewind() self._new_member = True -def compress(data, compresslevel=COMPRESS_LEVEL_BEST): +def compress(data, compresslevel=_COMPRESS_LEVEL_BEST): """Compress data in one shot and return the compressed string. Optional argument is the compression level, in range of 0-9. """ @@ -551,11 +551,11 @@ def main(): parser.add_argument("args", nargs="*", default=["-"], metavar='file') args = parser.parse_args() - compresslevel = COMPRESS_LEVEL_TRADEOFF + compresslevel = _COMPRESS_LEVEL_TRADEOFF if args.fast: - compresslevel = COMPRESS_LEVEL_FAST + compresslevel = _COMPRESS_LEVEL_FAST elif args.best: - compresslevel = COMPRESS_LEVEL_BEST + compresslevel = _COMPRESS_LEVEL_BEST for arg in args.args: if args.decompress: From 6547cbec2ba323871b1fcc52b4dfe5d25b46a2f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Thu, 25 Oct 2018 14:22:31 +0200 Subject: [PATCH 11/14] Remove add_compress_level_flag helper in the test of gzip --- Lib/test/test_gzip.py | 45 ++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index fc1b5a9c9e3fd9..047103ad4ae698 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -687,19 +687,6 @@ def wrapper(*args, **kwargs): return decorator -def add_compress_level_flag(*compress_levels): - def decorator(function): - @functools.wraps(function) - def wrapper(*args, **kwargs): - self = args[0] - for compress_level in compress_levels: - with self.subTest(compress_level=compress_level): - kwargs.update(compress_level=compress_level) - function(*args, **kwargs) - return wrapper - return decorator - - class TestCommandLine(unittest.TestCase): data = b'This is a simple test with gzip' @@ -763,22 +750,24 @@ def test_compress_infile_outfile(self): self.assertEqual(out, b'') self.assertEqual(err, b'') - @add_compress_level_flag('--fast', '--best') @create_and_remove_directory(TEMPDIR) - def test_compress_infile_outfile(self, compress_level): - local_testgzip = os.path.join(TEMPDIR, 'testgzip') - gzipname = local_testgzip + '.gz' - self.assertFalse(os.path.exists(gzipname)) - - with open(local_testgzip, 'wb') as fp: - fp.write(self.data) - - rc, out, err = assert_python_ok('-m', 'gzip', compress_level, local_testgzip) - - self.assertTrue(os.path.exists(gzipname)) - self.assertEqual(rc, 0) - self.assertEqual(out, b'') - self.assertEqual(err, b'') + def test_compress_infile_outfile(self): + for compress_level in ('--fast', '--best'): + with self.subTest(compress_level=compress_level): + local_testgzip = os.path.join(TEMPDIR, 'testgzip') + gzipname = local_testgzip + '.gz' + self.assertFalse(os.path.exists(gzipname)) + + with open(local_testgzip, 'wb') as fp: + fp.write(self.data) + + rc, out, err = assert_python_ok('-m', 'gzip', compress_level, local_testgzip) + + self.assertTrue(os.path.exists(gzipname)) + self.assertEqual(out, b'') + self.assertEqual(err, b'') + os.remove(gzipname) + self.assertFalse(os.path.exists(gzipname)) def test_compress_fast_best_are_exclusive(self): rc, out, err = assert_python_failure('-m', 'gzip', '--fast', '--best') From 74647afb28e4fb4bfcfc4f22b32f1a4641fe45ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Thu, 25 Oct 2018 14:24:21 +0200 Subject: [PATCH 12/14] Add the module name as prefix of the blurb entry --- .../next/Library/2018-10-13-07-46-50.bpo-34969.Mfnhjb.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2018-10-13-07-46-50.bpo-34969.Mfnhjb.rst b/Misc/NEWS.d/next/Library/2018-10-13-07-46-50.bpo-34969.Mfnhjb.rst index 3028f6c742c3b4..e3b7132614061e 100644 --- a/Misc/NEWS.d/next/Library/2018-10-13-07-46-50.bpo-34969.Mfnhjb.rst +++ b/Misc/NEWS.d/next/Library/2018-10-13-07-46-50.bpo-34969.Mfnhjb.rst @@ -1,3 +1,3 @@ -Add --fast, --best on the gzip CLI, these parameters will be used for the -fast compression method (quick) or the best method compress (slower, but -smaller file). Also, change the default compression level to 6 (tradeoff). +gzip: Add --fast, --best on the gzip CLI, these parameters will be used for the +fast compression method (quick) or the best method compress (slower, but smaller +file). Also, change the default compression level to 6 (tradeoff). From fdaa34b3c7f75845afcf0ea00a321048924e8bf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Thu, 25 Oct 2018 14:25:47 +0200 Subject: [PATCH 13/14] Reword the sentence about the default value for the compression level --- Doc/library/gzip.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index 0c4c1fd0c1e6e1..a93f37743854ae 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -222,8 +222,7 @@ Once executed the :mod:`gzip` module keeps the input file(s). .. versionchanged:: 3.8 Add a new command line interface with a usage. - By default, when you will execute the CLI, the default compression level is 6, - it's a good tradeoff between the best and fast compression methods. + By default, when you will execute the CLI, the default compression level is 6. Command line options ^^^^^^^^^^^^^^^^^^^^ From 6e67e72044d31e4860d9e001862063f5b8992b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Thu, 25 Oct 2018 14:35:43 +0200 Subject: [PATCH 14/14] remove useless tests about rc==0 --- Lib/test/test_gzip.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index 047103ad4ae698..1e8b41f07b3ecf 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -746,7 +746,6 @@ def test_compress_infile_outfile(self): rc, out, err = assert_python_ok('-m', 'gzip', local_testgzip) self.assertTrue(os.path.exists(gzipname)) - self.assertEqual(rc, 0) self.assertEqual(out, b'') self.assertEqual(err, b'') @@ -772,13 +771,11 @@ def test_compress_infile_outfile(self): def test_compress_fast_best_are_exclusive(self): rc, out, err = assert_python_failure('-m', 'gzip', '--fast', '--best') self.assertIn(b"error: argument --best: not allowed with argument --fast", err) - self.assertGreater(rc, 0) self.assertEqual(out, b'') def test_decompress_cannot_have_flags_compression(self): rc, out, err = assert_python_failure('-m', 'gzip', '--fast', '-d') self.assertIn(b'error: argument -d/--decompress: not allowed with argument --fast', err) - self.assertGreater(rc, 0) self.assertEqual(out, b'')