a
    QýIhY  ã                   @  sz  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rÂd dlmZ d dlmZm Z  edƒZ!ej"dk rØd 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	d-ddddœdd„ƒZ+dd d!d"œd#d$„Z,d%d&d!d"œd'd(„Z-d)d*d!d"œd+d,„Z.dS ).é    )Ú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œdd„Zddœdd„Zd	d
dddœdd„ZdS )Ú_ForceCloseBothztuple[Res1, Res2]ÚNone)ÚbothÚreturnc                 C  s   |\| _ | _d S ©N©Ú_firstÚ_second)Úselfr#   © r*   úU/var/www/html/venv_bot_3.9/lib/python3.9/site-packages/trio/testing/_check_streams.pyÚ__init__#   s    z_ForceCloseBoth.__init__©r$   c                 Ã  s   | j | jfS r%   r&   )r)   r*   r*   r+   Ú
__aenter__&   s    z_ForceCloseBoth.__aenter__ztype[BaseException] | NonezBaseException | NonezTracebackType | None)Úexc_typeÚ	exc_valueÚ	tracebackr$   c                 Ã  s<   z$t | jƒI d H  W t | jƒI d H  nt | jƒI d H  0 d S r%   )r   r'   r(   )r)   r/   r0   r1   r*   r*   r+   Ú	__aexit__)   s    z_ForceCloseBoth.__aexit__N)Ú__name__Ú
__module__Ú__qualname__r,   r.   r2   r*   r*   r*   r+   r!   "   s   r!   Fztype[BaseException]ÚboolzGenerator[None, None, None])Úexpected_excÚwrappedr$   c              
   c  s”   d}z
