a
    QIh                     @  s  U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
mZ d dlmZmZmZmZ d dlZd dlZddlmZmZmZmZmZmZmZmZ ddlmZ ddlmZ dd	l m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' dd
l(m)Z) erd dl*m+Z+m,Z,m-Z- d dl.m/Z/ ddl0m1Z1 e2e3e4eej5e6e7 f f  Z8edZ9ddddZ:ddddZ;ddddZ<ddddZ=ddddZ>dodddddZ?ddd d!Z@ddd"d#ZAddd$d%ZBddd&d'ZCd(d)dd*d+d,ZDejEFd-g d.ejEFd/d0d1gejEFd2d0d1gd3d4d4dd5d6d7ZGddd8d9ZHddd:d;ZId)dd<d=d>ZJddd?d@ZKdddAdBZLeMdCZNdDeOdC< dddEdFZPdddGdHZQdddIdJZRdddKdLZSdddMdNZTdddOdPZUdddQdRZVdddSdTZWdddUdVZXdddWdXZYdddYdZZZddd[d\Z[ddd]d^Z\ddd_d`Z]dddadbZ^dddcddZ_dddedfZ`dddgdhZaedddidjZbdddkdlZcdddmdnZddS )p    )annotationsNpartial)TYPE_CHECKINGNoReturnTypeVarUnion   )CancelScopeCapacityLimiterEvent_core
fail_aftermove_on_aftersleepsleep_forever)ki_self)slow)active_thread_countcurrent_default_thread_limiterfrom_thread_check_cancelledfrom_thread_runfrom_thread_run_syncto_thread_run_syncwait_all_threads_completed)wait_all_tasks_blocked)AsyncGenerator	AwaitableCallable)Outcome)TaskTNonereturnc                    s   t   ddddddd fdd} t }d	d
ddd}| t|d|dI d H  d	dddd}| t|dtf|dI d H  d	d
ddd}| t|d|dI d H  d	dddd}| t|dtf|dI d H  d S )NzCallable[..., threading.Thread]zCallable[..., T | Awaitable[T]]ztuple[str, T]_core.TrioToken | Noner"   )do_in_trio_threadfnexpected
trio_tokenr$   c                   sp   g dd fdd}t j|dd}|  | rRtd tdI d H  q2d	|fd
f|gkslJ d S )Nr"   r#   c               
     sv   z2 dt f  d}  d| f W n> typ } z&t|  dt|f W Y d }~n
d }~0 0 d S )Nstartr)   goterror)append	threadingcurrent_threadBaseExceptionprinttype)xexcr&   r'   recordr)    R/var/www/html/venv_bot_3.9/lib/python3.9/site-packages/trio/_tests/test_threads.pythreadfnB   s    z<test_do_in_trio_thread.<locals>.check_case.<locals>.threadfnT)targetdaemonZyawn{Gz?r*   f)r/   Threadr*   is_aliver2   r   )r&   r'   r(   r)   r:   child_threadtrio_threadr6   r9   
check_case:   s    	z*test_do_in_trio_thread.<locals>.check_case
RecordTypeint)r7   r$   c                 S  s"   t  rJ | dt f dS )Nr>   r	   )r   currently_ki_protectedr.   r/   r0   r7   r8   r8   r9   f1T   s    z"test_do_in_trio_thread.<locals>.f1)r,   r	   r+   r   c                 S  s&   t  rJ | dt f td S Nr>   )r   rG   r.   r/   r0   
ValueErrorrH   r8   r8   r9   f2[   s    z"test_do_in_trio_thread.<locals>.f2r-   c                   s0   t  rJ t  I d H  | dt f dS )Nr>      )r   rG   
checkpointr.   r/   r0   rH   r8   r8   r9   f3b   s    z"test_do_in_trio_thread.<locals>.f3)r,   rM   c                   s4   t  rJ t  I d H  | dt f td S rJ   )r   rG   rN   r.   r/   r0   KeyErrorrH   r8   r8   r9   f4j   s    z"test_do_in_trio_thread.<locals>.f4)N)r/   r0   r   current_trio_tokenr   rK   r   rP   )rD   tokenrI   rL   rO   rQ   r8   rB   r9   test_do_in_trio_thread7   s     rT   c                    sz   t t tdd  W d    n1 s,0    Y  dddd} t t t|  W d    n1 sl0    Y  d S )Nc                   S  s   d S Nr8   r8   r8   r8   r9   <lambda>u       z9test_do_in_trio_thread_from_trio_thread.<locals>.<lambda>r"   r#   c                     s   d S rU   r8   r8   r8   r8   r9   foow   s    z4test_do_in_trio_thread_from_trio_thread.<locals>.foo)pytestraisesRuntimeErrorr   r   )rX   r8   r8   r9   'test_do_in_trio_thread_from_trio_threads   s
    *r\   c                    s6   t   dd fdd} t|   ddhks2J d S )Nr"   r#   c                    s   t   ddddddfdddd fdd} tj| d	}|  td
 | rttdI d H  q\td |  td d S )Nr"   r#   c               
   S  sZ   t d t rJ t d z t  W dd l} t d|   ndd l} t d|   0 d S )Nzin Trio threadr   r   finally)r2   r   rG   r   sysexc_info)r^   r8   r8   r9   trio_thread_fn   s    zTtest_run_in_trio_thread_ki.<locals>.check_run_in_trio_thread.<locals>.trio_thread_fnc                     s
      d S rU   r8   r8   )r`   r8   r9   trio_thread_afn   s    zUtest_run_in_trio_thread_ki.<locals>.check_run_in_trio_thread.<locals>.trio_thread_afnc                     sx   zt d td W n$ ty<   t d  d Y n0 ztd W n$ tyr   t d  d Y n0 d S )Nrunningr+   ok1ok2)r2   r   KeyboardInterruptaddr   r8   )r7   rS   ra   r`   r8   r9   external_thread_fn   s    zXtest_run_in_trio_thread_ki.<locals>.check_run_in_trio_thread.<locals>.external_thread_fn)r;   Zwaitingr=   zwaited, joiningdone)	r   rR   r/   r?   r*   r2   r@   r   join)rg   threadrH   )rS   ra   r`   r9   check_run_in_trio_thread   s    z<test_run_in_trio_thread_ki.<locals>.check_run_in_trio_threadrc   rd   )setr   run)rk   r8   rH   r9   test_run_in_trio_thread_ki~   s    '
rn   c                    sp   g t   dd fdddddfddd	d fd
d} t| }|  ddgkslJ d S )Nr"   r#   c                     s*    d    tdd I d H  d S )Nsleepingc                 S  s   t jjS rU   )r   ZAbortZ	SUCCEEDED_r8   r8   r9   rV      rW   zMtest_await_in_trio_thread_while_main_exits.<locals>.trio_fn.<locals>.<lambda>)r.   rl   r   Zwait_task_rescheduledr8   )evr7   r8   r9   trio_fn   s    
z;test_await_in_trio_thread_while_main_exits.<locals>.trio_fn_core.TrioTokenrS   r$   c                   s4   zt | d W n tjy.    d Y n0 d S )Nr+   	cancelled)r   r   	Cancelledr.   rS   )r7   rs   r8   r9   	thread_fn   s    z=test_await_in_trio_thread_while_main_exits.<locals>.thread_fnthreading.Threadc                    s@   t  } tj| fd}|    I d H  dgks<J |S )N)r;   argsro   )r   rR   r/   r?   r*   wait)rS   rj   )rr   r7   ry   r8   r9   main   s    z8test_await_in_trio_thread_while_main_exits.<locals>.mainro   rv   )r   r   rm   ri   )r}   rj   r8   )rr   r7   ry   rs   r9   *test_await_in_trio_thread_while_main_exits   s    
r~   c                    s   d} d|  fddddddddfd	d
 t I d H  t d dI d H  t  d|  I d H  ddd fdd}|dI d H  |dI d H  |dI d H  |dI d H  d S )Nz0 from trio._tests.test_threads.test_named_threadinnerstrrz   namer$   c                 S  s   t  j| ksJ t  S rU   )r/   r0   r   r   r8   r8   r9   r      s    z test_named_thread.<locals>.innerCallable[[], threading.Thread]c                   s
   t  | S rU   r   r   r   r8   r9   r>      s    ztest_named_thread.<locals>.fZthread_namer"   c                   s,   t  | | dI d H }td|js(J d S )Nr   Trio thread [0-9]*)r   rematchr   )r   rj   r>   r8   r9   test_thread_name   s    z+test_named_thread.<locals>.test_thread_name fobiedooname_longer_than_15_characters   💙r   )endingr   r8   r>   r   r9   test_named_thread   s    r   z
int | None
str | None)identr$   c              
   C  s  dd l }dd l}|jd}|s$d}z||}W n$ tyV   tdtj  Y d S 0 t	|dd }|slJ |j
|j|jg|_|j|_|jddd}| d u rt } || |ddksJ z|j W S  ty } z$td	|jd
|  W Y d }~n
d }~0 0 d S )Nr   Zpthreadzlibc.sozno pthread on pthread_getname_nprW      )sizezvalue: z, exception: )ctypesZctypes.utilutilZfind_libraryCDLL	Exceptionr2   r^   platformgetattrZc_void_pc_char_pZc_size_targtypesc_intrestypeZcreate_string_bufferr/   	get_identvaluedecodeUnicodeDecodeErrorrY   Zfail)r   r   Zlibpthread_pathZ
libpthreadr   Zname_bufferer8   r8   r9   _get_thread_name   s2    r   c                    s   ddddddddfdd d	} t  | I d H  t  | d d
I d H  ddddd fdd}|dI d H  |dI d H  |dI d H  |dddI d H  d S )Nr   rz   r   c                 S  sH   t  }|d u r,tjdkr,tdtj  n|| d d ks@J t S )Nlinuxzno pthread OS support on    )r   r^   r   rY   skipr/   r0   )r   os_thread_namer8   r8   r9   r     s
    z#test_named_thread_os.<locals>.innerr   c                   s
   t  | S rU   r   r   r   r8   r9   r>     s    ztest_named_thread_os.<locals>.fz4None from trio._tests.test_threads.test_named_threadr   r   r"   )r   r(   r$   c                   sP   |d u r| }t  || dI d H }t|j}|d us<J dtd|sLJ d S )Nr   z'should skip earlier if this is the caser   )r   r   r   r   r   )r   r(   rj   r   r   r8   r9   r   (  s    
z.test_named_thread_os.<locals>.test_thread_namer   r   r   r   ?)r(   )Nr   )defaultr   r8   r   r9   test_named_thread_os  s    		r   c                  C  s>   ddl m}  |  }|d u r:tjdks(J tdtj  d S )Nr   )get_os_thread_name_funcr   zno pthread_setname_np on )Ztrio._core._thread_cacher   r^   r   rY   r   )r   kr8   r8   r9   test_has_pthread_setname_np8  s
    r   c                    s   t  } ddddd}t|dI d H \}}|dks8J || ksDJ ddd	d
}tjtdd}t|I d H  W d    n1 s0    Y  t|jj |jjd | ksJ d S )Nr!   ztuple[T, threading.Thread])r4   r$   c                 S  s   | t  fS rU   )r/   r0   )r4   r8   r8   r9   r>   D  s    z$test_run_in_worker_thread.<locals>.f   r   r#   c                   S  s   t t d S rU   )rK   r/   r0   r8   r8   r8   r9   gK  s    z$test_run_in_worker_thread.<locals>.gz1^<Thread\(Trio thread \d+, started daemon \d+\)>$r   r   )	r/   r0   r   rY   rZ   rK   r2   r   r{   )rC   r>   r4   rA   r   excinfor8   r8   r9   test_run_in_worker_threadA  s    ,r   c               	     s  d gdddfdd dddd fdd	} g t  }t 4 I d H H}|| |d
 t I d H  dgksxJ |j  W d   I d H  q1 I d H s0    Y  d dksJ |d  d dkrt	
d qg d d< t 4 I d H }|| |d t I d H  |j  tjd
d. tdD ]}t I d H  q6W d    n1 sb0    Y  dgks|J |d  W d   I d H  q1 I d H s0    Y  t (}|  | |dI d H  W d    n1 s0    Y  |jsJ d S )Nzstdlib_queue.Queue[None]r"   )qr$   c                   s   d d< |    d d< d S )Nblockingr   finished)getr   )registerr8   r9   r>   Z  s    z1test_run_in_worker_thread_cancellation.<locals>.fbool)r   abandon_on_cancelr$   c              	     s<    d z t | |dI d H W  d S  d 0 d S )Nr*   r   exit)r.   r   )r   r   r>   r7   r8   r9   child`  s    

z5test_run_in_worker_thread_cancellation.<locals>.childTr*   r   r   r=   Fshield
   )stdlib_queueQueuer   open_nursery
start_soonr   cancel_scopecancelputtimer   r
   rangerN   cancelled_caught)r   r   nurseryrq   scoper8   )r>   r7   r   r9   &test_run_in_worker_thread_cancellationW  s:    4

28
0r   zpytest.CaptureFixture[str]zpytest.MonkeyPatch)capfdmonkeypatchr$   c                   s   | tjdd t  t dd fddddfdd}t|  d   }| rvt	
d qb|  \}}d	|vsJ d	|vsJ d S )
NZIDLE_TIMEOUTr=   r"   r#   c                     s       t  d S rU   )r   r   r/   r0   r8   )q1q2r8   r9   ry     s    z6test_run_in_worker_thread_abandoned.<locals>.thread_fnc               	     sr   dd fdd} t  4 I d H 6}||  t I d H  |j  W d   I d H  qn1 I d H sd0    Y  d S )Nr"   r#   c                     s   t  ddI d H  d S )NTr   r   r8   ry   r8   r9   r     s    z@test_run_in_worker_thread_abandoned.<locals>.main.<locals>.child)r   r   r   r   r   r   )r   r   r   r8   r9   r}     s
    
