1 | n/a | __all__ = [] |
---|
2 | n/a | |
---|
3 | n/a | import concurrent.futures._base |
---|
4 | n/a | import reprlib |
---|
5 | n/a | |
---|
6 | n/a | from . import events |
---|
7 | n/a | |
---|
8 | n/a | Error = concurrent.futures._base.Error |
---|
9 | n/a | CancelledError = concurrent.futures.CancelledError |
---|
10 | n/a | TimeoutError = concurrent.futures.TimeoutError |
---|
11 | n/a | |
---|
12 | n/a | |
---|
13 | n/a | class InvalidStateError(Error): |
---|
14 | n/a | """The operation is not allowed in this state.""" |
---|
15 | n/a | |
---|
16 | n/a | |
---|
17 | n/a | # States for Future. |
---|
18 | n/a | _PENDING = 'PENDING' |
---|
19 | n/a | _CANCELLED = 'CANCELLED' |
---|
20 | n/a | _FINISHED = 'FINISHED' |
---|
21 | n/a | |
---|
22 | n/a | |
---|
23 | n/a | def isfuture(obj): |
---|
24 | n/a | """Check for a Future. |
---|
25 | n/a | |
---|
26 | n/a | This returns True when obj is a Future instance or is advertising |
---|
27 | n/a | itself as duck-type compatible by setting _asyncio_future_blocking. |
---|
28 | n/a | See comment in Future for more details. |
---|
29 | n/a | """ |
---|
30 | n/a | return (hasattr(obj.__class__, '_asyncio_future_blocking') and |
---|
31 | n/a | obj._asyncio_future_blocking is not None) |
---|
32 | n/a | |
---|
33 | n/a | |
---|
34 | n/a | def _format_callbacks(cb): |
---|
35 | n/a | """helper function for Future.__repr__""" |
---|
36 | n/a | size = len(cb) |
---|
37 | n/a | if not size: |
---|
38 | n/a | cb = '' |
---|
39 | n/a | |
---|
40 | n/a | def format_cb(callback): |
---|
41 | n/a | return events._format_callback_source(callback, ()) |
---|
42 | n/a | |
---|
43 | n/a | if size == 1: |
---|
44 | n/a | cb = format_cb(cb[0]) |
---|
45 | n/a | elif size == 2: |
---|
46 | n/a | cb = '{}, {}'.format(format_cb(cb[0]), format_cb(cb[1])) |
---|
47 | n/a | elif size > 2: |
---|
48 | n/a | cb = '{}, <{} more>, {}'.format(format_cb(cb[0]), |
---|
49 | n/a | size - 2, |
---|
50 | n/a | format_cb(cb[-1])) |
---|
51 | n/a | return 'cb=[%s]' % cb |
---|
52 | n/a | |
---|
53 | n/a | |
---|
54 | n/a | def _future_repr_info(future): |
---|
55 | n/a | # (Future) -> str |
---|
56 | n/a | """helper function for Future.__repr__""" |
---|
57 | n/a | info = [future._state.lower()] |
---|
58 | n/a | if future._state == _FINISHED: |
---|
59 | n/a | if future._exception is not None: |
---|
60 | n/a | info.append('exception={!r}'.format(future._exception)) |
---|
61 | n/a | else: |
---|
62 | n/a | # use reprlib to limit the length of the output, especially |
---|
63 | n/a | # for very long strings |
---|
64 | n/a | result = reprlib.repr(future._result) |
---|
65 | n/a | info.append('result={}'.format(result)) |
---|
66 | n/a | if future._callbacks: |
---|
67 | n/a | info.append(_format_callbacks(future._callbacks)) |
---|
68 | n/a | if future._source_traceback: |
---|
69 | n/a | frame = future._source_traceback[-1] |
---|
70 | n/a | info.append('created at %s:%s' % (frame[0], frame[1])) |
---|
71 | n/a | return info |
---|