1class ProtectedSection:
2 """Context manager that suppresses or logs exceptions based on provided exception types."""
3
4 def __init__(self, log=(), suppress=()):
5 """
6 Initialize the ProtectedSection context manager.
7
8 Args:
9 log (tuple): Tuple of exception types to log. Defaults to an empty tuple.
10 suppress (tuple): Tuple of exception types to suppress. Defaults to an empty tuple.
11 """
12 self._exception = None
13 self._log_exceptions = tuple(
14 filter(lambda exc_type: issubclass(exc_type, Exception), log)
15 )
16 self._suppress_exceptions = tuple(
17 filter(lambda exc_type: issubclass(exc_type, Exception), suppress)
18 )
19
20 def __enter__(self):
21 """Return the context manager instance."""
22 return self
23
24 def __exit__(self, exc_type, exc_value, traceback):
25 """Handle exceptions based on log and suppress settings."""
26 if exc_type in self._log_exceptions:
27 self._exception = exc_value
28 return True
29
30 return exc_type in self._suppress_exceptions
31
32 @property
33 def exception(self):
34 """Return the exception if any."""
35 return self._exception
.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 | """Context manager that suppresses or logs exceptions based on provided exception types.""" | 2 | """Context manager that suppresses or logs exceptions based on provided exception types.""" | ||
3 | 3 | ||||
4 | def __init__(self, log=(), suppress=()): | 4 | def __init__(self, log=(), suppress=()): | ||
5 | """ | 5 | """ | ||
6 | Initialize the ProtectedSection context manager. | 6 | Initialize the ProtectedSection context manager. | ||
7 | 7 | ||||
8 | Args: | 8 | Args: | ||
9 | log (tuple): Tuple of exception types to log. Defaults to an empty tuple. | 9 | log (tuple): Tuple of exception types to log. Defaults to an empty tuple. | ||
10 | suppress (tuple): Tuple of exception types to suppress. Defaults to an empty tuple. | 10 | suppress (tuple): Tuple of exception types to suppress. Defaults to an empty tuple. | ||
11 | """ | 11 | """ | ||
n | 12 | self.exception = None | n | 12 | self._exception = None |
13 | self._log_exceptions = tuple( | 13 | self._log_exceptions = tuple( | ||
14 | filter(lambda exc_type: issubclass(exc_type, Exception), log) | 14 | filter(lambda exc_type: issubclass(exc_type, Exception), log) | ||
15 | ) | 15 | ) | ||
16 | self._suppress_exceptions = tuple( | 16 | self._suppress_exceptions = tuple( | ||
17 | filter(lambda exc_type: issubclass(exc_type, Exception), suppress) | 17 | filter(lambda exc_type: issubclass(exc_type, Exception), suppress) | ||
18 | ) | 18 | ) | ||
19 | 19 | ||||
20 | def __enter__(self): | 20 | def __enter__(self): | ||
21 | """Return the context manager instance.""" | 21 | """Return the context manager instance.""" | ||
22 | return self | 22 | return self | ||
23 | 23 | ||||
24 | def __exit__(self, exc_type, exc_value, traceback): | 24 | def __exit__(self, exc_type, exc_value, traceback): | ||
25 | """Handle exceptions based on log and suppress settings.""" | 25 | """Handle exceptions based on log and suppress settings.""" | ||
26 | if exc_type in self._log_exceptions: | 26 | if exc_type in self._log_exceptions: | ||
n | 27 | self.exception = exc_value | n | 27 | self._exception = exc_value |
28 | return True | 28 | return True | ||
29 | 29 | ||||
30 | return exc_type in self._suppress_exceptions | 30 | return exc_type in self._suppress_exceptions | ||
t | t | 31 | |||
32 | @property | ||||
33 | def exception(self): | ||||
34 | """Return the exception if any.""" | ||||
35 | return self._exception |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|