|
| 1 | +# Copied from asyncio 3.5.2 |
| 2 | + |
| 3 | +cdef _set_concurrent_future_state(concurrent, source): |
| 4 | + """Copy state from a future to a concurrent.futures.Future.""" |
| 5 | + assert source.done() |
| 6 | + if source.cancelled(): |
| 7 | + concurrent.cancel() |
| 8 | + if not concurrent.set_running_or_notify_cancel(): |
| 9 | + return |
| 10 | + exception = source.exception() |
| 11 | + if exception is not None: |
| 12 | + concurrent.set_exception(exception) |
| 13 | + else: |
| 14 | + result = source.result() |
| 15 | + concurrent.set_result(result) |
| 16 | + |
| 17 | + |
| 18 | +cdef _copy_future_state(source, dest): |
| 19 | + """Internal helper to copy state from another Future. |
| 20 | +
|
| 21 | + The other Future may be a concurrent.futures.Future. |
| 22 | + """ |
| 23 | + assert source.done() |
| 24 | + if dest.cancelled(): |
| 25 | + return |
| 26 | + assert not dest.done() |
| 27 | + if source.cancelled(): |
| 28 | + dest.cancel() |
| 29 | + else: |
| 30 | + exception = source.exception() |
| 31 | + if exception is not None: |
| 32 | + dest.set_exception(exception) |
| 33 | + else: |
| 34 | + result = source.result() |
| 35 | + dest.set_result(result) |
| 36 | + |
| 37 | + |
| 38 | +cdef _chain_future(source, destination): |
| 39 | + """Chain two futures so that when one completes, so does the other. |
| 40 | +
|
| 41 | + The result (or exception) of source will be copied to destination. |
| 42 | + If destination is cancelled, source gets cancelled too. |
| 43 | + Compatible with both asyncio.Future and concurrent.futures.Future. |
| 44 | + """ |
| 45 | + if not isinstance(source, (aio_Future, cc_Future)): |
| 46 | + raise TypeError('A future is required for source argument') |
| 47 | + if not isinstance(destination, (aio_Future, cc_Future)): |
| 48 | + raise TypeError('A future is required for destination argument') |
| 49 | + |
| 50 | + source_loop = None |
| 51 | + dest_loop = None |
| 52 | + |
| 53 | + source_type = type(source) |
| 54 | + dest_type = type(destination) |
| 55 | + |
| 56 | + if source_type is uvloop_Future: |
| 57 | + source_loop = (<BaseFuture>source)._loop |
| 58 | + elif source_type is not cc_Future and \ |
| 59 | + isinstance(source, aio_Future): |
| 60 | + source_loop = source._loop |
| 61 | + |
| 62 | + if dest_type is uvloop_Future: |
| 63 | + dest_loop = (<BaseFuture>destination)._loop |
| 64 | + elif dest_type is not cc_Future and \ |
| 65 | + isinstance(destination, aio_Future): |
| 66 | + dest_loop = destination._loop |
| 67 | + |
| 68 | + def _set_state(future, other): |
| 69 | + if isinstance(future, aio_Future): |
| 70 | + _copy_future_state(other, future) |
| 71 | + else: |
| 72 | + _set_concurrent_future_state(future, other) |
| 73 | + |
| 74 | + def _call_check_cancel(destination): |
| 75 | + if destination.cancelled(): |
| 76 | + if source_loop is None or source_loop is dest_loop: |
| 77 | + source.cancel() |
| 78 | + else: |
| 79 | + source_loop.call_soon_threadsafe(source.cancel) |
| 80 | + |
| 81 | + def _call_set_state(source): |
| 82 | + if dest_loop is None or dest_loop is source_loop: |
| 83 | + _set_state(destination, source) |
| 84 | + else: |
| 85 | + dest_loop.call_soon_threadsafe(_set_state, destination, source) |
| 86 | + |
| 87 | + destination.add_done_callback(_call_check_cancel) |
| 88 | + source.add_done_callback(_call_set_state) |
0 commit comments