diff --git a/satella/coding/recast_exceptions.py b/satella/coding/recast_exceptions.py index 4c98b00f2e780c7e26ac2a952f5727b5b73a6a12..c7e46e75d8e905f27b98894f3e787d08820e5e7e 100644 --- a/satella/coding/recast_exceptions.py +++ b/satella/coding/recast_exceptions.py @@ -45,24 +45,18 @@ class rethrow_as(object): Should return a text """ - # You can also provide just two exceptions - - two_entries_provided = True - - if len(pairs) != 2: - two_entries_provided = False - else: - a, b = pairs - if isinstance(b, (tuple, list)): - two_entries_provided = False - elif not ((b is None) or (issubclass(b, BaseException))): - two_entries_provided = False - - if two_entries_provided: - self.mapping = [(a, b)] - else: - self.mapping = list(pairs) - + try: + a, b = pairs # throws ValueError + op = issubclass(b, BaseException) # throws TypeError + except TypeError: + op = b is None + except ValueError: + op = False + + if op: + pairs = [pairs] + + self.mapping = list(pairs) self.exception_preprocessor = kwargs.get('exception_preprocessor', repr) def __call__(self, fun): diff --git a/tests/test_coding/test_rethrow.py b/tests/test_coding/test_rethrow.py index 46ee7af4cd6b74a0b202b25a42928eee9c556e5a..daa5e4641892663d6a29559b4295b5043a553c50 100644 --- a/tests/test_coding/test_rethrow.py +++ b/tests/test_coding/test_rethrow.py @@ -56,7 +56,7 @@ class TestStuff(unittest.TestCase): def test_issue_14a(self): - @rethrow_as(((NameError, ValueError), TypeError)) + @rethrow_as(*(((NameError, ValueError), TypeError), )) def ro(p): raise p() @@ -66,7 +66,7 @@ class TestStuff(unittest.TestCase): def test_issue_14b(self): - @rethrow_as((((NameError, ValueError), TypeError), )) + @rethrow_as((NameError, ValueError), TypeError) def ro(p): raise p()