z1test_run_in_worker_thread_abandoned.<locals>.mainzException in thread)setattrr   _thread_cacher   r   rm   r   r   r@   r   r   Z
readouterr)r   r   r}   rj   outerrr8   )r   r   ry   r9   #test_run_in_worker_thread_abandoned  s    	

r   MAX)rM      r   r   FTuse_default_limiterrF   r   )r   r   r   r$   c           
   	     s  d|  }t  t  |r4t }|j}| |_d nt| }| }|zG dd dd_d_d_d_	t
 dddfdd	d
dd fdd}t
 4 I d H }td g }t|D ],}	|t  |||d  t I d H  qΈ r(td |d  I d H  t I d H  j	| ksD| j| krVtdI d H  q(  W d   I d H  q1 I d H s0    Y  j| ksJ  r|jdkrtdI d H  qj|ksJ jdksJ W ||_n||_0 d S )Nr	   c                   @  s.   e Zd ZU ded< ded< ded< ded< dS )z0test_run_in_worker_thread_limiter.<locals>.staterF   ran
high_waterrb   parkedN)__name__
__module____qualname____annotations__r8   r8   r8   r9   state  s   
r   r   r
   r"   )r   r$   c                   s   t d t| jd J  jd7  _ jd7  _tjj_ jd7  _W d    n1 sj0    Y     ,  jd8  _ jd8  _W d    n1 s0    Y  t d d S )Nzthread_fn startr+   r   zthread_fn exiting)	r2   r   r   r   rb   maxr   r   r|   r   )gatelockr   rS   r8   r9   ry     s    ,,z4test_run_in_worker_thread_limiter.<locals>.thread_fnr   )eventr$   c                   sV   t  &}t| dI d H  W d    n1 s40    Y  td|j |   d S )N)r   limiterzrun_thread finished, cancelled:)r   r
   r   r2   r   rl   )r   r   )r   limiter_argry   r8   r9   
