[New-bugs-announce] [issue43316] python -m gzip handles error incorrectly

Ruben Vorderman report at bugs.python.org
Wed Feb 24 09:34:00 EST 2021


New submission from Ruben Vorderman <r.h.p.vorderman at lumc.nl>:

`Python -m gzip -d myfile` will throw an error because myfile does not end in '.gz'. That is fair (even though a bit redundant, GzipFile contains a header check, so why bother checking the extension?).

The problem is how this error is thrown.
1. Error is printed to stdout instead of stderr
2. Tool exits with exit 0.

This is not the behaviour that is expected when using python -m gzip in a script.

The error is even codified in a test: https://github.com/python/cpython/blob/1f433406bd46fbd00b88223ad64daea6bc9eaadc/Lib/test/test_gzip.py#L776

    def test_decompress_infile_outfile_error(self):
        rc, out, err = assert_python_ok('-m', 'gzip', '-d', 'thisisatest.out')
        self.assertIn(b"filename doesn't end in .gz:", out)
        self.assertEqual(rc, 0)
        self.assertEqual(err, b'')

This should be assert_python_failure, out and err should be swapped, and exit code should be something different than 0.

>From the zen of python: Errors should never pass silently.

I am willing to fix this in a PR, but first I would like some feedback on how to solve this exactly. 

I propose raising a ValueError("can not determine output filename: 'myfile' does not end in '.gz'").

----------
components: Library (Lib)
messages: 387622
nosy: rhpvorderman
priority: normal
severity: normal
status: open
title: python -m gzip handles error incorrectly
versions: Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43316>
_______________________________________


More information about the New-bugs-announce mailing list