From 3f8e5f109949dc6acb5fecd91c5067968fe271ee Mon Sep 17 00:00:00 2001 From: nanoric Date: Wed, 10 Oct 2018 22:26:17 -0400 Subject: [PATCH] =?UTF-8?q?[Add]=20Promise=E5=A2=9E=E5=8A=A0with=20self.ca?= =?UTF-8?q?tch():=E8=AF=AD=E6=B3=95=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/Promise.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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)