o
    4@HhY                     @  s\  U d dl mZ d dlZd dlZd dlmZmZmZ d dlm	Z	m
Z
 d dlmZmZmZ ddl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 erad dlmZ d dlmZm Z  edZ!ej"dk rld dl#m$Z$ ededZ%ededZ&eg ee'e%e&f  f Z(de)d< G dd dee%e&f Z*e		d0d1dd Z+d2d&d'Z,d3d*d+Z-d4d.d/Z.dS )5    )annotationsN)	AwaitableCallable	Generator)contextmanagersuppress)TYPE_CHECKINGGenericTypeVar   )CancelScope_core)AsyncResourceHalfCloseableStreamReceiveStream
SendStreamStream)aclose_forcefully   )assert_checkpoints)TracebackType)	ParamSpec	TypeAliasArgsT)      )BaseExceptionGroupRes1)boundRes2r   StreamMakerc                   @  s*   e Zd ZdddZdddZdddZdS )_ForceCloseBothbothtuple[Res1, Res2]returnNonec                 C  s   |\| _ | _d S N_first_second)selfr"    r+   [/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/trio/testing/_check_streams.py__init__#   s   z_ForceCloseBoth.__init__c                   s   | j | jfS r&   r'   )r*   r+   r+   r,   
__aenter__&   s   z_ForceCloseBoth.__aenter__exc_typetype[BaseException] | None	exc_valueBaseException | None	tracebackTracebackType | Nonec                   s<   zt | jI d H  W t | jI d H  d S t | jI d H  w r&   )r   r(   r)   )r*   r/   r1   r3   r+   r+   r,   	__aexit__)   s   &z_ForceCloseBoth.__aexit__N)r"   r#   r$   r%   )r$   r#   )r/   r0   r1   r2   r3   r4   r$   r%   )__name__
__module____qualname__r-   r.   r5   r+   r+   r+   r,   r!   "   s    

r!   Fexpected_exctype[BaseException]wrappedboolr$   Generator[None, None, None]c              
   c  s    d}zd V  W n; t y4 } z |sJ dt|jdksJ t|jd | s)J W Y d }~d S d }~w | yC   |r@J dY d S w td|  )NTz:caught exceptiongroup, but expected an unwrapped exceptionr   r   z0caught exception, but expected an exceptiongroupzexpected exception: )r   len
exceptions
isinstanceAssertionError)r9   r;   __tracebackhide__excr+   r+   r,   _assert_raises7   s   
"rD   stream_maker&StreamMaker[SendStream, ReceiveStream]clogged_stream_maker-StreamMaker[SendStream, ReceiveStream] | Noner%   c                   s   t |  I dH 4 I dH \ttsJ ttsJ dGfdddHdIfdddJfdd dKddt 4 I dH }|d | d W d  I dH  n1 I dH s_w   Y  dLfdd}t 4 I dH }|| | d W d  I dH  n1 I dH sw   Y  t 4 I dH }|td | d W d  I dH  n1 I dH sw   Y  t 4 I dH }|td | d W d  I dH  n1 I dH sw   Y  t	t
 dI dH  W d   n	1 sw   Y  t	t
 dI dH  W d   n	1 s!w   Y  t	t dI dH  W d   n	1 s>w   Y  t 4 I dH }|d  I dH dks]J W d  I dH  n1 I dH snw   Y  t 4 I dH }|d dI dH dksJ W d  I dH  n1 I dH sw   Y  t	tjdd3 t 4 I dH }|d  |d  W d  I dH  n1 I dH sw   Y  W d   n	1 sw   Y  dMfd#d$}t 4 I dH }|||j |d  W d  I dH  n1 I dH sw   Y  dLfd%d&}t 4 I dH }|| | W d  I dH  n1 I dH sFw   Y  t	tj d'I dH  W d   n	1 scw   Y  t	tj d(I dH  W d   n	1 sw   Y  I dH  I dH  I dH  t	tj d'I dH  W d   n	1 sw   Y  t	tj d)I dH  W d   n	1 sw   Y  t	tj# t   I dH  W d   n	1 sw   Y  W d   n	1 s w   Y  I dH  I dH  W d  I dH  n1 I dH s$w   Y  t |  I dH 4 I dH H\dLfd*d+}dL fd,d-}t 4 I dH }|| || W d  I dH  n1 I dH snw   Y  W d  I dH  n1 I dH sw   Y  t |  I dH 4 I dH J\tI dH  t	tj 	 d'I dH  q1 sw   Y  t	tj d(I dH  W d   n	1 sw   Y  W d  I dH  n1 I dH sw   Y  t |  I dH 4 I dH N\tI dH  t	tj d.I dH  W d   n	1 sw   Y  ttj  d)I dH  W d   n	1 s6w   Y  W d  I dH  n1 I dH sLw   Y  t |  I dH 4 I dH \t }|   I dH  W d   n	1 s{w   Y  t }|   I dH  W d   n	1 sw   Y  t	tj d.I dH  W d   n	1 sw   Y  t	tj d(I dH  W d   n	1 sw   Y  W d  I dH  n1 I dH sw   Y  t |  I dH 4 I dH \dNd5d6}	t 9}|  t 4 I dH }||	d ||	d  W d  I dH  n1 I dH s5w   Y  W d   n	1 sEw   Y  t 4 I dH }| | W d  I dH  n1 I dH spw   Y  W d  I dH  n1 I dH sw   Y  t |  I dH 4 I dH F\dLfd7d8}
