# HG changeset patch # User Dirkjan Ochtman # Date 2008-04-04 20:36:40 # Node ID ac0bcd951c2c95b32a1b6cc09f0de66b56b6ed1b # Parent fb502719c75c048af6e81289dc8a70edb8fc43aa python 2.6 compatibility: compatibility wrappers for hash functions diff --git a/mercurial/keepalive.py b/mercurial/keepalive.py --- a/mercurial/keepalive.py +++ b/mercurial/keepalive.py @@ -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 @@ def error_handler(url): 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 diff --git a/mercurial/mdiff.py b/mercurial/mdiff.py --- a/mercurial/mdiff.py +++ b/mercurial/mdiff.py @@ -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 @@ def unidiff(a, ad, b, bd, fn1, fn2, r=No 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] diff --git a/mercurial/patch.py b/mercurial/patch.py --- a/mercurial/patch.py +++ b/mercurial/patch.py @@ -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 @@ def b85diff(to, tn): 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() diff --git a/mercurial/revlog.py b/mercurial/revlog.py --- a/mercurial/revlog.py +++ b/mercurial/revlog.py @@ -13,13 +13,13 @@ of the GNU General Public License, incor 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 diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -17,12 +17,38 @@ import cStringIO, errno, getpass, re, sh 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: diff --git a/tests/md5sum.py b/tests/md5sum.py --- a/tests/md5sum.py +++ b/tests/md5sum.py @@ -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 @@ for filename in sys.argv[1:]: 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)