util: make hashlib import unconditional
authorGregory Szorc <gregory.szorc@gmail.com>
Sat, 12 Dec 2015 23:30:37 -0500
changeset 27357 7f5a0bd4c9aa
parent 27356 c2effd1ecebf
child 27358 ac839ee45b6a
util: make hashlib import unconditional hashlib was added in Python 2.5. As far as I can tell, SHA-512 is always available in 2.6+. So move the hashlib import to the top of the file and remove the one-off handling of SHA-512.
mercurial/util.py
--- a/mercurial/util.py	Sat Dec 12 23:26:12 2015 -0800
+++ b/mercurial/util.py	Sat Dec 12 23:30:37 2015 -0500
@@ -23,12 +23,17 @@
 import gc
 import bz2
 import zlib
+import hashlib
 
 if os.name == 'nt':
     import windows as platform
 else:
     import posix as platform
 
+md5 = hashlib.md5
+sha1 = hashlib.sha1
+sha512 = hashlib.sha512
+
 cachestat = platform.cachestat
 checkexec = platform.checkexec
 checklink = platform.checklink
@@ -95,23 +100,13 @@
 def safehasattr(thing, attr):
     return getattr(thing, attr, _notset) is not _notset
 
-from hashlib import md5, sha1
-
 DIGESTS = {
     'md5': md5,
     'sha1': sha1,
+    'sha512': sha512,
 }
 # List of digest types from strongest to weakest
-DIGESTS_BY_STRENGTH = ['sha1', 'md5']
-
-try:
-    import hashlib
-    DIGESTS.update({
-        'sha512': hashlib.sha512,
-    })
-    DIGESTS_BY_STRENGTH.insert(0, 'sha512')
-except ImportError:
-    pass
+DIGESTS_BY_STRENGTH = ['sha512', 'sha1', 'md5']
 
 for k in DIGESTS_BY_STRENGTH:
     assert k in DIGESTS