1class ProtectedSection:
2 """Protect a section of code from exceptions and react accordingly."""
3 def __init__(self, log=(), suppress=()):
4 self.log = log
5 self.suppress = suppress
6 self.exception = None
7
8 def __enter__(self):
9 self.exception = None
10 return self
11
12 def __exit__(self, exc_type, exc_value, exc_tb):
13 if exc_type is None:
14 return False
15
16 if exc_type in self.log:
17 self.exception = exc_value
18 return True
19
20 if exc_type in self.suppress:
21 return True
22
23 return False
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK