diff -r b09d07d1f696 Lib/gzip.py --- a/Lib/gzip.py Sat Aug 20 03:19:34 2011 +0200 +++ b/Lib/gzip.py Sat Aug 20 10:09:25 2011 +0200 @@ -89,6 +89,12 @@ return getattr(self.file, name) +class BadGzipFile(IOError): + """ + Exception raised if the processed gzip file is not valid. + """ + + class GzipFile(io.BufferedIOBase): """The GzipFile class simulates most of the methods of a file object with the exception of the readinto() and truncate() methods. @@ -247,10 +253,10 @@ raise EOFError("Reached EOF") if magic != b'\037\213': - raise IOError('Not a gzipped file') + raise BadGzipFile('Not a gzipped file') method = ord( self.fileobj.read(1) ) if method != 8: - raise IOError('Unknown compression method') + raise BadGzipFile('Unknown compression method') flag = ord( self.fileobj.read(1) ) self.mtime = read32(self.fileobj) # extraflag = self.fileobj.read(1) @@ -442,10 +448,10 @@ crc32 = read32(self.fileobj) isize = read32(self.fileobj) # may exceed 2GB if crc32 != self.crc: - raise IOError("CRC check failed %s != %s" % (hex(crc32), - hex(self.crc))) + raise BadGzipFile("CRC check failed %s != %s" % + (hex(crc32), hex(self.crc))) elif isize != (self.size & 0xffffffff): - raise IOError("Incorrect length of data produced") + raise BadGzipFile("Incorrect length of data produced") # Gzip files can be padded with zeroes and still have archives. # Consume all zero bytes and set the file position to the first diff -r b09d07d1f696 Lib/test/test_gzip.py --- a/Lib/test/test_gzip.py Sat Aug 20 03:19:34 2011 +0200 +++ b/Lib/test/test_gzip.py Sat Aug 20 10:09:25 2011 +0200 @@ -306,6 +306,17 @@ d = f.read() self.assertEqual(d, data1 * 50, "Incorrect data in file") + def test_gzip_exception(self): + self.assert_(issubclass(gzip.BadGzipFile, IOError)) + + def test_bad_gzip_file(self): + with open(self.filename, 'wb') as file: + file.write(data1 * 50) + + with gzip.GzipFile(self.filename, 'r') as file: + self.assertRaises(IOError, file.readlines) + self.assertRaises(gzip.BadGzipFile, file.readlines) + def test_non_seekable_file(self): uncompressed = data1 * 50 buf = UnseekableIO()