clarify exception-driven control flow
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Fri, 28 Apr 2017 08:41:27 +0000 (10:41 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 2 Apr 2018 11:34:08 +0000 (13:34 +0200)
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

index 5768e18..6bd3a42 100644 (file)
@@ -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