mercurial/util.py
changeset 26847 9f16787cbefd
parent 26665 6331a0c310db
child 27015 341cb90ffd18
equal deleted inserted replaced
26846:7c1b4840c2cd 26847:9f16787cbefd
    89 _notset = object()
    89 _notset = object()
    90 
    90 
    91 def safehasattr(thing, attr):
    91 def safehasattr(thing, attr):
    92     return getattr(thing, attr, _notset) is not _notset
    92     return getattr(thing, attr, _notset) is not _notset
    93 
    93 
    94 def sha1(s=''):
    94 from hashlib import md5, sha1
    95     '''
       
    96     Low-overhead wrapper around Python's SHA support
       
    97 
       
    98     >>> f = _fastsha1
       
    99     >>> a = sha1()
       
   100     >>> a = f()
       
   101     >>> a.hexdigest()
       
   102     'da39a3ee5e6b4b0d3255bfef95601890afd80709'
       
   103     '''
       
   104 
       
   105     return _fastsha1(s)
       
   106 
       
   107 def _fastsha1(s=''):
       
   108     # This function will import sha1 from hashlib or sha (whichever is
       
   109     # available) and overwrite itself with it on the first call.
       
   110     # Subsequent calls will go directly to the imported function.
       
   111     if sys.version_info >= (2, 5):
       
   112         from hashlib import sha1 as _sha1
       
   113     else:
       
   114         from sha import sha as _sha1
       
   115     global _fastsha1, sha1
       
   116     _fastsha1 = sha1 = _sha1
       
   117     return _sha1(s)
       
   118 
       
   119 def md5(s=''):
       
   120     try:
       
   121         from hashlib import md5 as _md5
       
   122     except ImportError:
       
   123         from md5 import md5 as _md5
       
   124     global md5
       
   125     md5 = _md5
       
   126     return _md5(s)
       
   127 
    95 
   128 DIGESTS = {
    96 DIGESTS = {
   129     'md5': md5,
    97     'md5': md5,
   130     'sha1': sha1,
    98     'sha1': sha1,
   131 }
    99 }