changeset 6470:ac0bcd951c2c

python 2.6 compatibility: compatibility wrappers for hash functions
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Fri, 04 Apr 2008 22:36:40 +0200
parents fb502719c75c
children 0c1ec0cd7c6a
files mercurial/keepalive.py mercurial/mdiff.py mercurial/patch.py mercurial/revlog.py mercurial/util.py tests/md5sum.py
diffstat 6 files changed, 41 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/keepalive.py	Fri Apr 04 22:41:17 2008 +0200
+++ b/mercurial/keepalive.py	Fri Apr 04 22:36:40 2008 +0200
@@ -19,6 +19,8 @@
 
 # Modified by Benoit Boissinot:
 #  - fix for digest auth (inspired from urllib2.py @ Python v2.4)
+# Modified by Dirkjan Ochtman:
+#  - import md5 function from a local util module
 
 """An HTTP handler for urllib2 that supports HTTP 1.1 and keepalive.
 
@@ -450,7 +452,7 @@
     keepalive_handler.close_all()
 
 def continuity(url):
-    import md5
+    from util import md5
     format = '%25s: %s'
 
     # first fetch the file with the normal http handler
--- a/mercurial/mdiff.py	Fri Apr 04 22:41:17 2008 +0200
+++ b/mercurial/mdiff.py	Fri Apr 04 22:36:40 2008 +0200
@@ -6,7 +6,7 @@
 # of the GNU General Public License, incorporated herein by reference.
 
 from i18n import _
-import bdiff, mpatch, re, struct, util, md5
+import bdiff, mpatch, re, struct, util
 
 def splitnewlines(text):
     '''like str.splitlines, but only split on newlines.'''
@@ -80,7 +80,7 @@
     if not opts.text and (util.binary(a) or util.binary(b)):
         def h(v):
             # md5 is used instead of sha1 because md5 is supposedly faster
-            return md5.new(v).digest()
+            return util.md5(v).digest()
         if a and b and len(a) == len(b) and h(a) == h(b):
             return ""
         l = ['Binary file %s has changed\n' % fn1]
--- a/mercurial/patch.py	Fri Apr 04 22:41:17 2008 +0200
+++ b/mercurial/patch.py	Fri Apr 04 22:36:40 2008 +0200
@@ -9,7 +9,7 @@
 from i18n import _
 from node import hex, nullid, short
 import base85, cmdutil, mdiff, util, context, revlog, diffhelpers, copies
-import cStringIO, email.Parser, os, popen2, re, sha, errno
+import cStringIO, email.Parser, os, popen2, re, errno
 import sys, tempfile, zlib
 
 class PatchError(Exception):
@@ -1120,7 +1120,7 @@
         if not text:
             return '0' * 40
         l = len(text)
-        s = sha.new('blob %d\0' % l)
+        s = util.sha1('blob %d\0' % l)
         s.update(text)
         return s.hexdigest()
 
--- a/mercurial/revlog.py	Fri Apr 04 22:41:17 2008 +0200
+++ b/mercurial/revlog.py	Fri Apr 04 22:36:40 2008 +0200
@@ -13,13 +13,13 @@
 from node import bin, hex, nullid, nullrev, short
 from i18n import _
 import changegroup, errno, ancestor, mdiff
-import sha, struct, util, zlib
+import struct, util, zlib
 
 _pack = struct.pack
 _unpack = struct.unpack
 _compress = zlib.compress
 _decompress = zlib.decompress
-_sha = sha.new
+_sha = util.sha1
 
 # revlog flags
 REVLOGV0 = 0
--- a/mercurial/util.py	Fri Apr 04 22:41:17 2008 +0200
+++ b/mercurial/util.py	Fri Apr 04 22:36:40 2008 +0200
@@ -17,12 +17,38 @@
 import os, stat, threading, time, calendar, ConfigParser, locale, glob, osutil
 import urlparse
 
+# Python compatibility
+
 try:
     set = set
     frozenset = frozenset
 except NameError:
     from sets import Set as set, ImmutableSet as frozenset
 
+_md5 = None
+def md5(s):
+    global _md5
+    if _md5 is None:
+        try:
+            import hashlib
+            _md5 = hashlib.md5
+        except ImportError:
+            import md5
+            _md5 = md5.md5
+    return _md5(s)
+
+_sha1 = None
+def sha1(s):
+    global _sha1
+    if _sha1 is None:
+        try:
+            import hashlib
+            _sha1 = hashlib.sha1
+        except ImportError:
+            import sha
+            _sha1 = sha.sha
+    return _sha1(s)
+
 try:
     _encoding = os.environ.get("HGENCODING")
     if sys.platform == 'darwin' and not _encoding:
--- a/tests/md5sum.py	Fri Apr 04 22:41:17 2008 +0200
+++ b/tests/md5sum.py	Fri Apr 04 22:36:40 2008 +0200
@@ -7,7 +7,11 @@
 # GPL-compatible.
 
 import sys
-import md5
+
+try:
+    from hashlib import md5
+except ImportError:
+    from md5 import md5
 
 for filename in sys.argv[1:]:
     try:
@@ -16,7 +20,7 @@
         sys.stderr.write('%s: Can\'t open: %s\n' % (filename, msg))
         sys.exit(1)
 
-    m = md5.new()
+    m = md5()
     try:
         while 1:
             data = fp.read(8192)