python / cpython

The Python programming language
https://www.python.org
Other
62.75k stars 30.07k forks source link

gzip._GzipReader should only throw BadGzipFile errors #87787

Open c0d574d1-8b74-45e1-a4a4-bfafd7c52638 opened 3 years ago

c0d574d1-8b74-45e1-a4a4-bfafd7c52638 commented 3 years ago
BPO 43621
Nosy @rhpvorderman

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['type-bug', '3.8', '3.9', '3.10', '3.7', 'library'] title = 'gzip._GzipReader should only throw BadGzipFile errors' updated_at = user = 'https://github.com/rhpvorderman' ``` bugs.python.org fields: ```python activity = actor = 'rhpvorderman' assignee = 'none' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = creator = 'rhpvorderman' dependencies = [] files = [] hgrepos = [] issue_num = 43621 keywords = [] message_count = 1.0 messages = ['389494'] nosy_count = 1.0 nosy_names = ['rhpvorderman'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue43621' versions = ['Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10'] ```

c0d574d1-8b74-45e1-a4a4-bfafd7c52638 commented 3 years ago

This is properly documented: https://docs.python.org/3/library/gzip.html#gzip.BadGzipFile .

It now hrows EOFErrors when a stream is truncated. But this means that upstream both BadGzipFile and EOFError need to be catched in the exception handling when opening a gzip file for reading. When a gzip file is truncated it is also a "bad gzip file" in my opinion, so there is no reason to have an extra class of errors. Also it throws zlib.error's when zlib craches for some reason. This means there is some corruption in the raw deflate block. Well that means it is a "bad gzip file" as well and the error message should reflect that.

This won't break people's code. If they are already catching EOFError zlib.error and BadGzipFile it changes nothing. If they only catch BadGzipFile, they will have less annoying errors that pop through.

I can make the PR, but of course not without any feedback. I am curious what other people think.