changeset 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 9542f4c3fa1b
files mercurial/util.py
diffstat 1 files changed, 9 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/util.py	Mon May 04 21:30:39 2009 +0200
+++ b/mercurial/util.py	Mon May 04 22:14:52 2009 +0200
@@ -21,13 +21,18 @@
 # Python compatibility
 
 def sha1(s):
+    return _fastsha1(s)
+
+def _fastsha1(s):
+    # This function will import sha1 from hashlib or sha (whichever is
+    # available) and overwrite itself with it on the first call.
+    # Subsequent calls will go directly to the imported function.
     try:
-        import hashlib
-        _sha1 = hashlib.sha1
+        from hashlib import sha1 as _sha1
     except ImportError:
         from sha import sha as _sha1
-    global sha1
-    sha1 = _sha1
+    global _fastsha1
+    _fastsha1 = _sha1
     return _sha1(s)
 
 import subprocess