Patchwork D476: util: add an mmapread method

login
register
mail settings
Submitter phabricator
Date Sept. 13, 2017, 5:22 p.m.
Message ID <8352dc771f8cdab7828891ce086d05ad@localhost.localdomain>
Download mbox | patch
Permalink /patch/23865/
State Not Applicable
Headers show

Comments

phabricator - Sept. 13, 2017, 5:22 p.m.
mbthomas updated this revision to Diff 1785.
mbthomas added a comment.


  Accept both file objects and fds

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D476?vs=1157&id=1785

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D476

AFFECTED FILES
  mercurial/util.py

CHANGE DETAILS




To: mbthomas, #fbhgext, #hg-reviewers, quark
Cc: quark, indygreg, simonfar, mercurial-devel

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -26,6 +26,7 @@ 
 import gc
 import hashlib
 import imp
+import mmap
 import os
 import platform as pyplatform
 import re as remod
@@ -407,6 +408,13 @@ 
             self._lenbuf += len(data)
             self._buffer.append(data)
 
+def mmapread(fp):
+    try:
+        fd = getattr(fp, 'fileno', lambda: fp)()
+        return mmap.mmap(fd, 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.
     # The default for bufsize is 0, meaning unbuffered. This leads to