comparison mercurial/util.py @ 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 6d3b67a837a6
children a61287a95dc3
comparison
equal deleted inserted replaced
44034:ab595920de0e 44035:8ed8dfbeabb9
413 self._buffer.append(data) 413 self._buffer.append(data)
414 414
415 return data 415 return data
416 416
417 417
418 def mmapread(fp): 418 def mmapread(fp, size=None):
419 if size == 0:
420 # size of 0 to mmap.mmap() means "all data"
421 # rather than "zero bytes", so special case that.
422 return b''
423 elif size is None:
424 size = 0
419 try: 425 try:
420 fd = getattr(fp, 'fileno', lambda: fp)() 426 fd = getattr(fp, 'fileno', lambda: fp)()
421 return mmap.mmap(fd, 0, access=mmap.ACCESS_READ) 427 return mmap.mmap(fd, size, access=mmap.ACCESS_READ)
422 except ValueError: 428 except ValueError:
423 # Empty files cannot be mmapped, but mmapread should still work. Check 429 # Empty files cannot be mmapped, but mmapread should still work. Check
424 # if the file is empty, and if so, return an empty buffer. 430 # if the file is empty, and if so, return an empty buffer.
425 if os.fstat(fd).st_size == 0: 431 if os.fstat(fd).st_size == 0:
426 return b'' 432 return b''