1class ProtectedSection:
2 """Handle specific exceptions in a controlled manner."""
3
4 def __init__(self, log=(), suppress=()):
5 self.log = log
6 self.suppress = suppress
7 self.exception = None
8
9 def __enter__(self):
10 self.exception = None
11 return self
12
13 def __exit__(self, exc_type, exc_val, exc_tb):
14 if exc_type in self.log:
15 self.exception = exc_val
16 return True
17 elif exc_type in self.suppress:
18 return True
19 return False
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
| f | 1 | class ProtectedSection: | f | 1 | class ProtectedSection: |
| 2 | """Handle specific exceptions in a controlled manner.""" | 2 | """Handle specific exceptions in a controlled manner.""" | ||
| 3 | 3 | ||||
| 4 | def __init__(self, log=(), suppress=()): | 4 | def __init__(self, log=(), suppress=()): | ||
| 5 | self.log = log | 5 | self.log = log | ||
| 6 | self.suppress = suppress | 6 | self.suppress = suppress | ||
| 7 | self.exception = None | 7 | self.exception = None | ||
| 8 | 8 | ||||
| 9 | def __enter__(self): | 9 | def __enter__(self): | ||
| 10 | self.exception = None | 10 | self.exception = None | ||
| 11 | return self | 11 | return self | ||
| 12 | 12 | ||||
| 13 | def __exit__(self, exc_type, exc_val, exc_tb): | 13 | def __exit__(self, exc_type, exc_val, exc_tb): | ||
| 14 | if exc_type in self.log: | 14 | if exc_type in self.log: | ||
| 15 | self.exception = exc_val | 15 | self.exception = exc_val | ||
| 16 | return True | 16 | return True | ||
| 17 | elif exc_type in self.suppress: | 17 | elif exc_type in self.suppress: | ||
| 18 | return True | 18 | return True | ||
| 19 | return False | 19 | return False | ||
| t | 20 | t | |||
| 21 | |||||
| 22 | protected_section = ProtectedSection(log=(ValueError,)) | ||||
| 23 | |||||
| 24 | with protected_section as cm1: | ||||
| 25 | raise ValueError('Text 1') | ||||
| 26 | print(cm1.exception) | ||||
| 27 | |||||
| 28 | with protected_section as cm2: | ||||
| 29 | raise ValueError('Text 2') | ||||
| 30 | print(cm2.exception) | ||||
| 31 | |||||
| 32 | print(cm1.exception) |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| n | 1 | n | |||
| 2 | class ProtectedSection: | 1 | class ProtectedSection: | ||
| 3 | """Handle specific exceptions in a controlled manner.""" | 2 | """Handle specific exceptions in a controlled manner.""" | ||
| 4 | 3 | ||||
| 5 | def __init__(self, log=(), suppress=()): | 4 | def __init__(self, log=(), suppress=()): | ||
| 6 | self.log = log | 5 | self.log = log | ||
| 7 | self.suppress = suppress | 6 | self.suppress = suppress | ||
| 8 | self.exception = None | 7 | self.exception = None | ||
| 9 | 8 | ||||
| 10 | def __enter__(self): | 9 | def __enter__(self): | ||
| n | n | 10 | self.exception = None | ||
| 11 | return self | 11 | return self | ||
| 12 | 12 | ||||
| 13 | def __exit__(self, exc_type, exc_val, exc_tb): | 13 | def __exit__(self, exc_type, exc_val, exc_tb): | ||
| 14 | if exc_type in self.log: | 14 | if exc_type in self.log: | ||
| 15 | self.exception = exc_val | 15 | self.exception = exc_val | ||
| 16 | return True | 16 | return True | ||
| 17 | elif exc_type in self.suppress: | 17 | elif exc_type in self.suppress: | ||
| 18 | return True | 18 | return True | ||
| 19 | return False | 19 | return False | ||
| t | t | 20 | |||
| 21 | |||||
| 22 | protected_section = ProtectedSection(log=(ValueError,)) | ||||
| 23 | |||||
| 24 | with protected_section as cm1: | ||||
| 25 | raise ValueError('Text 1') | ||||
| 26 | print(cm1.exception) | ||||
| 27 | |||||
| 28 | with protected_section as cm2: | ||||
| 29 | raise ValueError('Text 2') | ||||
| 30 | print(cm2.exception) | ||||
| 31 | |||||
| 32 | print(cm1.exception) |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | f | 1 | ||
| 2 | class ProtectedSection: | 2 | class ProtectedSection: | ||
| 3 | """Handle specific exceptions in a controlled manner.""" | 3 | """Handle specific exceptions in a controlled manner.""" | ||
| 4 | 4 | ||||
| t | 5 | def __init__(self, log = (), suppress = ()): | t | 5 | def __init__(self, log=(), suppress=()): |
| 6 | self.log = log | 6 | self.log = log | ||
| 7 | self.suppress = suppress | 7 | self.suppress = suppress | ||
| 8 | self.exception = None | 8 | self.exception = None | ||
| 9 | 9 | ||||
| 10 | def __enter__(self): | 10 | def __enter__(self): | ||
| 11 | return self | 11 | return self | ||
| 12 | 12 | ||||
| 13 | def __exit__(self, exc_type, exc_val, exc_tb): | 13 | def __exit__(self, exc_type, exc_val, exc_tb): | ||
| 14 | if exc_type in self.log: | 14 | if exc_type in self.log: | ||
| 15 | self.exception = exc_val | 15 | self.exception = exc_val | ||
| 16 | return True | 16 | return True | ||
| 17 | elif exc_type in self.suppress: | 17 | elif exc_type in self.suppress: | ||
| 18 | return True | 18 | return True | ||
| 19 | return False | 19 | return False |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||