(arg)
return outer
Wait for a future, shielding it from cancellation.

    The statement

        res = await shield(something())

    is exactly equivalent to the statement

        res = await something()

    *except* that if the coroutine containing it is cancelled, the
    task running in something() is not cancelled.  From the POV of
    something(), the cancellation did not happen.  But its caller is
    still cancelled, so the yield-from expression still raises
    CancelledError.  Note: If something() is cancelled by other means
    this will still cancel shield().

    If you want to completely ignore cancellation (not recommended)
    you can combine shield() with a try/except clause, as follows:

        try:
            res = await shield(something())
        except CancelledError:
            res = None
Wait for a future, shielding it from cancellation.
Wait for a future shielding it from cancellation .
def shield(arg):
    """Wait for a future, shielding it from cancellation.

    The statement

        res = await shield(something())

    is exactly equivalent to the statement

        res = await something()

    *except* that if the coroutine containing it is cancelled, the
    task running in something() is not cancelled.  From the POV of
    something(), the cancellation did not happen.  But its caller is
    still cancelled, so the yield-from expression still raises
    CancelledError.  Note: If something() is cancelled by other means
    this will still cancel shield().

    If you want to completely ignore cancellation (not recommended)
    you can combine shield() with a try/except clause, as follows:

        try:
            res = await shield(something())
        except CancelledError:
            res = None
    """
    inner = _ensure_future(arg)
    if inner.done():
        # Shortcut.
        return inner
    loop = futures._get_loop(inner)
    outer = loop.create_future()

    def _inner_done_callback(inner):
        if outer.cancelled():
            if not inner.cancelled():
                # Mark inner's result as retrieved.
                inner.exception()
            return

        if inner.cancelled():
            outer.cancel()
        else:
            exc = inner.exception()
            if exc is not None:
                outer.set_exception(exc)
            else:
                outer.set_result(inner.result())


    def _outer_done_callback(outer):
        if not inner.done():
            inner.remove_done_callback(_inner_done_callback)

    inner.add_done_callback(_inner_done_callback)
    outer.add_done_callback(_outer_done_callback)
    return outer
def shield ( arg ) : inner = _ensure_future ( arg ) if inner . done ( ) : # Shortcut. return inner loop = futures . _get_loop ( inner ) outer = loop . create_future ( ) def _inner_done_callback ( inner ) : if outer . cancelled ( ) : if not inner . cancelled ( ) : # Mark inner's result as retrieved. inner . exception ( ) return if inner . cancelled ( ) : outer . cancel ( ) else : exc = inner . exception ( ) if exc is not None : outer . set_exception ( exc ) else : outer . set_result ( inner . result ( ) ) def _outer_done_callback ( outer ) : if not inner . done ( ) : inner . remove_done_callback ( _inner_done_callback ) inner . add_done_callback ( _inner_done_callback ) outer . add_done_callback ( _outer_done_callback ) return outer