comparison mercurial/util.py @ 8281:3e1e499db9d7

util: initialize md5 and sha1 without using extra global variables This lets the functions skip the "if _sha1 is None" test on each call.
author Martin Geisler <mg@lazybytes.net>
date Sun, 03 May 2009 00:03:35 +0200
parents 0b02d98d44d0
children 1ea7e7d90007
comparison
equal deleted inserted replaced
8280:0b02d98d44d0 8281:3e1e499db9d7
18 import os, stat, threading, time, calendar, glob, osutil, random 18 import os, stat, threading, time, calendar, glob, osutil, random
19 import imp 19 import imp
20 20
21 # Python compatibility 21 # Python compatibility
22 22
23 _md5 = None
24 def md5(s): 23 def md5(s):
25 global _md5 24 try:
26 if _md5 is None: 25 import hashlib
27 try: 26 _md5 = hashlib.md5
28 import hashlib 27 except ImportError:
29 _md5 = hashlib.md5 28 import md5
30 except ImportError: 29 _md5 = md5.md5
31 import md5 30 global md5
32 _md5 = md5.md5 31 md5 = _md5
33 return _md5(s) 32 return _md5(s)
34 33
35 _sha1 = None
36 def sha1(s): 34 def sha1(s):
37 global _sha1 35 try:
38 if _sha1 is None: 36 import hashlib
39 try: 37 _sha1 = hashlib.sha1
40 import hashlib 38 except ImportError:
41 _sha1 = hashlib.sha1 39 import sha
42 except ImportError: 40 _sha1 = sha.sha
43 import sha 41 global sha1
44 _sha1 = sha.sha 42 sha1 = _sha1
45 return _sha1(s) 43 return _sha1(s)
46 44
47 import subprocess 45 import subprocess
48 closefds = os.name == 'posix' 46 closefds = os.name == 'posix'
49 def popen2(cmd, mode='t', bufsize=-1): 47 def popen2(cmd, mode='t', bufsize=-1):