1class ProtectedSection:
2 def __init__(self, log=(), suppress=()):
3 self.log_elements = log
4 self.suppress_elements = suppress
5 self.exception = None
6
7 def __enter__(self):
8 return self
9
10 def __exit__(self, exc_type, exc_value, exc_tb):
11 if exc_type is None:
12 return False
13
14 if isinstance(exc_value, self.log_elements):
15 self.exception = exc_value
16 return True
17
18 if isinstance(exc_value, self.suppress_elements):
19 return True
20
21 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.001s
FAILED (failures=1)
| f | 1 | class ProtectedSection: | f | 1 | class ProtectedSection: |
| 2 | def __init__(self, log=(), suppress=()): | 2 | def __init__(self, log=(), suppress=()): | ||
| 3 | self.log_elements = log | 3 | self.log_elements = log | ||
| 4 | self.suppress_elements = suppress | 4 | self.suppress_elements = suppress | ||
| 5 | self.exception = None | 5 | self.exception = None | ||
| 6 | 6 | ||||
| 7 | def __enter__(self): | 7 | def __enter__(self): | ||
| 8 | return self | 8 | return self | ||
| 9 | 9 | ||||
| n | 10 | def __exit__(self, exc_type, exc_value, traceback): | n | 10 | def __exit__(self, exc_type, exc_value, exc_tb): |
| 11 | if exc_type is None: | 11 | if exc_type is None: | ||
| 12 | return False | 12 | return False | ||
| 13 | 13 | ||||
| 14 | if isinstance(exc_value, self.log_elements): | 14 | if isinstance(exc_value, self.log_elements): | ||
| n | 15 | self.exception = exc_value | n | 15 | self.exception = exc_value |
| 16 | return True | 16 | return True | ||
| 17 | 17 | ||||
| 18 | if isinstance(exc_value, self.suppress_elements): | 18 | if isinstance(exc_value, self.suppress_elements): | ||
| n | 19 | return True | n | 19 | return True |
| 20 | 20 | ||||
| 21 | return False | 21 | return False | ||
| 22 | 22 | ||||
| t | 23 | t |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||