run_thread  s    
*z5test_run_in_worker_thread_limiter.<locals>.run_threadZspawningz'waiting for first cancellation to clearr=   )r/   r   Lockr   Ztotal_tokensr   r   r   rb   r   r   rR   r   r2   r   r.   r   r   r|   
statisticsZtasks_waitingr   rl   borrowed_tokens)
r   r   r   ZCOUNTcZorig_total_tokensr   r   eventsrq   r8   )r   r   r   r   r   ry   rS   r9   !test_run_in_worker_thread_limiter  sX    6r   c                    sB   g  G  fddd} t dd |  dI d H   ddgks>J d S )Nc                      s4   e Zd Zddd fddZddd fddZdS )	z?test_run_in_worker_thread_custom_limiter.<locals>.CustomLimiterr    r"   borrowerr$   c                   s     d || _d S Nacquirer.   Z	_borrowerselfr   rH   r8   r9   acquire_on_behalf_of6  s    
zTtest_run_in_worker_thread_custom_limiter.<locals>.CustomLimiter.acquire_on_behalf_ofc                   s     d || jksJ d S )Nreleaser   r   rH   r8   r9   release_on_behalf_of:  s    
zTtest_run_in_worker_thread_custom_limiter.<locals>.CustomLimiter.release_on_behalf_ofNr   r   r   r   r  r8   rH   r8   r9   CustomLimiter5  s   r  c                   S  s   d S rU   r8   r8   r8   r8   r9   rV   @  rW   z:test_run_in_worker_thread_custom_limiter.<locals>.<lambda>r   r   r   r   )r  r8   rH   r9   (test_run_in_worker_thread_custom_limiter0  s    r  c                    s   g G fddd} |  }t jtdd&}tdd |dI d H  W d    n1 sV0    Y  |jjd u spJ dd	gksJ g i  t jtdd*}t fd
d|dI d H  W d    n1 s0    Y  t|jjtsJ dd	gksJ d S )Nc                      s4   e Zd Zddd fddZddd fddZd	S )
zCtest_run_in_worker_thread_limiter_error.<locals>.BadCapacityLimiterr    r"   r   c                   s     d d S r   )r.   r   rH   r8   r9   r   H  s    zXtest_run_in_worker_thread_limiter_error.<locals>.BadCapacityLimiter.acquire_on_behalf_ofr   c                   s     d tdd S )Nr   zrelease on behalf)r.   rK   r   rH   r8   r9   r  K  s    
zXtest_run_in_worker_thread_limiter_error.<locals>.BadCapacityLimiter.release_on_behalf_ofNr  r8   rH   r8   r9   BadCapacityLimiterG  s   r  z^release on behalf$r   c                   S  s   d S rU   r8   r8   r8   r8   r9   rV   R  rW   z9test_run_in_worker_thread_limiter_error.<locals>.<lambda>r  r   r   c                     s    d S )Nr4   r8   r8   )dr8   r9   rV   [  rW   )rY   rZ   rK   r   r   __context__
isinstancerP   )r  bsr   r8   )r  r7   r9   'test_run_in_worker_thread_limiter_errorD  s    48r  )r   r$   c                   s   dddddd}|  tjjd| t }|jdks8J tt"}t	dd	 I d H  W d    n1 sj0    Y  d
t
|jv sJ |jdksJ d S )Nobjectr   )r   r{   r$   c                 W  s   t dd S )Nz!the engines canna take it captain)r[   )r   r{   r8   r8   r9   	bad_startd  s    z:test_run_in_worker_thread_fail_to_spawn.<locals>.bad_startstart_thread_soonr   c                   S  s   d S rU   r8   r8   r8   r8   r9   rV   n  rW   z9test_run_in_worker_thread_fail_to_spawn.<locals>.<lambda>Zengines)r   r   r   ZThreadCacher   r   rY   rZ   r[   r   r   r   )r   r  r   r   r8   r8   r9   'test_run_in_worker_thread_fail_to_spawn`  s    0r  c                    s4   dddd} t  }t| I d H }||ks0J d S )Nrt   r#   c                  S  s   t tj} | S rU   r   r   rR   callee_tokenr8   r8   r9   ry   w  s    
z5test_trio_to_thread_run_sync_token.<locals>.thread_fnr   rR   r   ry   Zcaller_tokenr  r8   r8   r9   "test_trio_to_thread_run_sync_tokent  s    r  c                    sN   dddd} t jtdd t| I d H  W d    n1 s@0    Y  d S )Nr"   r#   c                     s   d S rU   r8   r8   r8   r8   r9   async_fn  s    z=test_trio_to_thread_run_sync_expected_error.<locals>.async_fnzexpected a sync functionr   )rY   rZ   	TypeErrorr   r  r8   r8   r9   +test_trio_to_thread_run_sync_expected_error  s    r  trio_test_contextvarzcontextvars.ContextVar[str]c                    s   t  } td dddd}t|I d H \}}|dks>J || ksJJ dddd}t|I d H \}}}t }|dksJ |d	ksJ |dksJ d
t dksJ d S )Nr}   ztuple[str, threading.Thread]r#   c                  S  sH   t  } ttj t  W d    n1 s20    Y  | t fS rU   )	r  r   rY   rZ   sniffioAsyncLibraryNotFoundErrorcurrent_async_libraryr/   r0   )r   r8   r8   r9   r>     s    &z3test_trio_to_thread_run_sync_contextvars.<locals>.fz!tuple[str, str, threading.Thread]c                  S  s\   t  } t d t  }ttj t  W d    n1 sD0    Y  | |t	 fS )Nworker)
r  r   rl   rY   rZ   r  r  r  r/   r0   )parent_valueinner_valuer8   r8   r9   r     s    
&z3test_trio_to_thread_run_sync_contextvars.<locals>.gr  zRThe contextvar value set on the worker would not propagate back to the main threadtrio)r/   r0   r  rl   r   r   r  r  )rC   r>   r   rA   r   r  r   current_valuer8   r8   r9   (test_trio_to_thread_run_sync_contextvars  s    

r#  c                    s   dddd} t | I d H }t|ts*J dddd dd fdd	}tjtd
d t |I d H  W d    n1 s|0    Y  d S )Nfloatr#   c                  S  s   t tj} | S rU   )r   r   current_time)	trio_timer8   r8   r9   thread_fn_1  s    
z3test_trio_from_thread_run_sync.<locals>.thread_fn_1r"   c                     s   d S rU   r8   r8   r8   r8   r9   r    s    z0test_trio_from_thread_run_sync.<locals>.async_fnc                     s   t   d S rU   )r   r8   r  r8   r9   thread_fn_2  s    z3test_trio_from_thread_run_sync.<locals>.thread_fn_2zexpected a synchronous functionr   )r   r	  r$  rY   rZ   r  )r'  r&  r(  r8   r  r9   test_trio_from_thread_run_sync  s    r)  c                    s   g ddfdd dd fdd} t | I d H  ddgksHJ ddd	d
}tjtdd  t t|I d H  W d    n1 s0    Y  d S )Nr"   r#   c                     s   t    d d S )Nback in trio)r   r%  r.   r8   rH   r8   r9   back_in_trio_fn  s    z2test_trio_from_thread_run.<locals>.back_in_trio_fnc                     s    d t  d S )N	in thread)r.   r   r8   r+  r7   r8   r9   ry     s    
z,test_trio_from_thread_run.<locals>.thread_fnr,  r*  c                   S  s   d S rU   r8   r8   r8   r8   r9   sync_fn  s    z*test_trio_from_thread_run.<locals>.sync_fnzappears to be synchronousr   )r   rY   rZ   r  r   )ry   r.  r8   r-  r9   test_trio_from_thread_run  s    r/  c                    s4   dddd} t  }t| I d H }||ks0J d S )Nrt   r#   c                  S  s   t tj} | S rU   r  r  r8   r8   r9   ry     s    
z.test_trio_from_thread_token.<locals>.thread_fnr  r  r8   r8   r9   test_trio_from_thread_token  s    r0  c                    s8   ddddd} t  }t| |I d H }||ks4J d S )Nrt   ru   c                 S  s   t tj| d}|S )Nr+   r  )rS   r  r8   r8   r9   ry     s    z4test_trio_from_thread_token_kwarg.<locals>.thread_fnr  r  r8   r8   r9   !test_trio_from_thread_token_kwarg  s    r1  c                     s8   t t ttj W d    n1 s*0    Y  d S rU   rY   rZ   r[   r   r   r%  r8   r8   r8   r9   test_from_thread_no_token  s    r3  c                    s   t d dddd} t| I d H \}}}}}t  }||  krLdksRn J ||  krp|  krpdksvn J t dksJ |dksJ d S )	Nr}   tuple[str, str, str, str, str]r#   c                  S  s   t  } t d t  }ttj t  W d    n1 sD0    Y  dddd}t|\}}t  }ttj t  W d    n1 s0    Y  | ||||fS )Nr  tuple[str, str]r#   c                  S  s2   t  } t d t  }t dks*J | |fS Nback_in_mainr!  r  r   rl   r  r  back_parent_valueback_current_valuer8   r8   r9   r7  
  s
    
