easy +8 pts

Suppress stderr manager

Build a context manager that redirects stderr to devnull and restores it afterward.

Implement a context manager class `suppress_stderr` that suppresses all output written to `sys.stderr` while inside its `with` block. When the block exits, the original `sys.stderr` must be restored. The class must be used as: with suppress_stderr(): # code that writes to stderr is silenced After the block, writing to `sys.stderr` should work normally again. The class must not suppress `sys.stdout` or any other stream. Ensure your class is usable with `with suppress_stderr() as _:` by returning an appropriate object from `__enter__`. Do not rely on any external libraries; use only `sys` and `io`. Your implementation must pass the provided tests: `test_basic_suppression`, `test_nested_suppression`, `test_restore_after_exit`, and `test_exception_inside_block`.

Constraints

The solution must work for any code that writes to `sys.stderr` during the block. The context manager must restore the original `sys.stderr` even if an exception occurs inside the block. Do not permanently modify `sys.stderr`. The implementation must be a class that supports the context manager protocol. There are no inputs, but the testing harness will invoke the named test functions.

Example

```python
>>> import sys
>>> with suppress_stderr():
...     sys.stderr.write('this is hidden')
>>> # nothing printed
>>> sys.stderr.write('visible again')
visible again
```
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

You can replace `sys.stderr` with an object that discards writes, like `io.StringIO()`.
In `__enter__`, save the original `sys.stderr` and return `self` (or any object).
In `__exit__`, restore the original stream and return `False` so exceptions propagate.
To be safe with `as _:`, you can return `self` from `__enter__`.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.