]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/compat.py
Remove Client __del__ in favor of context management
[bup.git] / lib / bup / compat.py
index a06ffe8ee581d9cab13e1764740241768d2d2210..fbeee4a478fd3da42976d8b50a05f03558a2996d 100644 (file)
@@ -1,6 +1,5 @@
 
 from __future__ import absolute_import, print_function
 
 from __future__ import absolute_import, print_function
-from array import array
 from binascii import hexlify
 from traceback import print_exception
 import os, sys
 from binascii import hexlify
 from traceback import print_exception
 import os, sys
@@ -13,9 +12,13 @@ py3 = py_maj >= 3
 
 if py3:
 
 
 if py3:
 
+    # pylint: disable=unused-import
+    from contextlib import nullcontext
     from os import environb as environ
     from os import fsdecode, fsencode
     from shlex import quote
     from os import environb as environ
     from os import fsdecode, fsencode
     from shlex import quote
+    # pylint: disable=undefined-variable
+    # (for python2 looking here)
     ModuleNotFoundError = ModuleNotFoundError
     input = input
     range = range
     ModuleNotFoundError = ModuleNotFoundError
     input = input
     range = range
@@ -38,18 +41,19 @@ if py3:
         return ex
 
     class pending_raise:
         return ex
 
     class pending_raise:
-        """Rethrow either the provided ex, or any exception raised by the with
-        statement body.  (Supports Python 2 compatibility.)
+        """If rethrow is true, rethrow ex (if any), unless the body throws.
+
+        (Supports Python 2 compatibility.)
 
         """
 
         """
-        def __init__(self, ex):
+        def __init__(self, ex, rethrow=True):
             self.ex = ex
             self.ex = ex
+            self.rethrow = rethrow
         def __enter__(self):
             return None
         def __exit__(self, exc_type, exc_value, traceback):
         def __enter__(self):
             return None
         def __exit__(self, exc_type, exc_value, traceback):
-            if not exc_type:
+            if not exc_type and self.ex and self.rethrow:
                 raise self.ex
                 raise self.ex
-            return None
 
     def items(x):
         return x.items()
 
     def items(x):
         return x.items()
@@ -79,6 +83,8 @@ if py3:
 
 else:  # Python 2
 
 
 else:  # Python 2
 
+    from contextlib import contextmanager
+
     ModuleNotFoundError = ImportError
 
     def fsdecode(x):
     ModuleNotFoundError = ImportError
 
     def fsdecode(x):
@@ -88,13 +94,24 @@ else:  # Python 2
         return x
 
     from pipes import quote
         return x
 
     from pipes import quote
+    # pylint: disable=unused-import
     from os import environ, getcwd
 
     from os import environ, getcwd
 
+    # pylint: disable=unused-import
     from bup.py2raise import reraise
 
     from bup.py2raise import reraise
 
+    @contextmanager
+    def nullcontext(enter_result=None):
+        yield enter_result
+
+    # on py3 this causes errors, obviously
+    # pylint: disable=undefined-variable
     input = raw_input
     input = raw_input
+    # pylint: disable=undefined-variable
     range = xrange
     range = xrange
+    # pylint: disable=undefined-variable
     str_type = basestring
     str_type = basestring
+    # pylint: disable=undefined-variable
     int_types = (int, long)
 
     hexstr = hexlify
     int_types = (int, long)
 
     hexstr = hexlify
@@ -118,24 +135,28 @@ else:  # Python 2
         return ex
 
     class pending_raise:
         return ex
 
     class pending_raise:
-        """Rethrow either the provided ex, or any exception raised by the with
-        statement body, after making ex the __context__ of the newer
-        exception (assuming there's no existing __context__).  Ensure
-        the exceptions have __tracebacks__.  (Supports Python 2
-        compatibility.)
+        """If rethrow is true, rethrow ex (if any), unless the body throws.
+
+        If the body does throw, make any provided ex the __context__
+        of the newer exception (assuming there's no existing
+        __context__).  Ensure the exceptions have __tracebacks__.
+        (Supports Python 2 compatibility.)
 
         """
 
         """
-        def __init__(self, ex):
+        def __init__(self, ex, rethrow=True):
             self.ex = ex
             self.ex = ex
+            self.rethrow = rethrow
         def __enter__(self):
         def __enter__(self):
-            add_ex_tb(self.ex)
-            return None
+            if self.ex:
+                add_ex_tb(self.ex)
         def __exit__(self, exc_type, exc_value, traceback):
         def __exit__(self, exc_type, exc_value, traceback):
-            if not exc_type:
+            if exc_value:
+                if self.ex:
+                    add_ex_tb(exc_value)
+                    add_ex_ctx(exc_value, self.ex)
+                return
+            if self.rethrow and self.ex:
                 raise self.ex
                 raise self.ex
-            add_ex_tb(exc_value)
-            add_ex_ctx(exc_value, self.ex)
-            return None
 
     def dump_traceback(ex):
         stack = [ex]
 
     def dump_traceback(ex):
         stack = [ex]