1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Abstract classes."""
from __future__ import absolute_import, unicode_literals
import abc
from .five import with_metaclass, Callable
__all__ = ['Thenable']
@with_metaclass(abc.ABCMeta)
class Thenable(Callable): # pragma: no cover
"""Object that supports ``.then()``."""
__slots__ = ()
@abc.abstractmethod
def then(self, on_success, on_error=None):
raise NotImplementedError()
@abc.abstractmethod
def throw(self, exc=None, tb=None, propagate=True):
raise NotImplementedError()
@abc.abstractmethod
def cancel(self):
raise NotImplementedError()
@classmethod
def __subclasshook__(cls, C):
if cls is Thenable:
if any('then' in B.__dict__ for B in C.__mro__):
return True
return NotImplemented
@classmethod
def register(cls, other):
# overide to return other so `register` can be used as a decorator
type(cls).register(cls, other)
return other
@Thenable.register
class ThenableProxy(object):
"""Proxy to object that supports ``.then()``."""
def _set_promise_target(self, p):
self._p = p
def then(self, on_success, on_error=None):
return self._p.then(on_success, on_error)
def cancel(self):
return self._p.cancel()
def throw1(self, exc=None):
return self._p.throw1(exc)
def throw(self, exc=None, tb=None, propagate=True):
return self._p.throw(exc, tb=tb, propagate=propagate)
@property
def cancelled(self):
return self._p.cancelled
@property
def ready(self):
return self._p.ready
@property
def failed(self):
return self._p.failed