Explorar o código

Add separate exception for checksum mismatches.

Jelmer Vernooij %!s(int64=16) %!d(string=hai) anos
pai
achega
cc4186b191
Modificáronse 2 ficheiros con 22 adicións e 2 borrados
  1. 16 0
      dulwich/errors.py
  2. 6 2
      dulwich/pack.py

+ 16 - 0
dulwich/errors.py

@@ -18,6 +18,22 @@
 
 """Dulwich-related exception classes and utility functions."""
 
+class ChecksumMismatch(Exception):
+    """A checksum didn't match the expected contents."""
+
+    def __init__(self, expected, got, extra=None):
+        self.expected = expected
+        self.got = got
+        self.extra = extra
+        if self.extra is not None:
+            Exception.__init__(self, 
+                "Checksum mismatch: Expected %s, got %s" % (expected, got))
+        else:
+            Exception.__init__(self,
+                "Checksum mismatch: Expected %s, got %s; %s" % 
+                (expected, got, extra))
+
+
 class WrongObjectException(Exception):
     """Baseclass for all the _ is not a _ exceptions on objects.
   

+ 6 - 2
dulwich/pack.py

@@ -47,12 +47,15 @@ import sys
 import zlib
 import difflib
 
+from dulwich.errors import (
+    ApplyDeltaError,
+    ChecksumMismatch,
+    )
 from dulwich.objects import (
     ShaFile,
     hex_to_sha,
     sha_to_hex,
     )
-from dulwich.errors import ApplyDeltaError
 from dulwich.misc import make_sha
 
 supports_mmap_offset = (sys.version_info[0] >= 3 or
@@ -822,7 +825,8 @@ class Pack(object):
             idx_stored_checksum = self.idx.get_stored_checksums()[0]
             data_stored_checksum = self._data.get_stored_checksum()
             if idx_stored_checksum != data_stored_checksum:
-                raise AssertionError("Checksum for index does not match checksum for pack: %s != %s" % (sha_to_hex(idx_stored_checksum), sha_to_hex(data_stored_checksum)))
+                raise ChecksumMismatch(sha_to_hex(idx_stored_checksum), 
+                                       sha_to_hex(data_stored_checksum))
         return self._data
 
     @property