t 4 I dH }||
 t I dH  tI dH  W d  I dH  n1 I dH sw   Y  W d  I dH  n1 I dH sw   Y  |durt | I dH 4 I dH Z\g dOfd:d;}dLfd<d=}t 4 I dH }|||j t I dH  || W d  I dH  n1 I dH s<w   Y  g d>ksJJ W d  I dH  n1 I dH s[w   Y  t | I dH 4 I dH \t	tjdd3 t 4 I dH }|j |j W d  I dH  n1 I dH sw   Y  W d   n	1 sw   Y  t	tjdd4 t 4 I dH }|j |jd. W d  I dH  n1 I dH sw   Y  W d   n	1 sw   Y  W d  I dH  n1 I dH sw   Y  t | I dH 4 I dH R\t	tjdd5 t 4 I dH }|jd. |jd. W d  I dH  n1 I dH sKw   Y  W d   n	1 s[w   Y  W d  I dH  n1 I dH sqw   Y  t | I dH 4 I dH D\dLfd?d@}dLfdAd=}t 4 I dH }|| || W d  I dH  n1 I dH sw   Y  W d  I dH  n1 I dH sw   Y  t | I dH 4 I dH =\tI dH  zt   I dH  W d   n	1 sw   Y  W n tjy   Y nw W d  I dH  n1 I dH s"w   Y  dPdDdE}t | I dH 4 I dH P\t 4 I dH /}|| t	tj dFI dH  W d   n	1 scw   Y  W d  I dH  n1 I dH syw   Y  W d  I dH  n1 I dH sw   Y  t | I dH 4 I dH P\t 4 I dH .}|| t	tj  I dH  W d   n	1 sw   Y  W d  I dH  n1 I dH sw   Y  W d  I dH  dS 1 I dH sw   Y  dS dS )Qa  Perform a number of generic tests on a custom one-way stream
    implementation.

    Args:
      stream_maker: An async (!) function which returns a connected
          (:class:`~trio.abc.SendStream`, :class:`~trio.abc.ReceiveStream`)
          pair.
      clogged_stream_maker: Either None, or an async function similar to
          stream_maker, but with the extra property that the returned stream
          is in a state where ``send_all`` and
          ``wait_send_all_might_not_block`` will block until ``receive_some``
          has been called. This allows for more thorough testing of some edge
          cases, especially around ``wait_send_all_might_not_block``.

    Raises:
      AssertionError: if a test fails.

    Ndatabytes | bytearray | memoryviewr$   r%   c                   sF   t    | I d H d u sJ W d    d S 1 sw   Y  d S r&   )r   send_all)rI   sr+   r,   do_send_alld   s   "z)check_one_way_stream.<locals>.do_send_all	max_bytes