zStest_trio_from_thread_run_sync_contextvars.<locals>.thread_fn.<locals>.back_in_main)	r  r   rl   rY   rZ   r  r  r  r   )thread_parent_valuethread_current_valuer7  r:  r;  thread_after_valuer8   r8   r9   ry     s     
&&z=test_trio_from_thread_run_sync_contextvars.<locals>.thread_fnr  r!  r7  r  rl   r   r   r  r  ry   r<  r=  r>  r:  r;  r"  r8   r8   r9   *test_trio_from_thread_run_sync_contextvars   s    
 $rA  c                    s   t d dddd} t| I d H \}}}}}t  }||  krLdksRn J ||  krp|  krpdksvn J |dksJ t dksJ d S )	Nr}   r4  r#   c                  S  s   t  } t d t  }ttj t  W d    n1 sD0    Y  dddd}t|\}}t  }ttj t  W d    n1 s0    Y  | ||||fS )Nr  r5  r#   c                    s2   t  } t d t  }t dks*J | |fS r6  r8  r9  r8   r8   r9   async_back_in_main5  s
    
zTtest_trio_from_thread_run_contextvars.<locals>.thread_fn.<locals>.async_back_in_main)	r  r   rl   rY   rZ   r  r  r  r   )r<  r=  rB  r:  r;  r>  r8   r8   r9   ry   .  s     
&&z8test_trio_from_thread_run_contextvars.<locals>.thread_fnr  r7  r!  r?  r@  r8   r8   r9   %test_trio_from_thread_run_contextvars+  s    
 $rC  c                   C  s<   t t ttjdd W d    n1 s.0    Y  d S )NzNot TrioTokentyper+   r2  r8   r8   r8   r9   3test_run_fn_as_system_task_caught_badly_typed_tokenV  s
    rD  c                    sP   dddd} t  }tt t| |d W d    n1 sB0    Y  d S )Nr"   r#   c                   S  s
   t  d S rU   )AssertionErrorr8   r8   r8   r9   
