1class ProtectedSection:
 2    """Handle exceptions gracefully within a given section"""
 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 is None:
15            return True
16        if isinstance(exc_val, self.log):
17            self.exception = exc_val
18            return True
19        if isinstance(exc_val, self.suppress):
20            return True
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 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 | """Handle exceptions gracefully within a given section""" | 2 | """Handle exceptions gracefully within a given section""" | ||
| 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): | ||
| n | n | 10 | self.exception = None | ||
| 10 | return self | 11 | return self | ||
| 11 | 12 | ||||
| 12 | def __exit__(self, exc_type, exc_val, exc_tb): | 13 | def __exit__(self, exc_type, exc_val, exc_tb): | ||
| 13 | if exc_type is None: | 14 | if exc_type is None: | ||
| 14 | return True | 15 | return True | ||
| 15 | if isinstance(exc_val, self.log): | 16 | if isinstance(exc_val, self.log): | ||
| 16 | self.exception = exc_val | 17 | self.exception = exc_val | ||
| 17 | return True | 18 | return True | ||
| 18 | if isinstance(exc_val, self.suppress): | 19 | if isinstance(exc_val, self.suppress): | ||
| t | 19 | self.exception = None | t | ||
| 20 | return True | 20 | return True | ||
| 21 | return False | 21 | return False | 
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 
 | 
 | |||||||||
| f | 1 | class ProtectedSection: | f | 1 | class ProtectedSection: | 
| 2 | """Handle exceptions gracefully within a given section""" | 2 | """Handle exceptions gracefully within a given section""" | ||
| 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 | return self | 10 | return self | ||
| 11 | 11 | ||||
| 12 | def __exit__(self, exc_type, exc_val, exc_tb): | 12 | def __exit__(self, exc_type, exc_val, exc_tb): | ||
| 13 | if exc_type is None: | 13 | if exc_type is None: | ||
| 14 | return True | 14 | return True | ||
| 15 | if isinstance(exc_val, self.log): | 15 | if isinstance(exc_val, self.log): | ||
| 16 | self.exception = exc_val | 16 | self.exception = exc_val | ||
| 17 | return True | 17 | return True | ||
| 18 | if isinstance(exc_val, self.suppress): | 18 | if isinstance(exc_val, self.suppress): | ||
| t | t | 19 | self.exception = None | ||
| 19 | return True | 20 | return True | ||
| 20 | return False | 21 | return False | 
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 
 | 
 | |||||||||
| f | 1 | class ProtectedSection: | f | 1 | class ProtectedSection: | 
| 2 | """Handle exceptions gracefully within a given section""" | 2 | """Handle exceptions gracefully within a given section""" | ||
| 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 | return self | 10 | return self | ||
| 11 | 11 | ||||
| 12 | def __exit__(self, exc_type, exc_val, exc_tb): | 12 | def __exit__(self, exc_type, exc_val, exc_tb): | ||
| 13 | if exc_type is None: | 13 | if exc_type is None: | ||
| 14 | return True | 14 | return True | ||
| 15 | if isinstance(exc_val, self.log): | 15 | if isinstance(exc_val, self.log): | ||
| 16 | self.exception = exc_val | 16 | self.exception = exc_val | ||
| 17 | return True | 17 | return True | ||
| 18 | if isinstance(exc_val, self.suppress): | 18 | if isinstance(exc_val, self.suppress): | ||
| 19 | return True | 19 | return True | ||
| 20 | return False | 20 | return False | ||
| t | 21 | t | |||
| 22 | with ProtectedSection(log=(ZeroDivisionError, IndexError), suppress=(TypeError, ZeroDivisionError, Exception)) as err: | ||||
| 23 | x = 1 / 0 | ||||
| 24 | |||||
| 25 | print(err.exception) | ||||
| 26 | # division by zero | ||||
| 27 | print(type(err.exception)) | ||||
| 28 | # <class 'ZeroDivisionError'> | 
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 
 | 
 | |||||||||
| f | 1 | class ProtectedSection: | f | 1 | class ProtectedSection: | 
| n | 2 | """Handle exceptions gracefully within a given context""" | n | 2 | """Handle exceptions gracefully within a given section""" | 
| 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 | return self | 10 | return self | ||
| 11 | 11 | ||||
| 12 | def __exit__(self, exc_type, exc_val, exc_tb): | 12 | def __exit__(self, exc_type, exc_val, exc_tb): | ||
| 13 | if exc_type is None: | 13 | if exc_type is None: | ||
| 14 | return True | 14 | return True | ||
| 15 | if isinstance(exc_val, self.log): | 15 | if isinstance(exc_val, self.log): | ||
| 16 | self.exception = exc_val | 16 | self.exception = exc_val | ||
| 17 | return True | 17 | return True | ||
| 18 | if isinstance(exc_val, self.suppress): | 18 | if isinstance(exc_val, self.suppress): | ||
| 19 | return True | 19 | return True | ||
| 20 | return False | 20 | return False | ||
| 21 | 21 | ||||
| t | t | 22 | with ProtectedSection(log=(ZeroDivisionError, IndexError), suppress=(TypeError, ZeroDivisionError, Exception)) as err: | ||
| 23 | x = 1 / 0 | ||||
| 24 | |||||
| 25 | print(err.exception) | ||||
| 26 | # division by zero | ||||
| 27 | print(type(err.exception)) | ||||
| 28 | # <class 'ZeroDivisionError'> | 
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 
 | 
 | |||||||||
| t | 1 | class ProtectedSection: | t | 1 | class ProtectedSection: | 
| 2 | """Handle exceptions gracefully within a given context""" | 2 | """Handle exceptions gracefully within a given context""" | ||
| 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 | return self | 10 | return self | ||
| 11 | 11 | ||||
| 12 | def __exit__(self, exc_type, exc_val, exc_tb): | 12 | def __exit__(self, exc_type, exc_val, exc_tb): | ||
| 13 | if exc_type is None: | 13 | if exc_type is None: | ||
| 14 | return True | 14 | return True | ||
| 15 | if isinstance(exc_val, self.log): | 15 | if isinstance(exc_val, self.log): | ||
| 16 | self.exception = exc_val | 16 | self.exception = exc_val | ||
| 17 | return True | 17 | return True | ||
| 18 | if isinstance(exc_val, self.suppress): | 18 | if isinstance(exc_val, self.suppress): | ||
| 19 | return True | 19 | return True | ||
| 20 | return False | 20 | return False | ||
| 21 | 21 | 
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 
 | 
 | |||||||||