Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/util.py @ 8297:7f27e69dd27f
util: stop overwriting sha1, overwrite _fastsha1 instead
Some modules (like revlog) would import util.sha1 as _sha1. This
defeats the purpose of having util.sha1 overwrite itself with a faster
version -- revlog would end up always calling the slow version. By
always delegating to util._fastsha1 we avoid this at the cost of an
extra (but unconditional) indirection.
author | Martin Geisler <mg@lazybytes.net> |
---|---|
date | Mon, 04 May 2009 22:14:52 +0200 |
parents | 908c5906091b |
children | edd676eae7d7 |
comparison
equal
deleted
inserted
replaced
8296:908c5906091b | 8297:7f27e69dd27f |
---|---|
19 import imp | 19 import imp |
20 | 20 |
21 # Python compatibility | 21 # Python compatibility |
22 | 22 |
23 def sha1(s): | 23 def sha1(s): |
24 try: | 24 return _fastsha1(s) |
25 import hashlib | 25 |
26 _sha1 = hashlib.sha1 | 26 def _fastsha1(s): |
27 # This function will import sha1 from hashlib or sha (whichever is | |
28 # available) and overwrite itself with it on the first call. | |
29 # Subsequent calls will go directly to the imported function. | |
30 try: | |
31 from hashlib import sha1 as _sha1 | |
27 except ImportError: | 32 except ImportError: |
28 from sha import sha as _sha1 | 33 from sha import sha as _sha1 |
29 global sha1 | 34 global _fastsha1 |
30 sha1 = _sha1 | 35 _fastsha1 = _sha1 |
31 return _sha1(s) | 36 return _sha1(s) |
32 | 37 |
33 import subprocess | 38 import subprocess |
34 closefds = os.name == 'posix' | 39 closefds = os.name == 'posix' |
35 def popen2(cmd, mode='t', bufsize=-1): | 40 def popen2(cmd, mode='t', bufsize=-1): |