Mercurial > public > mercurial-scm > hg
comparison mercurial/util.py @ 34295:3bb2a9f25fe9
util: add an mmapread method
This is useful for large files that are only partly touched.
Test Plan:
Will be used and tested in a later patch.
Differential Revision: https://phab.mercurial-scm.org/D476
author | Mark Thomas <mbthomas@fb.com> |
---|---|
date | Thu, 21 Sep 2017 05:54:34 -0700 |
parents | be00af4a1ac5 |
children | c41444a39de2 |
comparison
equal
deleted
inserted
replaced
34294:05131c963767 | 34295:3bb2a9f25fe9 |
---|---|
24 import datetime | 24 import datetime |
25 import errno | 25 import errno |
26 import gc | 26 import gc |
27 import hashlib | 27 import hashlib |
28 import imp | 28 import imp |
29 import mmap | |
29 import os | 30 import os |
30 import platform as pyplatform | 31 import platform as pyplatform |
31 import re as remod | 32 import re as remod |
32 import shutil | 33 import shutil |
33 import signal | 34 import signal |
404 if not data: | 405 if not data: |
405 self._eof = True | 406 self._eof = True |
406 else: | 407 else: |
407 self._lenbuf += len(data) | 408 self._lenbuf += len(data) |
408 self._buffer.append(data) | 409 self._buffer.append(data) |
410 | |
411 def mmapread(fp): | |
412 try: | |
413 fd = getattr(fp, 'fileno', lambda: fp)() | |
414 return mmap.mmap(fd, 0, access=mmap.ACCESS_READ) | |
415 except ValueError: | |
416 # Empty files cannot be mmapped, but mmapread should still work. Check | |
417 # if the file is empty, and if so, return an empty buffer. | |
418 if os.fstat(fd).st_size == 0: | |
419 return '' | |
420 raise | |
409 | 421 |
410 def popen2(cmd, env=None, newlines=False): | 422 def popen2(cmd, env=None, newlines=False): |
411 # Setting bufsize to -1 lets the system decide the buffer size. | 423 # Setting bufsize to -1 lets the system decide the buffer size. |
412 # The default for bufsize is 0, meaning unbuffered. This leads to | 424 # The default for bufsize is 0, meaning unbuffered. This leads to |
413 # poor performance on Mac OS X: http://bugs.python.org/issue4194 | 425 # poor performance on Mac OS X: http://bugs.python.org/issue4194 |