diff --git a/tests/Promise.py b/tests/Promise.py index d5fe4dfe..2e37d7f0 100644 --- a/tests/Promise.py +++ b/tests/Promise.py @@ -8,6 +8,7 @@ class PromiseResultType(Enum): Result = 1 Exception = 2 Traceback = 3 + class Promise(object): @@ -36,3 +37,23 @@ class Promise(object): self._queue.put((PromiseResultType.Exception, valueOrType)) else: self._queue.put((PromiseResultType.Traceback, (valueOrType, val, tb))) + + def catch(self): + """ + Usage : + with promise.catch(): + raise Exception(); + """ + return _PromiseCatchContext(self) + + +class _PromiseCatchContext(object): + + def __init__(self, promise): + self.promise = promise + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.promise.set_exception(exc_type, exc_val, exc_tb)