o
    US`cX                     @   sV   d dl Z d dlZddlmZ d dlmZ dd Ze jdddG d	d
 d
eedZdS )    N   )HalfCloseableStream)Finalc                    sF   t  }|  |  I dH  W d   dS 1 sw   Y  dS )a  Close an async resource or async generator immediately, without
    blocking to do any graceful cleanup.

    :class:`~trio.abc.AsyncResource` objects guarantee that if their
    :meth:`~trio.abc.AsyncResource.aclose` method is cancelled, then they will
    still close the resource (albeit in a potentially ungraceful
    fashion). :func:`aclose_forcefully` is a convenience function that
    exploits this behavior to let you force a resource to be closed without
    blocking: it works by calling ``await resource.aclose()`` and then
    cancelling it immediately.

    Most users won't need this, but it may be useful on cleanup paths where
    you can't afford to block, or if you want to close a resource and don't
    care about handling it gracefully. For example, if
    :class:`~trio.SSLStream` encounters an error and cannot perform its
    own graceful close, then there's no point in waiting to gracefully shut
    down the underlying transport either, so it calls ``await
    aclose_forcefully(self.transport_stream)``.

    Note that this function is async, and that it acts as a checkpoint, but
    unlike most async functions it cannot block indefinitely (at least,
    assuming the underlying resource object is correctly implemented).

    N)trioZCancelScopecancelaclose)resourcecs r
   B/usr/local/lib/python3.10/dist-packages/trio/_highlevel_generic.pyaclose_forcefully	   s
   
"r   F)eqhashc                   @   sJ   e Zd ZdZe Ze Zdd Zdd Z	dd Z
dd	d
Zdd ZdS )StapledStreama  This class `staples <https://en.wikipedia.org/wiki/Staple_(fastener)>`__
    together two unidirectional streams to make single bidirectional stream.

    Args:
      send_stream (~trio.abc.SendStream): The stream to use for sending.
      receive_stream (~trio.abc.ReceiveStream): The stream to use for
          receiving.

    Example:

       A silly way to make a stream that echoes back whatever you write to
       it::

          left, right = trio.testing.memory_stream_pair()
          echo_stream = StapledStream(SocketStream(left), SocketStream(right))
          await echo_stream.send_all(b"x")
          assert await echo_stream.receive_some() == b"x"

    :class:`StapledStream` objects implement the methods in the
    :class:`~trio.abc.HalfCloseableStream` interface. They also have two
    additional public attributes:

    .. attribute:: send_stream

       The underlying :class:`~trio.abc.SendStream`. :meth:`send_all` and
       :meth:`wait_send_all_might_not_block` are delegated to this object.

    .. attribute:: receive_stream

       The underlying :class:`~trio.abc.ReceiveStream`. :meth:`receive_some`
       is delegated to this object.

    c                       | j |I dH S )z$Calls ``self.send_stream.send_all``.N)send_streamsend_all)selfdatar
   r
   r   r   N      zStapledStream.send_allc                    s   | j  I dH S )z9Calls ``self.send_stream.wait_send_all_might_not_block``.N)r   wait_send_all_might_not_blockr   r
   r
   r   r   R   s   z+StapledStream.wait_send_all_might_not_blockc                    s.   t | jdr| j I dH S | j I dH S )zShuts down the send side of the stream.

        If ``self.send_stream.send_eof`` exists, then calls it. Otherwise,
        calls ``self.send_stream.aclose()``.

        send_eofN)hasattrr   r   r   r   r
   r
   r   r   V   s   zStapledStream.send_eofNc                    r   )z+Calls ``self.receive_stream.receive_some``.N)receive_streamreceive_some)r   Z	max_bytesr
   r
   r   r   b   r   zStapledStream.receive_somec                    s<   z| j  I dH  W | j I dH  dS | j I dH  w )z,Calls ``aclose`` on both underlying streams.N)r   r   r   r   r
   r
   r   r   f   s   &zStapledStream.aclose)N)__name__
__module____qualname____doc__attribr   r   r   r   r   r   r   r
   r
   r
   r   r   '   s    "
r   )	metaclass)	r    r   abcr   Z
trio._utilr   r   sr   r
   r
   r
   r   <module>   s    