From: Philipp Gesang Date: Fri, 28 Apr 2017 08:41:27 +0000 (+0200) Subject: clarify exception-driven control flow X-Git-Tag: v2.2~7^2~140 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=e50fa574e5f76b8ec048568426852cdd0f3e19c3;p=python-delta-tar clarify exception-driven control flow 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. --- 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