1class ProtectedSection:
2 exception = Exception(None)
3
4 def __init__(self, log=(), suppress=()):
5 self.log = log
6 self.suppress = suppress
7
8 def __enter__(self):
9 return self
10
11 def __exit__(self, exc_type, exc_val, exc_tb):
12 if exc_type is None:
13 return True
14 elif 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
20
21
FF
======================================================================
FAIL: test_solution (test.TestSolution.test_solution)
Test everything in a single test case. Only 100% gives a point.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 26, in test_solution
self.assertIsNone(cm.exception)
AssertionError: Exception(None) is not None
======================================================================
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=2)
f | 1 | class ProtectedSection: | f | 1 | class ProtectedSection: |
2 | exception = Exception(None) | 2 | exception = Exception(None) | ||
3 | 3 | ||||
t | 4 | def __init__(self, log=None, suppress=None): | t | 4 | def __init__(self, log=(), suppress=()): |
5 | self.log = log or () | 5 | self.log = log | ||
6 | self.suppress = suppress or () | 6 | self.suppress = suppress | ||
7 | 7 | ||||
8 | def __enter__(self): | 8 | def __enter__(self): | ||
9 | return self | 9 | return self | ||
10 | 10 | ||||
11 | def __exit__(self, exc_type, exc_val, exc_tb): | 11 | def __exit__(self, exc_type, exc_val, exc_tb): | ||
12 | if exc_type is None: | 12 | if exc_type is None: | ||
13 | return True | 13 | return True | ||
14 | elif exc_type in self.log: | 14 | elif 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 | ||
20 | 20 | ||||
21 | 21 | ||||
22 | 22 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
09.11.2024 18:49