From d6bd7d7214df31665a361cbd9aa8285176460b1c Mon Sep 17 00:00:00 2001 From: Prodesire Date: Sun, 17 Dec 2017 17:14:32 +0800 Subject: [PATCH] add exception.ignore --- pydu/exception.py | 9 +++++++++ tests/test_exception.py | 7 +++++++ 2 files changed, 16 insertions(+) create mode 100644 pydu/exception.py create mode 100644 tests/test_exception.py diff --git a/pydu/exception.py b/pydu/exception.py new file mode 100644 index 0000000..49e917b --- /dev/null +++ b/pydu/exception.py @@ -0,0 +1,9 @@ +from contextlib import contextmanager + + +@contextmanager +def ignore(*exceptions): + try: + yield + except exceptions: + pass diff --git a/tests/test_exception.py b/tests/test_exception.py new file mode 100644 index 0000000..6f30aca --- /dev/null +++ b/tests/test_exception.py @@ -0,0 +1,7 @@ +from pydu.exception import ignore + + +def test_ignore(): + with ignore(ValueError, AttributeError): + int('abc') + int.no_exists_func()