Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class suppress:
- """
- Suppress *exceptions in contextmanager
- If subclass is True, each exception is checked,
- if it's a subclass. Exception for example catch all Exceptions.
- >>> with suppress(ArithmeticError, subclass=True) as exc:
- >>> 1 / 0
- >>> print(exc.etype, exc.value, exc.traceback)
- """
- def __init__(self, *exceptions, subclass=False):
- self.exceptions = set(exceptions)
- self.subclass = subclass
- def __enter__(self):
- return self
- def __exit__(self, etype, val, tb):
- self.etype = etype
- self.value = val
- self.traceback = tb
- if self.subclass:
- return any(
- issubclass(etype, exc)
- for exc in self.exceptions
- )
- else:
- return etype in self.exceptions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement