From aef8e1341b0ab06e875f8b81cfb54d81a724da28 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Tue, 30 Apr 2019 04:35:46 -0600 Subject: [PATCH 1/2] bpo-6584: Add a BadGzipFile exception to the gzip module 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ù --- Doc/library/gzip.rst | 6 ++++++ Doc/whatsnew/3.8.rst | 5 +++++ Lib/gzip.py | 17 +++++++++++------ Lib/test/test_gzip.py | 9 +++++++++ .../2019-04-30-04-34-53.bpo-6584.Hzp9-P.rst | 1 + 5 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2019-04-30-04-34-53.bpo-6584.Hzp9-P.rst diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index 8850a33f4abb90..2ab553771ae6c0 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -59,6 +59,12 @@ The module defines the following items: .. versionchanged:: 3.6 Accepts a :term:`path-like object`. +.. exception:: BadGzipFile + + The exception raised for invalid gzip files. It inherits :class:`OSError`. + + .. versionadded:: 3.8 + .. class:: GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None) Constructor for the :class:`GzipFile` class, which simulates most of the diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index bbc55ddd63418d..dbd2b25879df53 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -289,6 +289,11 @@ gzip Added the *mtime* parameter to :func:`gzip.compress` for reproducible output. (Contributed by Guo Ci Teo in :issue:`34898`.) +A :exc:`~gzip.BadGzipFile` exception is now raised in case of invalid or +corrupt gzip files instead of :exc:`OSError`. +(Contributed by Filip Gruszczyński, Michele Orrù, and Zackery Spytz in +:issue:`6584`.) + idlelib and IDLE ---------------- diff --git a/Lib/gzip.py b/Lib/gzip.py index 7c861874198842..86fbe4a82422b4 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -11,7 +11,7 @@ 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 @@ def seek(self, off): def seekable(self): return True # Allows fast-forwarding even in unseekable streams + +class BadGzipFile(OSError): + """Exception raised if the processed gzip file is not valid.""" + + 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 @@ def _read_gzip_header(self): 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(" Date: Thu, 2 May 2019 08:05:47 -0600 Subject: [PATCH 2/2] Mention EOFError and zlib.error in the gzip docs. --- Doc/library/gzip.rst | 4 +++- Doc/whatsnew/3.8.rst | 4 ++-- Lib/gzip.py | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index 2ab553771ae6c0..3349a94446d0ce 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -61,7 +61,9 @@ The module defines the following items: .. exception:: BadGzipFile - The exception raised for invalid gzip files. It inherits :class:`OSError`. + An exception raised for invalid gzip files. It inherits :exc:`OSError`. + :exc:`EOFError` and :exc:`zlib.error` can also be raised for invalid gzip + files. .. versionadded:: 3.8 diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index dbd2b25879df53..ebe5551916501d 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -289,8 +289,8 @@ gzip Added the *mtime* parameter to :func:`gzip.compress` for reproducible output. (Contributed by Guo Ci Teo in :issue:`34898`.) -A :exc:`~gzip.BadGzipFile` exception is now raised in case of invalid or -corrupt gzip files instead of :exc:`OSError`. +A :exc:`~gzip.BadGzipFile` exception is now raised instead of :exc:`OSError` +for certain types of invalid or corrupt gzip files. (Contributed by Filip Gruszczyński, Michele Orrù, and Zackery Spytz in :issue:`6584`.) diff --git a/Lib/gzip.py b/Lib/gzip.py index 86fbe4a82422b4..2968f475efad31 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -114,7 +114,7 @@ def seekable(self): class BadGzipFile(OSError): - """Exception raised if the processed gzip file is not valid.""" + """Exception raised in some cases for invalid gzip files.""" class GzipFile(_compression.BaseStream):