]> arthur.barton.de Git - bup.git/commitdiff
compat.mmap: add py2 context management and use everywhere
authorRob Browning <rlb@defaultvalue.org>
Sun, 3 Oct 2021 18:25:08 +0000 (13:25 -0500)
committerRob Browning <rlb@defaultvalue.org>
Mon, 22 Nov 2021 16:35:28 +0000 (10:35 -0600)
It looks like py2 doesn't support mmap __enter__ and __exit__, so add
them, and switch all our mmap instances to be compat.mmap so that
we'll be able to context manage them.

Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/compat.py
lib/bup/helpers.py

index fbeee4a478fd3da42976d8b50a05f03558a2996d..7527ae7695590fdae40f2e723c1df2a081c31b9c 100644 (file)
@@ -14,6 +14,7 @@ if py3:
 
     # pylint: disable=unused-import
     from contextlib import nullcontext
+    from mmap import mmap
     from os import environb as environ
     from os import fsdecode, fsencode
     from shlex import quote
@@ -84,6 +85,7 @@ if py3:
 else:  # Python 2
 
     from contextlib import contextmanager
+    import mmap as py_mmap
 
     ModuleNotFoundError = ImportError
 
@@ -190,6 +192,16 @@ else:  # Python 2
 
     buffer = buffer
 
+    assert not hasattr(py_mmap.mmap, '__enter__')
+    assert not hasattr(py_mmap.mmap, '__exit__')
+
+    class mmap(py_mmap.mmap):
+        def __enter__(self):
+            return self
+        def __exit__(self, type, value, traceback):
+            with pending_raise(value, rethrow=False):
+                self.close()
+
 try:
     import bup_main
 except ModuleNotFoundError:
index 8a8a3421b049ce49c22e8d389f458a778f923e59..3b37a2b72c065eccf1f9610575a1ee3f90212a8c 100644 (file)
@@ -755,7 +755,7 @@ def _mmap_do(f, sz, flags, prot, close):
         # string has all the same behaviour of a zero-length map, ie. it has
         # no elements :)
         return ''
-    map = mmap.mmap(f.fileno(), sz, flags, prot)
+    map = compat.mmap(f.fileno(), sz, flags, prot)
     if close:
         f.close()  # map will persist beyond file close
     return map
@@ -817,7 +817,7 @@ if _mincore:
             pos = _fmincore_chunk_size * ci;
             msize = min(_fmincore_chunk_size, st.st_size - pos)
             try:
-                m = mmap.mmap(fd, msize, mmap.MAP_PRIVATE, 0, 0, pos)
+                m = compat.mmap(fd, msize, mmap.MAP_PRIVATE, 0, 0, pos)
             except mmap.error as ex:
                 if ex.errno == errno.EINVAL or ex.errno == errno.ENODEV:
                     # Perhaps the file was a pipe, i.e. "... | bup split ..."