not_called_  s    z7test_from_thread_inside_trio_thread.<locals>.not_calledr+   )r   rR   rY   rZ   r[   r   )rF  r)   r8   r8   r9   #test_from_thread_inside_trio_thread^  s    rG  c                    s^   g g dddfdd ddd fd	d
} t | d t | d ddgksZJ d S )Nr%   zAsyncGenerator[None, None]ru   c                  s   z~d V  W t jddX zttttd| dI d H  W n t jyT    d Y n0  d W d    q1 st0    Y  nvt jddX zttttd| dI d H  W n t jy    d Y n0  d W d    n1 s0    Y  0 d S )NTr   r   r+   r   clean)r   r
   r   r   r   r   ZRunFinishedErrorr.   rx   rH   r8   r9   agenk  s$    *z2test_from_thread_run_during_shutdown.<locals>.agenr   r"   )use_system_taskr$   c                   s2     | rt nd  d d I d H  d S )Nr   )r.   r   rR   asend)rJ  )rI  saver8   r9   r}   y  s    z2test_from_thread_run_during_shutdown.<locals>.mainTFr   rH  )r   rm   )r}   r8   )rI  r7   rL  r9   $test_from_thread_run_during_shutdowng  s    rM  c                    s4   t  } t| t jsJ t| }| | u s0J d S rU   )r   rR   r	  Z	TrioTokenweakrefref)rS   Zweak_referencer8   r8   r9   "test_trio_token_weak_referenceable  s    
rP  c                    sP   G dd d} t t$ tt|  dI d H  W d    n1 sB0    Y  d S )Nc                   @  s   e Zd ZddddZdS )z4test_unsafe_abandon_on_cancel_kwarg.<locals>.BadBoolr   r#   c                 S  s   t d S rU   )NotImplementedError)r   r8   r8   r9   __bool__  s    z=test_unsafe_abandon_on_cancel_kwarg.<locals>.BadBool.__bool__N)r   r   r   rR  r8   r8   r8   r9   BadBool  s   rS  r   )rY   rZ   rQ  r   rF   )rS  r8   r8   r9   #test_unsafe_abandon_on_cancel_kwarg  s    rT  c                    sL   t  } dddd}| ttt jI d H u s0J | tt|I d H u sHJ d S )Nz
_core.Taskr#   c                     s   t  S rU   )r   current_taskr8   r8   r8   r9   async_current_task  s    z8test_from_thread_reuses_task.<locals>.async_current_task)r   rU  r   r   r   )taskrV  r8   r8   r9   test_from_thread_reuses_task  s    rX  c                    s0   d  dd fdd}  t | I d H ks,J d S )NrF   r#   c                     s   t   ttt jS rU   )r/   r   r   r   r8   tidr8   r9   get_tid_then_reenter  s    z6test_recursive_to_thread.<locals>.get_tid_then_reenterr   )r[  r8   rY  r9   test_recursive_to_thread  s    r\  c                    s   t  dd fdd} t  t| I d H  W d    n1 sH0    Y   jr\J  rhJ t " t| ddI d H  W d    n1 s0    Y   jsJ ttjddI d H rJ d	dd
ddd fdd}t  t|I d H  W d    n1 s0    Y   jr0J  r>J t " t|ddI d H  W d    n1 sp0    Y   jsJ ttjddI d H rJ dd fdd}t   tt	|I d H  W d    n1 s0    Y   jsJ d S )Nr"   r#   c                     sD   t  j zt t W n tjy4   d Y n0 d d S NTF)r   r   r   r   rw   r   r8   )r   queuer8   r9   
sync_check  s    
z3test_from_thread_host_cancelled.<locals>.sync_checkTr   r   timeoutr   c                     s   dS NTr8   r8   r8   r8   r9   no_checkpoint  s    z6test_from_thread_host_cancelled.<locals>.no_checkpointc                     sH   t  j ztsJ W n tjy8   d Y n0 d d S r]  )r   r   r   r   rw   r   r8   r   rc  r^  r8   r9   async_check  s    
z4test_from_thread_host_cancelled.<locals>.async_checkc                     s@       td t I d H  W d    n1 s20    Y  d S )Nr   )r   r   r   r8   r   r8   r9   async_time_bomb  s    
z8test_from_thread_host_cancelled.<locals>.async_time_bomb)
r   r   r   r
   r   r   
get_nowaitr   r   r   )r_  re  rf  r8   rd  r9   test_from_thread_host_cancelled  s0    	
,

0
	
