medium +20 pts

Suppress Specific Exceptions

Craft a context manager that silently suppresses chosen exception types.

In Python, the built-in `contextlib.suppress` allows you to suppress specific exceptions inside a `with` block. Your task is to implement your own version as a context manager class named `SuppressExceptions`. Define a class `SuppressExceptions` such that: - Its constructor accepts zero or more exception types as positional arguments. - When used in a `with` statement, any exception that is an instance of one of the provided exception types (or a subclass) is silently swallowed and execution continues after the `with` block. - Any other exception propagates normally. - It is re-usable: the same instance can be used in multiple `with` blocks. - It works even if called with no arguments (in that case, nothing is suppressed). Implementation requirements: - You must implement the context manager protocol (`__enter__` and `__exit__`). - You may NOT use `contextlib.suppress` or any other context manager utility. - You may NOT use `contextlib` or any other module beyond the standard library. Your class signature is exactly: ```python class SuppressExceptions: def __init__(self, *exception_types): ... def __enter__(self): ... def __exit__(self, exc_type, exc_val, exc_tb): ... ``` Additionally, you must provide four test functions that validate correct behavior: - `test_suppress_value_error()`: should return True if suppressing `ValueError` works (no exception escapes the `with` block). - `test_propagates_type_error()`: should return True if a `TypeError` is NOT suppressed when only `ValueError` is passed. - `test_no_args_no_suppression()`: should return True if an exception is NOT suppressed when no exception types are provided. - `test_multiple_types()`: should return True if suppression works when multiple exception types are provided. Each test function should use `SuppressExceptions` inside a `with` block and return a boolean indicating whether the expected behavior occurred. The tests must be named exactly as listed above and take no arguments.

Constraints

The `exception_types` are exception classes (subclasses of `BaseException`). There is no upper bound on the number of exception types. The method `__exit__` should return a boolean-like value indicating whether the exception was suppressed. Test functions take no arguments and return a boolean.

Example

>>> from suppress_exceptions import SuppressExceptions, test_suppress_value_error, test_propagates_type_error, test_no_args_no_suppression, test_multiple_types
>>> test_suppress_value_error()
True
>>> test_propagates_type_error()
True
>>> test_no_args_no_suppression()
True
>>> test_multiple_types()
True
20 points ~20 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

The `__exit__` method receives the exception type, value, and traceback. If `exc_type` is None, no exception occurred.
To check if the exception is suppressed, use `issubclass(exc_type, self.exception_types)` — but be careful when no exception types are provided.
Return `True` from `__exit__` to suppress the exception and `False` to let it propagate.
In each test function, use a try/except block around a `with` statement and return `True`/`False` based on whether the expected exception behavior occurs.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.