1class ProtectedSection:
2 def __init__(self, log=(), suppress=()):
3 self.log = log
4 self.suppress = suppress
5 self.exception = None
6
7 def __enter__(self):
8 self.exception = None
9 return self
10
11 def __exit__(self, exc_type, exc_val, exc_tb):
12 if exc_type is None:
13 return True
14
15 if isinstance(exc_val, self.log):
16 self.exception = exc_val
17 return True
18
19 if isinstance(exc_val, self.suppress):
20 return True
21
22 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 133, in test_special_cases
self.assertIsNone(cm.exception)
AssertionError: ZeroDivisionError('Ако ползваш AI генератори, ще влезеш в черния списък!') 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 = log | 3 | self.log = log | ||
4 | self.suppress = suppress | 4 | self.suppress = suppress | ||
5 | self.exception = None | 5 | self.exception = None | ||
6 | 6 | ||||
7 | def __enter__(self): | 7 | def __enter__(self): | ||
t | t | 8 | self.exception = None | ||
8 | return self | 9 | return self | ||
9 | 10 | ||||
10 | def __exit__(self, exc_type, exc_val, exc_tb): | 11 | def __exit__(self, exc_type, exc_val, exc_tb): | ||
11 | if exc_type is None: | 12 | if exc_type is None: | ||
12 | return True | 13 | return True | ||
13 | 14 | ||||
14 | if isinstance(exc_val, self.log): | 15 | if isinstance(exc_val, self.log): | ||
15 | self.exception = exc_val | 16 | self.exception = exc_val | ||
16 | return True | 17 | return True | ||
17 | 18 | ||||
18 | if isinstance(exc_val, self.suppress): | 19 | if isinstance(exc_val, self.suppress): | ||
19 | return True | 20 | return True | ||
20 | 21 | ||||
21 | return False | 22 | return False | ||
22 | 23 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | class ProtectedSection: | f | 1 | class ProtectedSection: |
2 | def __init__(self, log=(), suppress=()): | 2 | def __init__(self, log=(), suppress=()): | ||
3 | self.log = log | 3 | self.log = log | ||
4 | self.suppress = suppress | 4 | self.suppress = 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 | ||||
10 | def __exit__(self, exc_type, exc_val, exc_tb): | 10 | def __exit__(self, exc_type, exc_val, exc_tb): | ||
n | 11 | if exc_val is None: | n | 11 | if exc_type is None: |
12 | return True | 12 | return True | ||
13 | 13 | ||||
14 | if isinstance(exc_val, self.log): | 14 | if isinstance(exc_val, self.log): | ||
15 | self.exception = exc_val | 15 | self.exception = exc_val | ||
16 | return True | 16 | return True | ||
17 | 17 | ||||
18 | if isinstance(exc_val, self.suppress): | 18 | if isinstance(exc_val, self.suppress): | ||
19 | return True | 19 | return True | ||
20 | 20 | ||||
21 | return False | 21 | return False | ||
22 | 22 | ||||
t | 23 | t | |||
24 | def test_reuse_instance(self): | ||||
25 | protected_section = ProtectedSection(log=(ZeroDivisionError, IndexError), suppress=(TypeError, ValueError)) | ||||
26 | |||||
27 | with protected_section as err: | ||||
28 | x = 1/0 | ||||
29 | self.assertIsInstance(err.exception, ZeroDivisionError) | ||||
30 | self.assertEqual(str(err.exception), "division by zero") | ||||
31 | |||||
32 | #err.exception = None ??? | ||||
33 | |||||
34 | with protected_section as err: | ||||
35 | x = "text" + 5 | ||||
36 | self.assertIsInstance(err.exception, ZeroDivisionError) # ??? |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|