Mercurial > hg
changeset 44035:8ed8dfbeabb9
mmap: add a size argument to mmapread
With this argument, we can control the size of the mmap created. (previously it
was always the whole file.
Differential Revision: https://phab.mercurial-scm.org/D7808
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Tue, 07 Jan 2020 12:09:36 +0100 |
parents | ab595920de0e |
children | b084ad4875a4 |
files | mercurial/util.py |
diffstat | 1 files changed, 8 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/util.py Tue Jan 07 12:26:37 2020 +0100 +++ b/mercurial/util.py Tue Jan 07 12:09:36 2020 +0100 @@ -415,10 +415,16 @@ return data -def mmapread(fp): +def mmapread(fp, size=None): + if size == 0: + # size of 0 to mmap.mmap() means "all data" + # rather than "zero bytes", so special case that. + return b'' + elif size is None: + size = 0 try: fd = getattr(fp, 'fileno', lambda: fp)() - return mmap.mmap(fd, 0, access=mmap.ACCESS_READ) + return mmap.mmap(fd, size, 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.