Skip to content
Snippets Groups Projects
Commit d30c31a2 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

2.9.12

parent 5389ad42
No related branches found
Tags v2.9.12
No related merge requests found
......@@ -30,6 +30,8 @@ def silence_excs(*exc_types: ExcType, returns=None,
>>> def returns_5():
>>> raise KeyError()
>>> assert returns_5() == 5
:raises ValueError: you gave both returns and returns_factory. You can only pass one of them!
"""
return rethrow_as(exc_types, None, returns=returns,
returns_factory=returns_factory)
......@@ -143,6 +145,7 @@ class rethrow_as:
:param returns: what value should the function return if this is used as a decorator
:param returns_factory: a callable that returns the value this function should return is this
is used as as decorator
:raises ValueError: you specify both returns and returns_factory
"""
__slots__ = ('mapping', 'exception_preprocessor', 'returns', '__exception_remapped',
'returns_factory')
......@@ -167,6 +170,9 @@ class rethrow_as:
self.returns = returns
self.returns_factory = returns_factory
if self.returns is not None and self.returns_factory is not None:
raise ValueError('You can specify only one of (returns, returns_factory)')
# this is threading.local because two threads may execute the same function at the
# same time, and exceptions from one function would leak to another
self.__exception_remapped = threading.local()
......
......@@ -13,6 +13,15 @@ class TestStuff(unittest.TestCase):
self.assertFalse(raises_exception(NameError, lambda: None))
def test_silence_excs_returns_factory(self):
try:
@silence_excs(KeyError, returns=5, returns_factory=5)
def errors():
pass
except ValueError:
pass
else:
self.fail('ValueError not raised')
@silence_excs(KeyError, returns_factory=lambda: 5)
def key_error():
raise KeyError()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment