From ca1b82ee3ab053c50fc7c1fc491928c5c2c8fc19 Mon Sep 17 00:00:00 2001 From: Rob Browning Date: Sun, 3 Oct 2021 12:44:25 -0500 Subject: [PATCH] compat.pending_raise: allow/ignore None ex; make rethrow optional This supports compact __exit__ handlers, e.g. def __exit__(self, type, value, ...): with pending_raise(value, rethrow=False): clean_up() Signed-off-by: Rob Browning Tested-by: Rob Browning --- lib/bup/compat.py | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/bup/compat.py b/lib/bup/compat.py index 7272201..9002025 100644 --- a/lib/bup/compat.py +++ b/lib/bup/compat.py @@ -40,16 +40,18 @@ if py3: 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.rethrow = rethrow 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 def items(x): @@ -126,22 +128,28 @@ else: # Python 2 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.rethrow = rethrow def __enter__(self): - add_ex_tb(self.ex) + if self.ex: + add_ex_tb(self.ex) 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 - add_ex_tb(exc_value) - add_ex_ctx(exc_value, self.ex) def dump_traceback(ex): stack = [ex] -- 2.39.2