Ver Fonte

Raise a ValueError for invalid shas

When hex_to_sha is passed an invalid sha, raise a ValueError instead of a
TypeError. This exception is more appropriate since the sha is still a string.
Chow Loong Jin há 12 anos atrás
pai
commit
ba0b66535b
1 ficheiros alterados com 7 adições e 2 exclusões
  1. 7 2
      dulwich/objects.py

+ 7 - 2
dulwich/objects.py

@@ -88,7 +88,12 @@ def sha_to_hex(sha):
 def hex_to_sha(hex):
     """Takes a hex sha and returns a binary sha"""
     assert len(hex) == 40, "Incorrent length of hexsha: %s" % hex
-    return binascii.unhexlify(hex)
+    try:
+        return binascii.unhexlify(hex)
+    except TypeError, exc:
+        if not isinstance(hex, str):
+            raise
+        raise ValueError(exc.message)
 
 
 def hex_to_filename(path, hex):
@@ -149,7 +154,7 @@ def check_hexsha(hex, error_msg):
     """
     try:
         hex_to_sha(hex)
-    except (TypeError, AssertionError):
+    except (TypeError, AssertionError, ValueError):
         raise ObjectFormatException("%s %s" % (error_msg, hex))