Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 1 | __all__ = () |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 2 | |
jhaydaman | 0a28c0d | 2018-05-30 02:15:06 -0500 | [diff] [blame] | 3 | import concurrent.futures |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 4 | import reprlib |
| 5 | |
Andrew Svetlov | f74ef45 | 2017-12-15 07:04:38 +0200 | [diff] [blame] | 6 | from . import format_helpers |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 7 | |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 8 | CancelledError = concurrent.futures.CancelledError |
| 9 | TimeoutError = concurrent.futures.TimeoutError |
jhaydaman | 0a28c0d | 2018-05-30 02:15:06 -0500 | [diff] [blame] | 10 | InvalidStateError = concurrent.futures.InvalidStateError |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 11 | |
| 12 | |
| 13 | # States for Future. |
| 14 | _PENDING = 'PENDING' |
| 15 | _CANCELLED = 'CANCELLED' |
| 16 | _FINISHED = 'FINISHED' |
| 17 | |
| 18 | |
| 19 | def isfuture(obj): |
| 20 | """Check for a Future. |
| 21 | |
| 22 | This returns True when obj is a Future instance or is advertising |
| 23 | itself as duck-type compatible by setting _asyncio_future_blocking. |
| 24 | See comment in Future for more details. |
| 25 | """ |
Yury Selivanov | 6130c02 | 2016-11-07 16:07:30 -0500 | [diff] [blame] | 26 | return (hasattr(obj.__class__, '_asyncio_future_blocking') and |
| 27 | obj._asyncio_future_blocking is not None) |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 28 | |
| 29 | |
| 30 | def _format_callbacks(cb): |
| 31 | """helper function for Future.__repr__""" |
| 32 | size = len(cb) |
| 33 | if not size: |
| 34 | cb = '' |
| 35 | |
| 36 | def format_cb(callback): |
Andrew Svetlov | f74ef45 | 2017-12-15 07:04:38 +0200 | [diff] [blame] | 37 | return format_helpers._format_callback_source(callback, ()) |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 38 | |
| 39 | if size == 1: |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 40 | cb = format_cb(cb[0][0]) |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 41 | elif size == 2: |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 42 | cb = '{}, {}'.format(format_cb(cb[0][0]), format_cb(cb[1][0])) |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 43 | elif size > 2: |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 44 | cb = '{}, <{} more>, {}'.format(format_cb(cb[0][0]), |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 45 | size - 2, |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 46 | format_cb(cb[-1][0])) |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 47 | return f'cb=[{cb}]' |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 48 | |
| 49 | |
| 50 | def _future_repr_info(future): |
| 51 | # (Future) -> str |
| 52 | """helper function for Future.__repr__""" |
| 53 | info = [future._state.lower()] |
| 54 | if future._state == _FINISHED: |
| 55 | if future._exception is not None: |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 56 | info.append(f'exception={future._exception!r}') |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 57 | else: |
| 58 | # use reprlib to limit the length of the output, especially |
| 59 | # for very long strings |
| 60 | result = reprlib.repr(future._result) |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 61 | info.append(f'result={result}') |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 62 | if future._callbacks: |
| 63 | info.append(_format_callbacks(future._callbacks)) |
| 64 | if future._source_traceback: |
| 65 | frame = future._source_traceback[-1] |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 66 | info.append(f'created at {frame[0]}:{frame[1]}') |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 67 | return info |