
    +h:$                       d dl mZ d dlmZmZ d dlmZmZ d dlm	Z	 d dl
mZmZmZ ddlmZmZmZmZmZ ddlmZ dd	lmZmZmZ dd
lmZ  ed      Z edd      Z edd      Z G d de      Z ed       G d dee                Z  ed       G d dee   ee                Z! ed       G d dee   ee                Z"y)    )annotations)OrderedDictdeque)	dataclassfield)TracebackType)Generic
NamedTupleTypeVar   )BrokenResourceErrorClosedResourceErrorEndOfStream
WouldBlockget_cancelled_exc_class)DeprecatedAwaitable)EventObjectReceiveStreamObjectSendStream)
checkpointT_ItemT_coT)	covariantT_contra)contravariantc                  J    e Zd ZU ded<   ded<   ded<   ded<   ded<   ded<   y	)
MemoryObjectStreamStatisticsintcurrent_buffer_usedfloatmax_buffer_sizeopen_send_streamsopen_receive_streamstasks_waiting_sendtasks_waiting_receiveN)__name__
__module____qualname____annotations__     G/var/www/html/venv/lib/python3.12/site-packages/anyio/streams/memory.pyr   r      s&    r+   r   F)eqc                      e Zd ZU  e       Zded<    ede      Zded<    edd      Zd	ed
<    edd      Z	d	ed<    ede
      Zded<    ede
      Zded<   ddZy)MemoryObjectStreamStater    r!   F)initdefault_factoryzdeque[T_Item]bufferr   r0   defaultr   open_send_channelsopen_receive_channelsz OrderedDict[Event, list[T_Item]]waiting_receiverszOrderedDict[Event, T_Item]waiting_sendersc           
         t        t        | j                        | j                  | j                  | j
                  t        | j                        t        | j                              S N)r   lenr2   r!   r5   r6   r8   r7   selfs    r,   
statisticsz"MemoryObjectStreamState.statistics0   sR    +  ##&&$$%&&'
 	
r+   Nreturnr   )r&   r'   r(   r   r!   r)   r   r2   r5   r6   r   r7   r8   r>   r*   r+   r,   r/   r/   #   sy    "WOU$!ueDFMD#::!&E1!=3=:?K;7  38K3O/ 
r+   r/   c                      e Zd ZU ded<    edd      Zded<   ddZddZdd	Zdd
Z	ddZ
ddZddZddZ	 	 	 	 	 	 	 	 ddZy)MemoryObjectReceiveStreamzMemoryObjectStreamState[T_co]_stateFr3   bool_closedc                B    | j                   xj                  dz  c_        y N   )rC   r6   r<   s    r,   __post_init__z'MemoryObjectReceiveStream.__post_init__@   s    ))Q.)r+   c                   | j                   rt        | j                  j                  r^| j                  j                  j	                  d      \  }}| j                  j
                  j                  |       |j                          | j                  j
                  r$| j                  j
                  j                         S | j                  j                  st        t        )a  
        Receive the next item if it can be done without waiting.

        :return: the received item
        :raises ~anyio.ClosedResourceError: if this send stream has been closed
        :raises ~anyio.EndOfStream: if the buffer is empty and this stream has been
            closed from the sending end
        :raises ~anyio.WouldBlock: if there are no items in the buffer and no tasks
            waiting to send

        Flast)rE   r   rC   r8   popitemr2   appendsetpopleftr5   r   r   )r=   
send_eventitems      r,   receive_nowaitz(MemoryObjectReceiveStream.receive_nowaitC   s     <<%%;;&&#{{::BBBNJKK%%d+NN;;;;%%--////r+   c                  K   t                d {    	 | j                         S 7 # t        $ r t               }g }|| j                  j
                  |<   	 |j                          d {  7   n# t               $ r |s Y nw xY w| j                  j
                  j                  |d        n+# | j                  j
                  j                  |d        w xY w|r|d   cY S t        w xY ww)Nr   )
