From patchwork Thu Sep 21 12:56:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: D476: util: add an mmapread method From: phabricator X-Patchwork-Id: 24080 Message-Id: <38bf363876f839a2eca2c91d1929e439@localhost.localdomain> To: mercurial-devel@mercurial-scm.org Date: Thu, 21 Sep 2017 12:56:54 +0000 mbthomas updated this revision to Diff 1963. REPOSITORY rHG Mercurial CHANGES SINCE LAST UPDATE https://phab.mercurial-scm.org/D476?vs=1785&id=1963 REVISION DETAIL https://phab.mercurial-scm.org/D476 AFFECTED FILES mercurial/util.py CHANGE DETAILS To: mbthomas, #fbhgext, #hg-reviewers, quark, durin42 Cc: durin42, quark, indygreg, simonfar, mercurial-devel 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,17 @@ 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: + # Empty files cannot be mmapped, but mmapread should still work. Check + # if the file is empty, and if so, return an empty buffer. + if os.fstat(fd).st_size == 0: + return '' + raise + 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