From e50fa574e5f76b8ec048568426852cdd0f3e19c3 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Fri, 28 Apr 2017 10:41:27 +0200 Subject: [PATCH] clarify exception-driven control flow MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Distinguish the actual EOF when hit at the beginning from other IO errors in _init_read_gz() and only catch this one where it’s expected. Well formed archives do not end inside a header. --- deltatar/tarfile.py | 9 +++++++-- 1 files changed, 7 insertions(+), 2 deletions(-) diff --git a/deltatar/tarfile.py b/deltatar/tarfile.py index 5768e18..6bd3a42 100644 --- a/deltatar/tarfile.py +++ b/deltatar/tarfile.py @@ -329,6 +329,8 @@ class DecryptionError(TarError): class EncryptionError(TarError): """Exception for error during decryption.""" pass +class EndOfFile(Exception): + """Signal end of file condition when they’re not an error.""" #--------------------------- # internal stream interface @@ -704,8 +706,11 @@ class _Stream: """ self.cmp = self.zlib.decompressobj(-self.zlib.MAX_WBITS) - # taken from gzip.GzipFile with some alterations read2 = self.__read(2) + if read2 == b"": + raise EndOfFile ("_init_read_gz(): read returned zero bytes at pos " + "%d" % self.fileobj.tell()) + # taken from gzip.GzipFile with some alterations if read2 != GZ_MAGIC_BYTES: raise ReadError("not a gzip file") @@ -863,7 +868,7 @@ class _Stream: self.close(close_fileobj=False) try: self._init_read_gz() - except: + except EndOfFile: # happens at the end of the file pass self.crc = self.zlib.crc32(b"") & 0xFFFFffff -- 1.7.1