mirror of
https://github.com/markqvist/rnsh.git
synced 2024-09-19 23:55:39 +00:00
27 lines
770 B
Python
27 lines
770 B
Python
import contextlib
|
|
from contextlib import AbstractContextManager
|
|
|
|
|
|
class permit(AbstractContextManager):
|
|
"""Context manager to allow specified exceptions
|
|
|
|
The specified exceptions will be allowed to bubble up. Other
|
|
exceptions are suppressed.
|
|
|
|
After a non-matching exception is suppressed, execution proceeds
|
|
with the next statement following the with statement.
|
|
|
|
with allow(KeyboardInterrupt):
|
|
time.sleep(300)
|
|
# Execution still resumes here if no KeyboardInterrupt
|
|
"""
|
|
|
|
def __init__(self, *exceptions):
|
|
self._exceptions = exceptions
|
|
|
|
def __enter__(self):
|
|
pass
|
|
|
|
def __exit__(self, exctype, excinst, exctb):
|
|
return exctype is not None and not issubclass(exctype, self._exceptions)
|