]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/helpers.py
Check that all context managed objects are properly closed
[bup.git] / lib / bup / helpers.py
index 3b37a2b72c065eccf1f9610575a1ee3f90212a8c..fdc683bd7c2ca739699e760a0b1eda5c3a8f156d 100644 (file)
@@ -12,7 +12,7 @@ import hashlib, heapq, math, operator, time, tempfile
 
 from bup import _helpers
 from bup import compat
-from bup.compat import argv_bytes, byte_int, pending_raise
+from bup.compat import argv_bytes, byte_int, nullcontext, pending_raise
 from bup.io import byte_stream, path_msg
 # This function should really be in helpers, not in bup.options.  But we
 # want options.py to be standalone so people can include it in other projects.
@@ -27,6 +27,10 @@ class Nonlocal:
     pass
 
 
+def nullcontext_if_not(manager):
+    return manager if manager is not None else nullcontext()
+
+
 @contextmanager
 def finalized(enter_result=None, finalize=None):
     assert finalize
@@ -450,11 +454,16 @@ class NotOk(Exception):
 
 class BaseConn:
     def __init__(self, outp):
+        self._base_closed = False
         self.outp = outp
 
     def close(self):
+        self._base_closed = True
         while self._read(65536): pass
 
+    def __del__(self):
+        assert self._base_closed
+
     def _read(self, size):
         raise NotImplementedError("Subclasses must implement _read")