From 202cadd867f72839c88d0ca4dca8ede8de0960e6 Mon Sep 17 00:00:00 2001 From: Piotr Maslanka <piotr.maslanka@henrietta.com.pl> Date: Sat, 17 Feb 2018 00:45:56 +0100 Subject: [PATCH] clearer --- satella/coding/recast_exceptions.py | 30 ++++++++++++----------------- tests/test_coding/test_rethrow.py | 4 ++-- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/satella/coding/recast_exceptions.py b/satella/coding/recast_exceptions.py index 4c98b00f..c7e46e75 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 46ee7af4..daa5e464 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() -- GitLab