d V  W nr t yd } z>|s*J dƒ‚t|jƒdks<J ‚t|jd | ƒsPJ ‚W Y d }~n4d }~0  | y€   |r|J dƒ‚Y n0 td| › ƒ‚d S )NTz:caught exceptiongroup, but expected an unwrapped exceptionr   r   z0caught exception, but expected an exceptiongroupzexpected exception: )r   ÚlenÚ
exceptionsÚ
isinstanceÚAssertionError)r7   r8   Z__tracebackhide__Úexcr*   r*   r+   Ú_assert_raises7   s    
(r>   z&StreamMaker[SendStream, ReceiveStream]z-StreamMaker[SendStream, ReceiveStream] | Noner"   )Ústream_makerÚclogged_stream_makerr$   c                 ƒ  s¦  t | ƒ I dH ƒ4 I dH š\\‰‰tˆtƒs.J ‚tˆtƒs<J ‚dddœ‡fdd„‰dEddd	œ‡fd
d„‰dddœ‡fdd„‰ dddœdd„‰t ¡ 4 I dH š.}| ˆd¡ | ˆ d¡ W d  ƒI dH  qÞ1 I dH sÔ0    Y  ddœ‡fdd„}t ¡ 4 I dH š.}| |¡ | ˆ d¡ W d  ƒI dH  qF1 I dH s<0    Y  t ¡ 4 I dH š4}| ˆtdƒ¡ | ˆ d¡ W d  ƒI dH  q¢1 I dH s˜0    Y  t ¡ 4 I dH š4}| ˆtdƒ¡ | ˆ d¡ W d  ƒI dH  qþ1 I dH sô0    Y  t	t
ƒ  ˆ d¡I dH  W d  ƒ n1 s.0    Y  t	t
ƒ  ˆ d¡I dH  W d  ƒ n1 sh0    Y  t	tƒ  ˆ d¡I dH  W d  ƒ n1 s¢0    Y  t ¡ 4 I dH š:}| ˆd¡ ˆƒ I dH dksàJ ‚W d  ƒI dH  q1 I dH s0    Y  t ¡ 4 I dH š<}| ˆd¡ ˆdƒI dH dksDJ ‚W d  ƒI dH  qr1 I dH sh0    Y  t	tjddh t ¡ 4 I dH š0}| ˆd ¡ | ˆd ¡ W d  ƒI dH  qÚ1 I dH sÐ0    Y  W d  ƒ n1 sð0    Y  d!dd"œ‡fd#d$„}t ¡ 4 I dH š2}| ||j¡ | ˆd ¡ W d  ƒI dH  qh1 I dH s^0    Y  ddœ‡fd%d&„}t ¡ 4 I dH š.}| |¡ | ˆˆ¡ W d  ƒI dH  qÐ1 I dH sÆ0    Y  t	tjƒ ˆd'ƒI dH  W d  ƒ n1 s 0    Y  t	tjƒ ˆd(ƒI dH  W d  ƒ n1 s:0    Y  ˆˆƒI dH  ˆˆƒI dH  ˆˆƒI dH  t	tjƒ ˆd'ƒI dH  W d  ƒ n1 sž0    Y  t	tjƒ ˆd)ƒI dH  W d  ƒ n1 sØ0    Y  t	tjƒF tƒ  ˆ ¡ I dH  W d  ƒ n1 s0    Y  W d  ƒ n1 s:0    Y  ˆˆƒI dH  ˆˆƒI dH  W d  ƒI dH  qŽ1 I dH s„0    Y  t | ƒ I dH ƒ4 I dH šœ\‰‰ddœ‡‡‡fd*d+„}ddœ‡ ‡‡fd,d-„}t ¡ 4 I dH š,}| |¡ | |¡ W d  ƒI dH  q,1 I dH s"0    Y  W d  ƒI dH  qZ1 I dH sP0    Y  t | ƒ I dH ƒ4 I dH š¢\‰‰tˆƒI dH  t	tjƒ" ˆd'ƒI dH  q’W d  ƒ n1 sº0    Y  t	tjƒ ˆd(ƒI dH  W d  ƒ n1 sô0    Y  W d  ƒI dH  q,1 I dH s"0    Y  t | ƒ I dH ƒ4 I dH šž\‰‰tˆƒI dH  t	tjƒ ˆd.ƒI dH  W d  ƒ n1 sˆ0    Y  ttjƒ ˆ d)ƒI dH  W d  ƒ n1 sÂ0    Y  W d  ƒI dH  qú1 I dH sð0    Y  t | ƒ I dH ƒ4 I dH š\‰‰t ¡ &}| ¡  ˆ ¡ I dH  W d  ƒ n1 	sP0    Y  t ¡ &}| ¡  ˆ ¡ I dH  W d  ƒ n1 	s0    Y  t	tjƒ ˆd.ƒI dH  W d  ƒ n1 	sÊ0    Y  t	tjƒ ˆd(ƒI dH  W d  ƒ n1 
s0    Y  W d  ƒI dH  
q<1 I dH 
s20    Y  t | ƒ I dH ƒ4 I dH š\‰‰d/d0d1dd2œd3d4„}	t ¡ t}| ¡  t ¡ 4 I dH š4}| |	ˆd¡ | |	ˆd ¡ W d  ƒI dH  
qÞ1 I dH 
sÔ0    Y  W d  ƒ n1 
sô0    Y  t ¡ 4 I dH š0}| ˆˆ¡ | ˆˆ¡ W d  ƒI dH  qV1 I dH sL0    Y  W d  ƒI dH  q„1 I dH sz0    Y  t | ƒ I dH ƒ4 I dH š”\‰‰ddœ‡fd5d6„}
t ¡ 4 I dH š>}| |
¡ t ¡ I dH  tˆƒI dH  W d  ƒI dH  q1 I dH s0    Y  W d  ƒI dH  qH1 I dH s>0    Y  |dur¢t |ƒ I dH ƒ4 I dH šÂ\‰‰g ‰d!dd7œ‡‡fd8d9„}ddœ‡‡fd:d;„}t ¡ 4 I dH š>}| ||j¡ t ¡ I dH  | |¡ W d  ƒI dH  q1 I dH sú0    Y  ˆg d<¢ksJ ‚W d  ƒI dH  qD1 I dH s:0    Y  t |ƒ I dH ƒ4 I dH š.\‰‰t	tjddh t ¡ 4 I dH š0}| ˆj¡ | ˆj¡ W d  ƒI dH  qÌ1 I dH sÂ0    Y  W d  ƒ n1 sâ0    Y  t	tjddj t ¡ 4 I dH š2}| ˆj¡ | ˆjd.¡ W d  ƒI dH  qV1 I dH sL0    Y  W d  ƒ n1 sl0    Y  W d  ƒI dH  q¤1 I dH sš0    Y  t |ƒ I dH ƒ4 I dH š¨\‰‰t	tjddl t ¡ 4 I dH š4}| ˆjd.¡ | ˆjd.¡ W d  ƒI dH  q.1 I dH s$0    Y  W d  ƒ n1 sD0    Y  W d  ƒI dH  q|1 I dH sr0    Y  t |ƒ I dH ƒ4 I dH š”\‰‰ddœ‡fd=d>„}ddœ‡fd?d;„}t ¡ 4 I dH š,}| |¡ | |¡ W d  ƒI dH  q1 I dH s0    Y  W d  ƒI dH  q@1 I dH s60    Y  t |ƒ I dH ƒ4 I dH š|\‰‰tˆƒI dH  z:tƒ  ˆ ¡ I dH  W d  ƒ n1 sš0    Y  W n tjy¼   Y n0 W d  ƒI dH  qì1 I dH sâ0    Y  d@ddAœdBdC„}t |ƒ I dH ƒ4 I dH š¤\‰‰t ¡ 4 I dH š`}| |ˆ¡ t	tjƒ  ˆ dD¡I dH  W d  ƒ n1 sj0    Y  W d  ƒI dH  q¢1 I dH s˜0    Y  W d  ƒI dH  qÐ1 I dH sÆ0    Y  t |ƒ I dH ƒ4 I dH š¢\‰‰t ¡ 4 I dH š^}| |ˆ¡ t	tjƒ ˆ ¡ I dH  W d  ƒ n1 s<0    Y  W d  ƒI dH  qt1 I dH sj0    Y  W d  ƒI dH  q¢1 I dH s˜0    Y  dS )FaÜ  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úbytes | bytearray | memoryviewr"   )Údatar$   c                 “  sB   t ƒ ( ˆ  | ¡I d H d u s J ‚W d   ƒ n1 s40    Y  d S r%   )r   Úsend_all)rB   ©Úsr*   r+   Údo_send_alld   s    z)check_one_way_stream.<locals>.do_send_allz
int | Noneúbytes | bytearray)Ú	max_bytesr$   c                 “  s:   t ƒ   ˆ  | ¡I d H W  d   ƒ S 1 s,0    Y  d S r%   )r   Úreceive_some)rH   ©Úrr*   r+   Údo_receive_someh   s    z-check_one_way_stream.<locals>.do_receive_someÚbytes)Úexpectedr$   c                 “  s   ˆ dƒI d H | ksJ ‚d S ©Nr   r*   )rN   )rL   r*   r+   Úchecked_receive_1l   s    z/check_one_way_stream.<locals>.checked_receive_1r   )Úresourcer$   c                 Ó  s8   t ƒ  |  ¡ I d H  W d   ƒ n1 s*0    Y  d S r%   )r   Úaclose)rQ   r*   r*   r+   Ú	do_acloseo   s    z'check_one_way_stream.<locals>.do_acloseó   xr-   c                   “  s    ˆ dƒI d H  ˆ dƒI d H  d S )Nó    ó   yr*   r*   ©rF   r*   r+   Úsend_empty_then_yx   s    z/check_one_way_stream.<locals>.send_empty_then_yrV   ó   1ó   2éÿÿÿÿr   g      ø?T©r8   r   r   )Úscoper$   c                 “  s@   t ƒ  ˆ  ¡ I d H  W d   ƒ n1 s*0    Y  |  ¡  d S r%   )r   Úwait_send_all_might_not_blockÚcancel)r]   rD   r*   r+   Ú*simple_check_wait_send_all_might_not_block¦   s    ,zHcheck_one_way_stream.<locals>.simple_check_wait_send_all_might_not_blockc                   “  s>   t tjƒ  ˆ dƒI d H  qW d   ƒ n1 s00    Y  d S )Nód   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)r>   r   ÚBrokenResourceErrorr*   rW   r*   r+   Úexpect_broken_stream_on_send¶   s    z:check_one_way_stream.<locals>.expect_broken_stream_on_sendra   i   rU   c                   “  s    ˆdƒI d H  ˆ ˆƒI d H  d S )NrV   r*   r*   )rS   rF   rE   r*   r+   Úsend_then_closeá   s    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 )NrV   rU   )r   Úwait_all_tasks_blockedr*   )rP   rS   rK   r*   r+   Úreceive_send_then_closeå   s    z5check_one_way_stream.<locals>.receive_send_then_closes   123z"Callable[ArgsT, Awaitable[object]]z
ArgsT.argszArgsT.kwargs)ÚafnÚargsÚkwargsr$   c                 ß  sB   t tjƒ$ | |i |¤ŽI d H  W d   ƒ n1 s40    Y  d S r%   )r>   r   Z	Cancelled)rg   rh   ri   r*   r*   r+   Úexpect_cancelled$  s    z.check_one_way_stream.<locals>.expect_cancelledc                   “  s>   t tjƒ  ˆ  d¡I d H  W d   ƒ n1 s00    Y  d S )Né
   )r>   r   ÚClosedResourceErrorrI   r*   rJ   r*   r+   Úreceive_expecting_closed:  s    z6check_one_way_stream.<locals>.receive_expecting_closed)Úcancel_scoper$   c                 “  sT   ˆ   d¡ tƒ  ˆ ¡ I d H  W d   ƒ n1 s40    Y  ˆ   d¡ |  ¡  d S )Núwaiter sleepingúwaiter wokeup)Úappendr   r^   r_   )rn   )ÚrecordrE   r*   r+   ÚwaiterH  s
    
,
z$check_one_way_stream.<locals>.waiterc                   “  s.   t  ¡ I d H  ˆ d¡ ˆ  d¡I d H  qd S )Núreceiver startingiÂA  )r   re   rq   rI   r*   )rK   rr   r*   r+   ÚreceiverO  s    
z&check_one_way_stream.<locals>.receiver)ro   rt   rp   c                   “  sR   z8t ƒ  ˆ  ¡ I d H  W d   ƒ n1 s,0    Y  W n tjyL   Y n0 d S r%   )r   r^   r   rb   r*   rD   r*   r+   Úsender}  s
    0z$check_one_way_stream.<locals>.senderc                   “  s    t  ¡ I d H  tˆ ƒI d H  d S r%   ©r   re   r   r*   rJ   r*   r+   ru   „  s    r   ©rE   r$   c                 Ó  s    t  ¡ I d H  t| ƒI d H  d S r%   rw   rD   r*   r*   r+   Ú
close_soon—  s    z(check_one_way_stream.<locals>.close_soons   xyzzy)N)r!   r;   r   r   r   Úopen_nurseryÚ
start_soonÚ	bytearrayÚ
memoryviewr>   Ú
ValueErrorrI   Ú	TypeErrorÚBusyResourceErrorrn   rb   rl   r   r^   r   r   r   r_   rR   re   rC   )r?   r@   ÚnurseryrX   r`   rc   rd   rf   r]   rj   rm   rs   ru   rv   ry   r*   )rP   rS   rL   rF   rK   rr   rE   r+   Úcheck_one_way_streamJ   s8    6
:::
0
0
0DFZþ:
:....N<

