This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: python -m gzip handles error incorrectly
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: methane, miss-islington, rhpvorderman
Priority: normal Keywords: patch

Created on 2021-02-24 14:34 by rhpvorderman, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 24647 merged rhpvorderman, 2021-02-25 08:48
PR 24648 merged miss-islington, 2021-02-25 11:30
PR 24649 merged miss-islington, 2021-02-25 11:31
PR 24652 merged methane, 2021-02-26 01:12
PR 24653 merged miss-islington, 2021-02-26 02:09
PR 24654 merged miss-islington, 2021-02-26 02:09
Messages (9)
msg387622 - (view) Author: Ruben Vorderman (rhpvorderman) * Date: 2021-02-24 14:34
`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'").
msg387647 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2021-02-25 08:07
I prefer `sys.exit("message")`.
msg387650 - (view) Author: Ruben Vorderman (rhpvorderman) * Date: 2021-02-25 08:34
That sounds perfect, I didn't think of that. I will make a PR.
msg387659 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2021-02-25 11:30
New changeset cc3df6368d4f3f6c9c9b716876c7e7b79c7abf3f by Ruben Vorderman in branch 'master':
bpo-43316:  gzip: CLI uses non-zero return code on error. (GH-24647)
https://github.com/python/cpython/commit/cc3df6368d4f3f6c9c9b716876c7e7b79c7abf3f
msg387660 - (view) Author: miss-islington (miss-islington) Date: 2021-02-25 11:52
New changeset 69906c505b139cbd892866dbceeb607eff53ab3b by Miss Islington (bot) in branch '3.8':
bpo-43316:  gzip: CLI uses non-zero return code on error. (GH-24647)
https://github.com/python/cpython/commit/69906c505b139cbd892866dbceeb607eff53ab3b
msg387698 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2021-02-26 01:13
New changeset adea9b86a97794ca75054603497c195d5ba39aa5 by Miss Islington (bot) in branch '3.9':
bpo-43316:  gzip: CLI uses non-zero return code on error. (GH-24647)
https://github.com/python/cpython/commit/adea9b86a97794ca75054603497c195d5ba39aa5
msg387700 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2021-02-26 02:09
New changeset 9525a18b5bb317d9fb206c992ab62aa41559b0c8 by Inada Naoki in branch 'master':
bpo-43316: gzip: Fix sys.exit() usage. (GH-24652)
https://github.com/python/cpython/commit/9525a18b5bb317d9fb206c992ab62aa41559b0c8
msg387702 - (view) Author: miss-islington (miss-islington) Date: 2021-02-26 02:36
New changeset 540749ed6d8e29a11368bc7f343baf7b7ea7e4a8 by Miss Islington (bot) in branch '3.9':
bpo-43316: gzip: Fix sys.exit() usage. (GH-24652)
https://github.com/python/cpython/commit/540749ed6d8e29a11368bc7f343baf7b7ea7e4a8
msg387703 - (view) Author: miss-islington (miss-islington) Date: 2021-02-26 02:36
New changeset 07ab490a7a966ce00a61bf56ccd0604434b143a5 by Miss Islington (bot) in branch '3.8':
bpo-43316: gzip: Fix sys.exit() usage. (GH-24652)
https://github.com/python/cpython/commit/07ab490a7a966ce00a61bf56ccd0604434b143a5
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87482
2021-02-26 02:37:07methanesetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-02-26 02:36:52miss-islingtonsetmessages: + msg387703
2021-02-26 02:36:48miss-islingtonsetmessages: + msg387702
2021-02-26 02:09:44miss-islingtonsetpull_requests: + pull_request23439
2021-02-26 02:09:31miss-islingtonsetpull_requests: + pull_request23438
2021-02-26 02:09:23methanesetmessages: + msg387700
2021-02-26 01:13:24methanesetmessages: + msg387698
2021-02-26 01:12:05methanesetpull_requests: + pull_request23437
2021-02-25 11:52:36miss-islingtonsetmessages: + msg387660
2021-02-25 11:31:05miss-islingtonsetpull_requests: + pull_request23434
2021-02-25 11:30:56miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request23433
2021-02-25 11:30:52methanesetmessages: + msg387659
2021-02-25 08:48:18rhpvordermansetkeywords: + patch
stage: patch review
pull_requests: + pull_request23432
2021-02-25 08:34:57rhpvordermansetmessages: + msg387650
2021-02-25 08:07:39methanesetnosy: + methane
messages: + msg387647
2021-02-25 07:53:27rhpvordermansettype: behavior
2021-02-24 14:34:00rhpvordermancreate