mercurial/pure/mpatch.py
changeset 7775 5280c39778b6
parent 7699 fac054f84600
child 8225 46293a0c7e9f
--- 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