diff options
Diffstat (limited to 'Lib/asyncio/futures.py')
-rw-r--r-- | Lib/asyncio/futures.py | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py index a3cf379ee81..889f3e6eb86 100644 --- a/Lib/asyncio/futures.py +++ b/Lib/asyncio/futures.py @@ -51,6 +51,7 @@ class Future: _exception = None _loop = None _source_traceback = None + _cancel_message = None # This field is used for a dual purpose: # - Its presence is a marker to declare that a class implements @@ -123,7 +124,7 @@ class Future: raise RuntimeError("Future object is not initialized.") return loop - def cancel(self): + def cancel(self, msg=None): """Cancel the future and schedule callbacks. If the future is already done or cancelled, return False. Otherwise, @@ -134,6 +135,7 @@ class Future: if self._state != _PENDING: return False self._state = _CANCELLED + self._cancel_message = msg self.__schedule_callbacks() return True @@ -173,7 +175,9 @@ class Future: the future is done and has an exception set, this exception is raised. """ if self._state == _CANCELLED: - raise exceptions.CancelledError + raise exceptions.CancelledError( + '' if self._cancel_message is None else self._cancel_message) + if self._state != _FINISHED: raise exceptions.InvalidStateError('Result is not ready.') self.__log_traceback = False @@ -190,7 +194,8 @@ class Future: InvalidStateError. """ if self._state == _CANCELLED: - raise exceptions.CancelledError + raise exceptions.CancelledError( + '' if self._cancel_message is None else self._cancel_message) if self._state != _FINISHED: raise exceptions.InvalidStateError('Exception is not set.') self.__log_traceback = False |