r   rS   r   r   rC   r7   waitr   popr   )r=   receive_event	containers      r,   receivez!MemoryObjectReceiveStream.receive_   s     l	"&&(( 	  	"!GM$&I;DDKK))-8G#((****,  ! ! --11-F--11-F |#!!'	"sp   C,&C,( C,.C)A1*A-+A10B.1BB.BB.'C).(C
C) C,"C))C,c                R    | j                   rt        t        | j                        S )a  
        Create a clone of this receive stream.

        Each clone can be closed separately. Only when all clones have been closed will the
        receiving end of the memory stream be considered closed by the sending ends.

        :return: the cloned stream

        rC   )rE   r   rB   rC   r<   s    r,   clonezMemoryObjectReceiveStream.clonex   s      <<%%(<<r+   c                &   | j                   sd| _         | j                  xj                  dz  c_        | j                  j                  dk(  rEt        | j                  j                  j                               }|D ]  }|j                           yyyz
        Close the stream.

        This works the exact same way as :meth:`aclose`, but is provided as a special case for the
        benefit of synchronous callbacks.

        TrH   r   N)rE   rC   r6   listr8   keysrO   )r=   send_eventsevents      r,   closezMemoryObjectReceiveStream.close   sx     ||DLKK--2-{{00A5"4;;#>#>#C#C#EF(  EIIK  6 r+   c                ,   K   | j                          y wr:   rc   r<   s    r,   aclosez MemoryObjectReceiveStream.aclose        

   c                6    | j                   j                         S zj
        Return statistics about the current state of this stream.

        .. versionadded:: 3.0
        rC   r>   r<   s    r,   r>   z$MemoryObjectReceiveStream.statistics        {{%%''r+   c                    | S r:   r*   r<   s    r,   	__enter__z#MemoryObjectReceiveStream.__enter__       r+   c                $    | j                          y r:   re   r=   exc_typeexc_valexc_tbs       r,   __exit__z"MemoryObjectReceiveStream.__exit__        	

r+   Nr@   None)r@   r   )r@   zMemoryObjectReceiveStream[T_co]r?   rr   ztype[BaseException] | Noners   zBaseException | Nonert   zTracebackType | Noner@   rx   )r&   r'   r(   r)   r   rE   rI   rS   rY   r\   rc   rf   r>   rn   ru   r*   r+   r,   rB   rB   ;   sl    ))ue4GT4/8"2=  (, & %	
 
r+   rB   c                      e Zd ZU ded<    edd      Zded<   ddZddZdd	Zdd
Z	ddZ
ddZddZddZ	 	 	 	 	 	 	 	 ddZy)MemoryObjectSendStreamz!MemoryObjectStreamState[T_contra]rC   Fr3   rD   rE   c                B    | j                   xj                  dz  c_        y rG   )rC   r5   r<   s    r,   rI   z$MemoryObjectSendStream.__post_init__   s    &&!+&r+   c                   | j                   rt        | j                  j                  st        | j                  j
                  rK| j                  j
                  j                  d      \  }}|j                  |       |j                          nbt        | j                  j                        | j                  j                  k  r&| j                  j                  j                  |       nt        t        | j                        S )a  
        Send an item immediately if it can be done without waiting.

        :param item: the item to send
        :raises ~anyio.ClosedResourceError: if this send stream has been closed
        :raises ~anyio.BrokenResourceError: if the stream has been closed from the
            receiving end
        :raises ~anyio.WouldBlock: if the buffer is full and there are no tasks waiting
            to receive

        FrK   )rE   r   rC   r6   r   r7   rM   rN   rO   r;   r2   r!   r   r   send_nowait)r=   rR   rW   rX   s       r,   r~   z"MemoryObjectSendStream.send_nowait   s     <<%%{{00%%;;(('+{{'D'D'L'LRW'L'X$M9T"##$t{{'B'BBKK%%d+"4#3#344r+   c                  K   t                d {    	 | j                  |       y 7 # t        $ r t               }|| j                  j
                  |<   	 |j                          d {  7   n4# t        $ r( | j                  j
                  j                  |d         w xY w| j                  j
                  j                  |d       rt        Y y w xY wwr:   )
r   r~   r   r   rC   r8   rU   BaseExceptionrV   r   )r=   rR   rQ   s      r,   sendzMemoryObjectSendStream.send   s     l	*T" 	  	*J6:DKK''
3 oo'''  ++//
DA {{**..z4@)) A	*sR   C(C* C,CA1*A-+A10C11B""/CCCCc                R    | j                   rt        t        | j                        S )a  
        Create a clone of this send stream.

        Each clone can be closed separately. Only when all clones have been closed will the
        sending end of the memory stream be considered closed by the receiving ends.

        :return: the cloned stream

        r[   )rE   r   r{   rC   r<   s    r,   r\   zMemoryObjectSendStream.clone   s      <<%%%T[[99r+   c                n   | j                   sd| _         | j                  xj                  dz  c_        | j                  j                  dk(  rit        | j                  j                  j                               }| j                  j                  j                          |D ]  }|j                           yyyr^   )rE   rC   r5   r_   r7   r`   clearrO   )r=   receive_eventsrb   s      r,   rc   zMemoryObjectSendStream.close   s     ||DLKK**a/*{{--2!%dkk&C&C&H&H&J!K--335+  EIIK  3 r+   c                ,   K   | j                          y wr:   re   r<   s    r,   rf   zMemoryObjectSendStream.aclose  rg   rh   c                6    | j                   j                         S rj   rk   r<   s    r,   r>   z!MemoryObjectSendStream.statistics  rl   r+   c                    | S r:   r*   r<   s    r,   rn   z MemoryObjectSendStream.__enter__  ro   r+   c                $    | j                          y r:   re   rq   s       r,   ru   zMemoryObjectSendStream.__exit__  rv   r+   Nrw   )rR   r   r@   r   )rR   r   r@   rx   )r@   z MemoryObjectSendStream[T_contra]r?   ry   )r&   r'   r(   r)   r   rE   rI   r~   r   r\   rc   rf   r>   rn   ru   r*   r+   r,   r{   r{      sl    --ue4GT4,58*": "(, & %	
 
r+   r{   N)#
__future__r   collectionsr   r   dataclassesr   r   typesr   typingr	   r
   r    r   r   r   r   r   _core._compatr   abcr   r   r   lowlevelr   r   r   r   r   r/   rB   r{   r*   r+   r,   <module>r      s    " * (  / /  0 > > !		v&:T2:  e
gfo 
 
. eo/B4/H o od ehWX.0@0J h hr+   