]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/compat.py
compat.mmap: add py2 context management and use everywhere
[bup.git] / lib / bup / compat.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: