# HG changeset patch # User Martin Geisler # Date 1234739387 -3600 # Node ID 5280c39778b65641e8613b8f9f8dd2a7bfbf9240 # Parent 3b8f2750efcfa548849f7ac6001e9724d2c65f57 pure/mpatch: use StringIO instead of mmap (issue1493) This is more portable than memory mapping /dev/zero: Windows obviously does not have /dev/zero and mapping /dev/zero failed on Mac OS X. diff -r 3b8f2750efcf -r 5280c39778b6 mercurial/pure/mpatch.py --- a/mercurial/pure/mpatch.py Wed Feb 11 21:47:57 2009 +0100 +++ b/mercurial/pure/mpatch.py Mon Feb 16 00:09:47 2009 +0100 @@ -5,9 +5,11 @@ # This software may be used and distributed according to the terms # of the GNU General Public License, incorporated herein by reference. -import struct, mmap - -devzero = file("/dev/zero") +import struct +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO # This attempts to apply a series of patches in time proportional to # the total size of the patches, rather than patches * len(text). This @@ -30,7 +32,16 @@ if not tl: return a - m = mmap.mmap(devzero.fileno(), tl, mmap.MAP_PRIVATE) + m = StringIO() + def move(dest, src, count): + """move count bytes from src to dest + + The file pointer is left at the end of dest. + """ + m.seek(src) + buf = m.read(count) + m.seek(dest) + m.write(buf) # load our original text m.write(a) @@ -54,7 +65,7 @@ def collect(buf, list): start = buf for l, p in list: - m.move(buf, p, l) + move(buf, p, l) buf += l return (buf - start, start) @@ -68,7 +79,8 @@ end = pos + plen last = 0 while pos < end: - p1, p2, l = struct.unpack(">lll", m[pos:pos + 12]) + m.seek(pos) + p1, p2, l = struct.unpack(">lll", m.read(12)) pull(new, frags, p1 - last) # what didn't change pull([], frags, p2 - p1) # what got deleted new.append((l, pos + 12)) # what got added @@ -78,7 +90,8 @@ t = collect(b2, frags) - return m[t[1]:t[1] + t[0]] + m.seek(t[1]) + return m.read(t[0]) def patchedsize(orig, delta): outlen, last, bin = 0, 0, 0