[PATCH 2 of 3] util: add a mmapread method

Jun Wu quark at fb.com
Tue Oct 4 10:59:44 EDT 2016


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1475580982 -3600
#      Tue Oct 04 12:36:22 2016 +0100
# Node ID 5ebdfd4ffbedbfe66b7a36cbd06b1e8e624ae7ad
# Parent  663bae87a82f1468faf979bacfec30a426bebae1
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 5ebdfd4ffbed
util: add a mmapread method

This method uses mmap to read file contents. It's useful for big files.

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -24,4 +24,5 @@ import gc
 import hashlib
 import imp
+import mmap
 import os
 import re as remod
@@ -338,4 +339,10 @@ class bufferedinputpipe(object):
             self._buffer.append(data)
 
+def mmapread(fp):
+    try:
+        return mmap.mmap(fp.fileno(), 0, access=mmap.ACCESS_READ)
+    except ValueError: # cannot mmap an empty file
+        return ''
+
 def popen2(cmd, env=None, newlines=False):
     # Setting bufsize to -1 lets the system decide the buffer size.


More information about the Mercurial-devel mailing list