f2\.\ 
.
..\ 
\h
j
8@ ZŠŠ
f24Œr‚   zStreamMaker[Stream, Stream]z"StreamMaker[Stream, Stream] | Nonec              
   ƒ  s  t ˆˆƒI dH  ddœ‡fdd„}ˆdur>ddœ‡fdd„}nd}t ||ƒI dH  tˆƒ I dH ƒ4 I dH š\}‰t|tƒs€J ‚tˆtƒsŽJ ‚d}d	‰ t d
¡}| d| ¡}| |d¡}dddddœ‡ fdd„}	dddddœ‡ fdd„}
t 	¡ 4 I dH šl}| 
|	||d
¡ | 
|	ˆ|ddd… d¡ | 
|
||ddd… d¡ | 
|
ˆ|d¡ W d  ƒI dH  q~1 I dH st0    Y  ddœ‡fdd„}t 	¡ 4 I dH š.}| 
|¡ | 
|j¡ W d  ƒI dH  qæ1 I dH sÜ0    Y  W d  ƒI dH  q1 I dH s
0    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`.

    Nztuple[Stream, Stream]r-   c                   “  s   ˆ ƒ I d H d d d… S ©Nr[   r*   r*   )r?   r*   r+   Úflipped_stream_makerº  s    z2check_two_way_stream.<locals>.flipped_stream_makerc                   “  s   ˆ ƒ I d H d d d… S rƒ   r*   r*   )r@   r*   r+   Úflipped_clogged_stream_makerÁ  s    z:check_two_way_stream.<locals>.flipped_clogged_stream_makeri   i @  r   é   Úlittler   rA   Úintr"   )rE   rB   Úseedr$   c                 “  sL   t  |¡}t|ƒ}|rH| dˆ ¡}|  |d |… ¡I d H  ||d … }qd S rO   )ÚrandomÚRandomr}   ÚrandintrC   )rE   rB   r‰   rK   ÚmÚ
chunk_size©ÚCHUNK_SIZE_MAXr*   r+   rv   Ô  s    
z$check_two_way_stream.<locals>.senderrG   c                 “  sZ   t  |¡}tƒ }t|ƒt|ƒk rJ|  | dˆ ¡¡I d H }|s@J ‚||7 }q||ksVJ ‚d S rO   )rŠ   r‹   r|   r9   rI   rŒ   )rE   rB   r‰   rK   ÚgotÚchunkr   r*   r+   ru   à  s    

z&check_two_way_stream.<locals>.receiverr[   r   r   r   c                   “  s*   ˆ   d¡I d H dksJ ‚ˆ  ¡ I d H  d S )Nrk   rU   )rI   rR   r*   )Ús2r*   r+   Úexpect_receive_some_emptyï  s    z7check_two_way_stream.<locals>.expect_receive_some_empty)r‚   r!   r;   r   rŠ   r‹   ÚgetrandbitsÚto_bytesr   rz   r{   rR   )r?   r@   r„   r…   Ús1ZDUPLEX_TEST_SIZErK   ÚiZ	test_datarv   ru   r   r”   r*   )r   r@   r“   r?   r+   Úcheck_two_way_stream¨  s2     
	>
r™   z5StreamMaker[HalfCloseableStream, HalfCloseableStream]z<StreamMaker[HalfCloseableStream, HalfCloseableStream] | Nonec                 Ã  sŠ  t | |ƒI dH  t| ƒ I dH ƒ4 I dH št\}}t|tƒs>J ‚t|tƒsLJ ‚dddœdd„}dddœdd	„}t ¡ 4 I dH š.}| ||¡ | ||¡ W d  ƒI dH  qÀ1 I dH s¶0    Y  ttjƒ  | 	d
¡I dH  W d  ƒ n1 sð0    Y  t
ƒ  | ¡ I dH  W d  ƒ n1 s&0    Y  t ¡ 4 I dH š0}| ||¡ | ||¡ W d  ƒI dH  qˆ1 I dH s~0    Y  W d  ƒI dH  q¶1 I dH s¬0    Y  |dur†t|ƒ I dH ƒ4 I dH š´\}}ttjddx t ¡ 4 I dH š@}| |j	d¡ t ¡ I dH  | |j¡ W d  ƒI dH  qV1 I dH sL0    Y  W d  ƒ n1 sl0    Y  W d  ƒI dH  q¤1 I dH sš0    Y  t|ƒ I dH ƒ4 I dH š²\}}ttjddv t ¡ 4 I dH š>}| |j¡ t ¡ I dH  | |j¡ W d  ƒI dH  q81 I dH s.0    Y  W d  ƒ n1 sN0    Y  W d  ƒI dH  q†1 I dH s|0    Y  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`.

    Nr   r"   rx   c                 Ó  sH   |   d¡I d H  tƒ  |  ¡ I d H  W d   ƒ n1 s:0    Y  d S )NrT   )rC   r   Úsend_eofrD   r*   r*   r+   Úsend_x_then_eof  s    z4check_half_closeable_stream.<locals>.send_x_then_eof)rK   r$   c                 Ó  sB   t  ¡ I d H  |  d¡I d H dks&J ‚|  d¡I d H dks>J ‚d S )Nrk   rT   rU   )r   re   rI   rJ   r*   r*   r+   Úexpect_x_then_eof  s    z6check_half_closeable_stream.<locals>.expect_x_then_eofrV   Tr\   rT   )r™   r!   r;   r   r   rz   r{   r>   rl   rC   r   rš   r€   re   r^   )r?   r@   r—   r“   r›   rœ   r   r*   r*   r+   Úcheck_half_closeable_streamø  s:     6..h
ˆr   )F)/Ú
__future__r   rŠ   ÚsysÚcollections.abcr   r   r   Ú
contextlibr   r   Útypingr   r	   r
   Ú r   r   Ú_abcr   r   r   r   r   Z_highlevel_genericr   Z_checkpointsr   Útypesr   Ztyping_extensionsr   r   r   Úversion_infoZexceptiongroupr   r   r   Útupler    Ú__annotations__r!   r>   r‚   r™   r   r*   r*   r*   r+   Ú<module>   s6   
  þ  `P