From cf599f6f6f1c392d8f12936982a370d533782195 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Mon, 13 May 2019 01:50:52 -0600 Subject: bpo-6584: Add a BadGzipFile exception to the gzip module. (GH-13022) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Filip Gruszczyński Co-Authored-By: Michele Orrù --- Lib/gzip.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'Lib/gzip.py') diff --git a/Lib/gzip.py b/Lib/gzip.py index 7c86187419..2968f475ef 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -11,7 +11,7 @@ import builtins import io import _compression -__all__ = ["GzipFile", "open", "compress", "decompress"] +__all__ = ["BadGzipFile", "GzipFile", "open", "compress", "decompress"] FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16 @@ -112,6 +112,11 @@ class _PaddedFile: def seekable(self): return True # Allows fast-forwarding even in unseekable streams + +class BadGzipFile(OSError): + """Exception raised in some cases for invalid gzip files.""" + + class GzipFile(_compression.BaseStream): """The GzipFile class simulates most of the methods of a file object with the exception of the truncate() method. @@ -413,12 +418,12 @@ class _GzipReader(_compression.DecompressReader): return False if magic != b'\037\213': - raise OSError('Not a gzipped file (%r)' % magic) + raise BadGzipFile('Not a gzipped file (%r)' % magic) (method, flag, self._last_mtime) = struct.unpack("