1class ProtectedSection:
2 """
3 A context manager for handling exceptions in two ways.
4 The ways are logging and suppression.
5 """
6 def __init__(self, log=(), suppress=()):
7 """Initialize the ProtectedSection which can accept logging and suppression parameters"""
8 self._log = tuple(log)
9 self._suppress = tuple(suppress)
10 self._exception = None
11
12 @property
13 def exception(self):
14 """Return last logged exception"""
15 return self._exception
16
17 def __enter__(self):
18 """Enter context manager and return self attribute"""
19 return self
20
21 def __exit__(self, exc_type, exc_val, exc_tb):
22 """Exit context manager and handle exceptions"""
23 if exc_type and isinstance(exc_val, self._log):
24 # if exc_type and issubclass(exc_type, self._log):
25 self._exception = exc_val
26 return True
27 elif exc_type and isinstance(exc_val, self._suppress):
28 return True
29 return False
.F
======================================================================
FAIL: test_special_cases (test.TestSolution.test_special_cases)
Test special cases to show you that you missed something.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 128, in test_special_cases
self.assertIsNone(cm.exception)
AssertionError: ZeroDivisionError('division by zero') is not None
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (failures=1)
f | 1 | class ProtectedSection: | f | 1 | class ProtectedSection: |
2 | """ | 2 | """ | ||
3 | A context manager for handling exceptions in two ways. | 3 | A context manager for handling exceptions in two ways. | ||
4 | The ways are logging and suppression. | 4 | The ways are logging and suppression. | ||
5 | """ | 5 | """ | ||
6 | def __init__(self, log=(), suppress=()): | 6 | def __init__(self, log=(), suppress=()): | ||
7 | """Initialize the ProtectedSection which can accept logging and suppression parameters""" | 7 | """Initialize the ProtectedSection which can accept logging and suppression parameters""" | ||
8 | self._log = tuple(log) | 8 | self._log = tuple(log) | ||
9 | self._suppress = tuple(suppress) | 9 | self._suppress = tuple(suppress) | ||
10 | self._exception = None | 10 | self._exception = None | ||
11 | 11 | ||||
12 | @property | 12 | @property | ||
13 | def exception(self): | 13 | def exception(self): | ||
14 | """Return last logged exception""" | 14 | """Return last logged exception""" | ||
15 | return self._exception | 15 | return self._exception | ||
16 | 16 | ||||
17 | def __enter__(self): | 17 | def __enter__(self): | ||
t | 18 | """Enters context manager and return self attribute""" | t | 18 | """Enter context manager and return self attribute""" |
19 | return self | 19 | return self | ||
20 | 20 | ||||
21 | def __exit__(self, exc_type, exc_val, exc_tb): | 21 | def __exit__(self, exc_type, exc_val, exc_tb): | ||
22 | """Exit context manager and handle exceptions""" | 22 | """Exit context manager and handle exceptions""" | ||
23 | if exc_type and isinstance(exc_val, self._log): | 23 | if exc_type and isinstance(exc_val, self._log): | ||
24 | # if exc_type and issubclass(exc_type, self._log): | 24 | # if exc_type and issubclass(exc_type, self._log): | ||
25 | self._exception = exc_val | 25 | self._exception = exc_val | ||
26 | return True | 26 | return True | ||
27 | elif exc_type and isinstance(exc_val, self._suppress): | 27 | elif exc_type and isinstance(exc_val, self._suppress): | ||
28 | return True | 28 | return True | ||
29 | return False | 29 | return False |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
08.11.2024 17:37
08.11.2024 17:39