comparison mercurial/util.py @ 27357:7f5a0bd4c9aa

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.
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 12 Dec 2015 23:30:37 -0500
parents 39c14e89b881
children ac839ee45b6a
comparison
equal deleted inserted replaced
27356:c2effd1ecebf 27357:7f5a0bd4c9aa
21 import os, time, datetime, calendar, textwrap, signal, collections 21 import os, time, datetime, calendar, textwrap, signal, collections
22 import imp, socket, urllib 22 import imp, socket, urllib
23 import gc 23 import gc
24 import bz2 24 import bz2
25 import zlib 25 import zlib
26 import hashlib
26 27
27 if os.name == 'nt': 28 if os.name == 'nt':
28 import windows as platform 29 import windows as platform
29 else: 30 else:
30 import posix as platform 31 import posix as platform
32
33 md5 = hashlib.md5
34 sha1 = hashlib.sha1
35 sha512 = hashlib.sha512
31 36
32 cachestat = platform.cachestat 37 cachestat = platform.cachestat
33 checkexec = platform.checkexec 38 checkexec = platform.checkexec
34 checklink = platform.checklink 39 checklink = platform.checklink
35 copymode = platform.copymode 40 copymode = platform.copymode
93 os.stat_float_times(False) 98 os.stat_float_times(False)
94 99
95 def safehasattr(thing, attr): 100 def safehasattr(thing, attr):
96 return getattr(thing, attr, _notset) is not _notset 101 return getattr(thing, attr, _notset) is not _notset
97 102
98 from hashlib import md5, sha1
99
100 DIGESTS = { 103 DIGESTS = {
101 'md5': md5, 104 'md5': md5,
102 'sha1': sha1, 105 'sha1': sha1,
106 'sha512': sha512,
103 } 107 }
104 # List of digest types from strongest to weakest 108 # List of digest types from strongest to weakest
105 DIGESTS_BY_STRENGTH = ['sha1', 'md5'] 109 DIGESTS_BY_STRENGTH = ['sha512', 'sha1', 'md5']
106
107 try:
108 import hashlib
109 DIGESTS.update({
110 'sha512': hashlib.sha512,
111 })
112 DIGESTS_BY_STRENGTH.insert(0, 'sha512')
113 except ImportError:
114 pass
115 110
116 for k in DIGESTS_BY_STRENGTH: 111 for k in DIGESTS_BY_STRENGTH:
117 assert k in DIGESTS 112 assert k in DIGESTS
118 113
119 class digester(object): 114 class digester(object):