def _cancellableInlineCallbacks(g):
    """
    Make an C{@}L{inlineCallbacks} cancellable.

    @param g: a generator object returned by calling a function or method
        decorated with C{@}L{inlineCallbacks}

    @return: L{Deferred} for the C{@}L{inlineCallbacks} that is cancellable.
    """
    def cancel(it):
        it.callbacks, tmp = [], it.callbacks
        it.addErrback(handleCancel)
        it.callbacks.extend(tmp)
        it.errback(_InternalInlineCallbacksCancelledError())
    deferred = Deferred(cancel)
    status = _CancellationStatus(deferred)
    def handleCancel(result):
        """
        Propagate the cancellation of an C{@}L{inlineCallbacks} to the
        L{Deferred} it is waiting on.

        @param result: An L{_InternalInlineCallbacksCancelledError} from
            C{cancel()}.
        @return: A new L{Deferred} that the C{@}L{inlineCallback} generator
            can callback or errback through.
        """
        result.trap(_InternalInlineCallbacksCancelledError)
        status.deferred = Deferred(cancel)
        # We would only end up here if the inlineCallback is waiting on
        # another Deferred.  It needs to be cancelled.
        awaited = status.waitingOn
        awaited.cancel()
        return status.deferred
    _inlineCallbacks(None, g, status)
    return deferred

    Make an C{@}L{inlineCallbacks} cancellable.

    @param g: a generator object returned by calling a function or method
        decorated with C{@}L{inlineCallbacks}

    @return: L{Deferred} for the C{@}L{inlineCallbacks} that is cancellable.
    
stackoverflow/venv/lib/python3.6/site-packages/twisted/internet/defer.py