int | Nonebytes | bytearrayc                   s<   t    | I d H W  d    S 1 sw   Y  d S r&   )r   receive_some)rO   rr+   r,   do_receive_someh   s   $z-check_one_way_stream.<locals>.do_receive_someexpectedbytesc                   s    dI d H | ksJ d S Nr   r+   )rV   )rU   r+   r,   checked_receive_1l   s   z/check_one_way_stream.<locals>.checked_receive_1resourcer   c                   s<   t   |  I d H  W d    d S 1 sw   Y  d S r&   )r   aclose)rZ   r+   r+   r,   	do_acloseo   s   "z'check_one_way_stream.<locals>.do_aclose   xc                     s"    dI d H   dI d H  d S )N       yr+   r+   rN   r+   r,   send_empty_then_yx   s   z/check_one_way_stream.<locals>.send_empty_then_yr_      1   2r   g      ?Tr;   r   scoper   c                   sB   t     I d H  W d    n1 sw   Y  |   d S r&   )r   wait_send_all_might_not_blockcancel)rf   rL   r+   r,   *simple_check_wait_send_all_might_not_block   s
   zHcheck_one_way_stream.<locals>.simple_check_wait_send_all_might_not_blockc                     s4   t tj
 	  dI d H  q1 sw   Y  d S )NTd   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)rD   r   BrokenResourceErrorr+   r`   r+   r,   expect_broken_stream_on_send   s   z:check_one_way_stream.<locals>.expect_broken_stream_on_sendrj   i   r^   c                     s"   dI d H   I d H  d S )Nr_   r+   r+   )r\   rN   rM   r+   r,   send_then_close      z-check_one_way_stream.<locals>.send_then_closec                     s>   t  I d H   dI d H   dI d H  I d H  d S )Nr_   r^   )r   wait_all_tasks_blockedr+   )rY   r\   rT   r+   r,   receive_send_then_close   s
   z5check_one_way_stream.<locals>.receive_send_then_closes   123afn"Callable[ArgsT, Awaitable[object]]args
ArgsT.argskwargsArgsT.kwargsc                   sF   t tj | |i |I d H  W d    d S 1 sw   Y  d S r&   )rD   r   	Cancelled)rq   rs   ru   r+   r+   r,   expect_cancelled$  s   "z.check_one_way_stream.<locals>.expect_cancelledc                     sB   t tj  dI d H  W d    d S 1 sw   Y  d S )N
   )rD   r   ClosedResourceErrorrR   r+   rS   r+   r,   receive_expecting_closed:  s   "z6check_one_way_stream.<locals>.receive_expecting_closedcancel_scopec                   sV     d t   I d H  W d    n1 sw   Y    d |   d S )Nwaiter sleepingwaiter wokeup)appendr   rg   rh   )r|   )recordrM   r+   r,   waiterH  s   

z$check_one_way_stream.<locals>.waiterc                     s.   t  I d H  d 	  dI d H  q)Nreceiver startingTiA  )r   ro   r   rR   r+   )rT   r   r+   r,   receiverO  s   
z&check_one_way_stream.<locals>.receiver)r}   r   r~   c                     sX   zt     I d H  W d    W d S 1 sw   Y  W d S  tjy+   Y d S w r&   )r   rg   r   rk   r+   rL   r+   r,   sender}  s   &z$check_one_way_stream.<locals>.senderc                     s"   t  I d H  t I d H  d S r&   r   ro   r   r+   rS   r+   r,   r     rn   rM   r   c                   s"   t  I d H  t| I d H  d S r&   r   rL   r+   r+   r,   
close_soon  rn   z(check_one_way_stream.<locals>.close_soons   xyzzy)rI   rJ   r$   r%   r&   )rO   rP   r$   rQ   )rV   rW   r$   r%   )rZ   r   r$   r%   r$   r%   )rf   r   r$   r%   )rq   rr   rs   rt   ru   rv   r$   r%   )r|   r   r$   r%   )rM   r   r$   r%   )r!   r@   r   r   r   open_nursery
start_soon	bytearray
memoryviewrD   
ValueErrorrR   	TypeErrorBusyResourceErrorr|   rk   rz   r   rg   r   r   r   rh   r[   ro   rK   )rE   rG   nurseryra   ri   rl   rm   rp   rf   rx   r{   r   r   r   r   r+   )rY   r\   rU   rN   rT   r   rM   r,   check_one_way_streamJ   s   
(
(((


***
*	
**

,* *
 *

 *

*,*
,*
***	* ** *

,**

 ,* ,0r   StreamMaker[Stream, Stream]"StreamMaker[Stream, Stream] | Nonec              
     s  t I dH  dfdd}durdfdd}nd}t ||I dH  t I dH 4 I dH \}t|ts<J ttsCJ d}d	 td
}|d| }||d}d  fdd}	d! fdd}
t	 4 I dH 5}|
|	||d
 |
|	|ddd d |
|
||ddd d |
|
|d W d  I dH  n1 I dH sw   Y  d"fdd}t	 4 I dH }|
| |
|j W d  I dH  n1 I dH sw   Y  W d  I dH  dS 1 I dH sw   Y  dS )#u  Perform a number of generic tests on a custom two-way stream
    implementation.

    This is similar to :func:`check_one_way_stream`, except that the maker
    functions are expected to return objects implementing the
    :class:`~trio.abc.Stream` interface.

    This function tests a *superset* of what :func:`check_one_way_stream`
    checks – if you call this, then you don't need to also call
    :func:`check_one_way_stream`.

    Nr$   tuple[Stream, Stream]c                          I d H d d d S Nrd   r+   r+   )rE   r+   r,   flipped_stream_maker     z2check_two_way_stream.<locals>.flipped_stream_makerc                     r   r   r+   r+   )rG   r+   r,   flipped_clogged_stream_maker  r   z:check_two_way_stream.<locals>.flipped_clogged_stream_makeri   i @  r      littlerM   r   rI   rJ   seedintr%   c                   sT   t |}t|}|r(|d }| |d | I d H  ||d  }|sd S d S rX   )randomRandomr   randintrK   )rM   rI   r   rT   m
chunk_sizeCHUNK_SIZE_MAXr+   r,   r     s   
z$check_two_way_stream.<locals>.senderrQ   c                   sj   t |}t }t|t|k r-| |d I d H }|s!J ||7 }t|t|k s||ks3J d S rX   )r   r   r   r>   rR   r   )rM   rI   r   rT   gotchunkr   r+   r,   r     s   
z&check_two_way_stream.<locals>.receiverrd   r   r   r   c                     s,     dI d H dksJ   I d H  d S )Nry   r^   )rR   r[   r+   )s2r+   r,   expect_receive_some_empty  s   z7check_two_way_stream.<locals>.expect_receive_some_empty)r$   r   )rM   r   rI   rJ   r   r   r$   r%   )rM   r   rI   rQ   r   r   r$   r%   r   )r   r!   r@   r   r   r   getrandbitsto_bytesr   r   r   r[   )rE   rG   r   r   s1DUPLEX_TEST_SIZErT   i	test_datar   r   r   r   r+   )r   rG   r   rE   r,   check_two_way_stream  s:   
	(
*.r   5StreamMaker[HalfCloseableStream, HalfCloseableStream]<StreamMaker[HalfCloseableStream, HalfCloseableStream] | Nonec                   sr  t | |I dH  t|  I dH 4 I dH \}}t|tsJ t|ts&J ddd}dd	d
}t 4 I dH }||| ||| W d  I dH  n1 I dH sUw   Y  ttj |	dI dH  W d   n1 srw   Y  t
  | I dH  W d   n1 sw   Y  t 4 I dH }||| ||| W d  I dH  n1 I dH sw   Y  W d  I dH  n1 I dH sw   Y  |durt| I dH 4 I dH X\}}ttjdd; t 4 I dH }||j	d t I dH  ||j W d  I dH  n1 I dH sw   Y  W d   n	1 s*w   Y  W d  I dH  n1 I dH s@w   Y  t| I dH 4 I dH X\}}ttjdd: t 4 I dH }||j t I dH  ||j W d  I dH  n1 I dH sw   Y  W d   n	1 sw   Y  W d  I dH  dS 1 I dH sw   Y  dS dS )u  Perform a number of generic tests on a custom half-closeable stream
    implementation.

    This is similar to :func:`check_two_way_stream`, except that the maker
    functions are expected to return objects that implement the
    :class:`~trio.abc.HalfCloseableStream` interface.

    This function tests a *superset* of what :func:`check_two_way_stream`
    checks – if you call this, then you don't need to also call
    :func:`check_two_way_stream`.

    NrM   r   r$   r%   c                   sL   |  dI d H  t  |  I d H  W d    d S 1 sw   Y  d S )Nr]   )rK   r   send_eofrL   r+   r+   r,   send_x_then_eof  s
   "z4check_half_closeable_stream.<locals>.send_x_then_eofrT   c                   sD   t  I d H  | dI d H dksJ | dI d H dks J d S )Nry   r]   r^   )r   ro   rR   rS   r+   r+   r,   expect_x_then_eof  s   z6check_half_closeable_stream.<locals>.expect_x_then_eofr_   Tre   r]   )rM   r   r$   r%   )rT   r   r$   r%   )r   r!   r@   r   r   r   r   rD   rz   rK   r   r   r   ro   rg   )rE   rG   r   r   r   r   r   r+   r+   r,   check_half_closeable_stream  sX   

(*(
* ** 0r   )F)r9   r:   r;   r<   r$   r=   )rE   rF   rG   rH   r$   r%   )rE   r   rG   r   r$   r%   )rE   r   rG   r   r$   r%   )/
__future__r   r   syscollections.abcr   r   r   
contextlibr   r   typingr   r	   r
    r   r   _abcr   r   r   r   r   _highlevel_genericr   _checkpointsr   typesr   typing_extensionsr   r   r   version_infoexceptiongroupr   r   r   tupler    __annotations__r!   rD   r   r   r   r+   r+   r+   r,   <module>   s8   
 
  
`P