.
2
0rh  c               	     s  t  ddddfdd} dd fdd	g t  t 4 I d H `}|| d
t  t I d H  d dksJ j	dddksJ  
  W d   I d H  q1 I d H s0    Y  d dksJ g t  t }t 4 I d H j}|| d
| t I d H  d dks(J j	dddks>J |   
  W d   I d H  q|1 I d H sr0    Y  |jsJ dv sJ d dksJ dd fdd	g t  t }t 4 I d H T}|| d| t I d H  d dksJ |   
  W d   I d H  qL1 I d H sB0    Y  |jsXJ dv sfJ d dksxJ j	dddksJ d S )Nr   r
   r"   )r   r   r$   c              
     s   |  d z^z.t | dI d H W W  d W  d    S  tjy`    d  Y n0 W  d n d 0 W d    n1 s0    Y  d S )Nr*   r   r   r   )r.   r   r   rw   )r   r   r   r8   r9   r     s    



z/test_from_thread_check_cancelled.<locals>.childr#   c                     sB   z
t   W n tjy(   d Y n0 d    t  S Nrw   Not Cancelled)r   r   rw   r   r|   r8   rr   r   r8   r9   r>     s    

z+test_from_thread_check_cancelled.<locals>.fFr   r*   r   r`  rj  r   r   r   c                     s@       z
t  W n tjy0   d Y n0 d d S ri  )r|   r   r   rw   r   r8   rk  r8   r9   r>     s    
Trw   )r   r   r/   r   r   r   r   r
   r   r   rl   r   r   )r   r   r   r8   )rr   r>   r   r7   r9    test_from_thread_check_cancelled  sN    26	6rl  c                     s   t t t  W d    n1 s&0    Y  t  tt fdd t t   jdd	  W d    n1 s|0    Y  d S )Nc                   s
     | S rU   )r   rp   r   r8   r9   rV   6  rW   zLtest_from_thread_check_cancelled_raises_in_foreign_threads.<locals>.<lambda>r   r`  )
rY   rZ   r[   r   r   r   r   r  r   unwrapr8   r8   r   r9   :test_from_thread_check_cancelled_raises_in_foreign_threads2  s    $rn  c               
     s   dddd} t dd t 4 I d H .}tdD ]}||  q2W d   I d H  ql1 I d H sb0    Y  W d    n1 s0    Y  d S )Nr"   r#   c                     s   t ttdddI d H  q d S )Nr   Fr   )r   r   r   r8   r8   r8   r9   r   A  s    z+test_reentry_doesnt_deadlock.<locals>.childr	      )r   r   r   r   r   )r   r   rq   r8   r8   r9   test_reentry_doesnt_deadlock;  s
    
rp  c               	     sL  d t  } t  }t  }t  }dddddd}dd fdd	}t 4 I d H }||| | |||| t I d H  || t I d H   rJ t d
ksJ |   | I d H  t I d H   rJ t dksJ |  | I d H  t I d H   s
J t dksJ W d   I d H  qH1 I d H s>0    Y  d S )NFr   r"   )r   e_exitr$   c                   s,   dd fdd}t |I d H  |  d S )Nr"   r#   c                     s   t  j d S rU   )r   r|   r8   r   r8   r9   rj   T  s    zCtest_wait_all_threads_completed.<locals>.wait_event.<locals>.thread)r   rl   )r   rq  rj   r8   rr  r9   
wait_eventS  s    z3test_wait_all_threads_completed.<locals>.wait_eventr#   c                     s   t  I d H  d d S rb  )r   r8   Zno_threads_leftr8   r9   wait_no_threads_leftZ  s    z=test_wait_all_threads_completed.<locals>.wait_no_threads_leftr	   r   r   )r   r   r   r   r   r   rl   r|   )e1e2Z	e1_exitedZ	e2_exitedrs  ru  r   r8   rt  r9   test_wait_all_threads_completedK  s2    

rx  c                     s   t  I d H  t dksJ d S )Nr   )r   r   r8   r8   r8   r9   *test_wait_all_threads_completed_no_threadsu  s    ry  )N)e
__future__r   contextvarsr^  r   r   r^   r/   r   rN  	functoolsr   typingr   r   r   r   rY   r  r   r
   r   r   r   r   r   r   r   Z_core._tests.test_kir   Z_core._tests.tutilr   _threadsr   r   r   r   r   r   r   testingr   collections.abcr   r   r   outcomer   Zlowlevelr    listtupler   r?   r3   r1   rE   r!   rT   r\   rn   r~   r   r   r   r   r   r   r   markZparametrizer   r  r  r  r  r  
ContextVarr  r   r#  r)  r/  r0  r1  r3  rA  rC  rD  rG  rM  rP  rT  rX  r\  rh  rl  rn  rp  rx  ry  r8   r8   r8   r9   <module>   s   (
$	<0.#	<&t	%++	
;N	*