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 | |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 3 | import reprlib |
Andrew Svetlov | 42d873c | 2020-11-10 15:58:31 +0200 | [diff] [blame^] | 4 | from _thread import get_ident |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 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 | # States for Future. |
| 9 | _PENDING = 'PENDING' |
| 10 | _CANCELLED = 'CANCELLED' |
| 11 | _FINISHED = 'FINISHED' |
| 12 | |
| 13 | |
| 14 | def isfuture(obj): |
| 15 | """Check for a Future. |
| 16 | |
| 17 | This returns True when obj is a Future instance or is advertising |
| 18 | itself as duck-type compatible by setting _asyncio_future_blocking. |
| 19 | See comment in Future for more details. |
| 20 | """ |
Yury Selivanov | 6130c02 | 2016-11-07 16:07:30 -0500 | [diff] [blame] | 21 | return (hasattr(obj.__class__, '_asyncio_future_blocking') and |
| 22 | obj._asyncio_future_blocking is not None) |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 23 | |
| 24 | |
| 25 | def _format_callbacks(cb): |
| 26 | """helper function for Future.__repr__""" |
| 27 | size = len(cb) |
| 28 | if not size: |
| 29 | cb = '' |
| 30 | |
| 31 | def format_cb(callback): |
Andrew Svetlov | f74ef45 | 2017-12-15 07:04:38 +0200 | [diff] [blame] | 32 | return format_helpers._format_callback_source(callback, ()) |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 33 | |
| 34 | if size == 1: |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 35 | cb = format_cb(cb[0][0]) |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 36 | elif size == 2: |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 37 | cb = '{}, {}'.format(format_cb(cb[0][0]), format_cb(cb[1][0])) |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 38 | elif size > 2: |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 39 | cb = '{}, <{} more>, {}'.format(format_cb(cb[0][0]), |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 40 | size - 2, |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 41 | format_cb(cb[-1][0])) |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 42 | return f'cb=[{cb}]' |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 43 | |
| 44 | |
Andrew Svetlov | 42d873c | 2020-11-10 15:58:31 +0200 | [diff] [blame^] | 45 | # bpo-42183: _repr_running is needed for repr protection |
| 46 | # when a Future or Task result contains itself directly or indirectly. |
| 47 | # The logic is borrowed from @reprlib.recursive_repr decorator. |
| 48 | # Unfortunately, the direct decorator usage is impossible because of |
| 49 | # AttributeError: '_asyncio.Task' object has no attribute '__module__' error. |
| 50 | # |
| 51 | # After fixing this thing we can return to the decorator based approach. |
| 52 | _repr_running = set() |
| 53 | |
| 54 | |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 55 | def _future_repr_info(future): |
| 56 | # (Future) -> str |
| 57 | """helper function for Future.__repr__""" |
| 58 | info = [future._state.lower()] |
| 59 | if future._state == _FINISHED: |
| 60 | if future._exception is not None: |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 61 | info.append(f'exception={future._exception!r}') |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 62 | else: |
Andrew Svetlov | 42d873c | 2020-11-10 15:58:31 +0200 | [diff] [blame^] | 63 | key = id(future), get_ident() |
| 64 | if key in _repr_running: |
| 65 | result = '...' |
| 66 | else: |
| 67 | _repr_running.add(key) |
| 68 | try: |
| 69 | # use reprlib to limit the length of the output, especially |
| 70 | # for very long strings |
| 71 | result = reprlib.repr(future._result) |
| 72 | finally: |
| 73 | _repr_running.discard(key) |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 74 | info.append(f'result={result}') |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 75 | if future._callbacks: |
| 76 | info.append(_format_callbacks(future._callbacks)) |
| 77 | if future._source_traceback: |
| 78 | frame = future._source_traceback[-1] |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 79 | info.append(f'created at {frame[0]}:{frame[1]}') |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 80 | return info |