2
0
Эх сурвалжийг харах

Drop compatibility functions for `hashlib.sha1`.

Jelmer Vernooij 11 жил өмнө
parent
commit
3c23765bd3

+ 0 - 13
dulwich/_compat.py

@@ -21,10 +21,6 @@
 These utilities can all be deleted when dulwich decides it wants to stop
 support for python <2.6.
 """
-try:
-    import hashlib
-except ImportError:
-    import sha
 
 try:
     from urlparse import parse_qs
@@ -38,15 +34,6 @@ except ImportError:
     SEEK_END = 2
 
 
-def make_sha(source=''):
-    """A python2.4 workaround for the sha/hashlib module fiasco."""
-    try:
-        return hashlib.sha1(source)
-    except NameError:
-        sha1 = sha.sha(source)
-        return sha1
-
-
 # Backport of OrderedDict() class that runs on Python 2.4, 2.5, 2.6, 2.7 and pypy.
 # Passes Python2.7's test suite and incorporates all the latest updates.
 # Copyright (C) Raymond Hettinger, MIT license

+ 3 - 5
dulwich/objects.py

@@ -39,9 +39,7 @@ from dulwich.errors import (
     ObjectFormatException,
     )
 from dulwich.file import GitFile
-from dulwich._compat import (
-    make_sha,
-    )
+from hashlib import sha1
 
 ZERO_SHA = "0" * 40
 
@@ -479,7 +477,7 @@ class ShaFile(object):
         return ret
 
     def _make_sha(self):
-        ret = make_sha()
+        ret = sha1()
         ret.update(self._header())
         for chunk in self.as_raw_chunks():
             ret.update(chunk)
@@ -489,7 +487,7 @@ class ShaFile(object):
         """The SHA1 object that is the name of this object."""
         if self._sha is None or self._needs_serialization:
             # this is a local because as_raw_chunks() overwrites self._sha
-            new_sha = make_sha()
+            new_sha = sha1()
             new_sha.update(self._header())
             for chunk in self.as_raw_chunks():
                 new_sha.update(chunk)

+ 10 - 10
dulwich/pack.py

@@ -51,6 +51,7 @@ except ImportError:
     has_mmap = False
 else:
     has_mmap = True
+from hashlib import sha1
 import os
 import struct
 from struct import unpack_from
@@ -67,7 +68,6 @@ from dulwich.lru_cache import (
     LRUSizeCache,
     )
 from dulwich._compat import (
-    make_sha,
     SEEK_CUR,
     SEEK_END,
     )
@@ -251,10 +251,10 @@ def iter_sha1(iter):
     :param iter: Iterator over string objects
     :return: 40-byte hex sha1 digest
     """
-    sha1 = make_sha()
+    sha = sha1()
     for name in iter:
-        sha1.update(name)
-    return sha1.hexdigest()
+        sha.update(name)
+    return sha.hexdigest()
 
 
 def load_pack_index(path):
@@ -534,7 +534,7 @@ class FilePackIndex(PackIndex):
 
         :return: This is a 20-byte binary digest
         """
-        return make_sha(self._contents[:-20]).digest()
+        return sha1(self._contents[:-20]).digest()
 
     def get_pack_checksum(self):
         """Return the SHA1 checksum stored for the corresponding packfile.
@@ -739,7 +739,7 @@ class PackStreamReader(object):
             self.read_some = read_all
         else:
             self.read_some = read_some
-        self.sha = make_sha()
+        self.sha = sha1()
         self._offset = 0
         self._rbuf = StringIO()
         # trailer is a deque to avoid memory allocation on small reads
@@ -904,7 +904,7 @@ class PackStreamCopier(PackStreamReader):
 
 def obj_sha(type, chunks):
     """Compute the SHA for a numeric type and object chunks."""
-    sha = make_sha()
+    sha = sha1()
     sha.update(object_header(type, chunks_length(chunks)))
     for chunk in chunks:
         sha.update(chunk)
@@ -921,7 +921,7 @@ def compute_file_sha(f, start_ofs=0, end_ofs=0, buffer_size=1<<16):
     :param buffer_size: A buffer size for reading.
     :return: A new SHA object updated with data read from the file.
     """
-    sha = make_sha()
+    sha = sha1()
     f.seek(0, SEEK_END)
     todo = f.tell() + end_ofs - start_ofs
     f.seek(start_ofs)
@@ -1335,7 +1335,7 @@ class SHA1Reader(object):
 
     def __init__(self, f):
         self.f = f
-        self.sha1 = make_sha('')
+        self.sha1 = sha1('')
 
     def read(self, num=None):
         data = self.f.read(num)
@@ -1360,7 +1360,7 @@ class SHA1Writer(object):
     def __init__(self, f):
         self.f = f
         self.length = 0
-        self.sha1 = make_sha('')
+        self.sha1 = sha1('')
 
     def write(self, data):
         self.sha1.update(data)

+ 7 - 9
dulwich/tests/test_pack.py

@@ -21,14 +21,12 @@
 
 
 from cStringIO import StringIO
+from hashlib import sha1
 import os
 import shutil
 import tempfile
 import zlib
 
-from dulwich._compat import (
-    make_sha,
-    )
 from dulwich.errors import (
     ChecksumMismatch,
     )
@@ -250,16 +248,16 @@ class TestPackData(PackTests):
 
     def test_compute_file_sha(self):
         f = StringIO('abcd1234wxyz')
-        self.assertEqual(make_sha('abcd1234wxyz').hexdigest(),
+        self.assertEqual(sha1('abcd1234wxyz').hexdigest(),
                          compute_file_sha(f).hexdigest())
-        self.assertEqual(make_sha('abcd1234wxyz').hexdigest(),
+        self.assertEqual(sha1('abcd1234wxyz').hexdigest(),
                          compute_file_sha(f, buffer_size=5).hexdigest())
-        self.assertEqual(make_sha('abcd1234').hexdigest(),
+        self.assertEqual(sha1('abcd1234').hexdigest(),
                          compute_file_sha(f, end_ofs=-4).hexdigest())
-        self.assertEqual(make_sha('1234wxyz').hexdigest(),
+        self.assertEqual(sha1('1234wxyz').hexdigest(),
                          compute_file_sha(f, start_ofs=4).hexdigest())
         self.assertEqual(
-          make_sha('1234').hexdigest(),
+          sha1('1234').hexdigest(),
           compute_file_sha(f, start_ofs=4, end_ofs=-4).hexdigest())
 
 
@@ -504,7 +502,7 @@ class WritePackTests(TestCase):
         f = StringIO()
         f.write('header')
         offset = f.tell()
-        sha_a = make_sha('foo')
+        sha_a = sha1('foo')
         sha_b = sha_a.copy()
         write_pack_object(f, Blob.type_num, 'blob', sha=sha_a)
         self.assertNotEqual(sha_a.digest(), sha_b.digest())