Quellcode durchsuchen

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 vor 12 Jahren
Ursprung
Commit
3b80e9b702
1 geänderte Dateien mit 7 neuen und 2 gelöschten Zeilen
  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):
 def hex_to_sha(hex):
     """Takes a hex sha and returns a binary sha"""
     """Takes a hex sha and returns a binary sha"""
     assert len(hex) == 40, "Incorrent length of hexsha: %s" % hex
     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):
 def hex_to_filename(path, hex):
@@ -149,7 +154,7 @@ def check_hexsha(hex, error_msg):
     """
     """
     try:
     try:
         hex_to_sha(hex)
         hex_to_sha(hex)
-    except (TypeError, AssertionError):
+    except (TypeError, AssertionError, ValueError):
         raise ObjectFormatException("%s %s" % (error_msg, hex))
         raise ObjectFormatException("%s %s" % (error_msg, hex))