o
    }DeҔ                     @  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mZmZmZmZmZ d dlZd dl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"m#Z#m$Z$ dd	l%m&Z& dd
l'm(Z(m)Z) ddl*m+Z+m,Z,m-Z-m.Z.m/Z/ ddl0m1Z1 ddl2m3Z3 eee4eej5ee6 f f  Z7edZ8dpddZ9dpddZ:dpddZ;dpddZ<dpddZ=dqdrddZ>dpd d!Z?dpd"d#Z@dpd$d%ZAdpd&d'ZBdsd,d-ZCejDEd.g d/ejDEd0d1d2gejDEd3d1d2gdtd6d7ZFdpd8d9ZGdpd:d;ZHdud<d=ZIdpd>d?ZJdpd@dAZKeLdBZMdCeNdB< dpdDdEZOdpdFdGZPdpdHdIZQdpdJdKZRdpdLdMZSdpdNdOZTdpdPdQZUdpdRdSZVdpdTdUZWdpdVdWZXejDjYe(dXdYdpdZd[ZZdpd\d]Z[dpd^d_Z\dpd`daZ]dpdbdcZ^dpdddeZ_dpdfdgZ`dpdhdiZae)dpdjdkZbdpdldmZcdpdndoZddS )v    )annotationsNpartial)	AsyncGenerator	AwaitableCallableListNoReturnTupleTypeTypeVarUnion)Outcome)MonkeyPatch   )	CancelScopeCapacityLimiterEventTrioDeprecationWarning_core
fail_aftermove_on_aftersleepsleep_forever)ki_self)buggy_pypy_asyncgensslow)current_default_thread_limiterfrom_thread_check_cancelledfrom_thread_runfrom_thread_run_syncto_thread_run_sync)Task)wait_all_tasks_blockedTreturnNonec                    s   t   	 dd fdd} t }ddd}| t|d|dI d H  d dd}| t|dtf|dI d H  ddd}| t|d|dI d H  d dd}| t|dtf|dI d H  d S )!Ndo_in_trio_threadCallable[..., threading.Thread]fnCallable[..., T | Awaitable[T]]expectedtuple[str, T]
trio_token_core.TrioToken | Noner%   r&   c                   st   g d fdd}t j|dd}|  | r+td tdI d H  | sd	|fd
f|gks8J d S )Nr%   r&   c               
     sv   z dt f  d}  d| f W d S  ty: } zt|  dt|f W Y d }~d S d }~ww )Nstartr-   goterror)append	threadingcurrent_threadBaseExceptionprinttype)xexcr'   r)   recordr-    <usr/lib/python3.10/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%   r&   )r4   Threadr/   is_aliver7   r   )r'   r)   r+   r-   r?   child_threadtrio_threadr;   r>   
check_case:   s   	z*test_do_in_trio_thread.<locals>.check_caser<   
RecordTypeintc                 S  s"   t  rJ | dt f dS )NrC   r   )r   currently_ki_protectedr3   r4   r5   r<   r=   r=   r>   f1T      z"test_do_in_trio_thread.<locals>.f1)r1   r   r0   r	   c                 S  s"   t  rJ | dt f tNrC   )r   rM   r3   r4   r5   
ValueErrorrN   r=   r=   r>   f2[   rP   z"test_do_in_trio_thread.<locals>.f2r2   c                   s2   t  rJ t  I d H  | dt f dS )NrC      )r   rM   
checkpointr3   r4   r5   rN   r=   r=   r>   f3b   
   z"test_do_in_trio_thread.<locals>.f3)r1   rT   c                   s2   t  rJ t  I d H  | dt f trQ   )r   rM   rU   r3   r4   r5   KeyErrorrN   r=   r=   r>   f4j   rW   z"test_do_in_trio_thread.<locals>.f4N)
r'   r(   r)   r*   r+   r,   r-   r.   r%   r&   )r<   rK   r%   rL   )r<   rK   r%   r	   )r4   r5   r   current_trio_tokenr    rR   r   rX   )rJ   tokenrO   rS   rV   rY   r=   rH   r>   test_do_in_trio_thread7   s   



r]   c                    sz   t t tdd  W d    n1 sw   Y  ddd} t t t|  W d    d S 1 s6w   Y  d S )Nc                   S     d S rZ   r=   r=   r=   r=   r>   <lambda>u       z9test_do_in_trio_thread_from_trio_thread.<locals>.<lambda>r%   r&   c                        d S rZ   r=   r=   r=   r=   r>   foow      z4test_do_in_trio_thread_from_trio_thread.<locals>.foorD   )pytestraisesRuntimeErrorr    r   )rb   r=   r=   r>   'test_do_in_trio_thread_from_trio_threads   s   

"rg   c                    s2   t   d fdd} t|   ddhksJ d S )Nr%   r&   c                    s   t   ddddfddd fdd} tj| d	}|  td
 | r8tdI d H  | s-td |  td d S )Nr%   r&   c               
   S  sX   t d t r
J t d zt  W dd l} t d|   d S dd l} t d|   w )Nzin Trio threadr   r   finally)r7   r   rM   r   sysexc_info)ri   r=   r=   r>   trio_thread_fn   s   zTtest_run_in_trio_thread_ki.<locals>.check_run_in_trio_thread.<locals>.trio_thread_fnc                     s      d S rZ   r=   r=   )rk   r=   r>   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w z	td W d S  ty;   t d  d Y d S w )Nrunningr0   ok1ok2)r7   r    KeyboardInterruptaddr   r=   )r<   r\   rl   rk   r=   r>   external_thread_fn   s   zXtest_run_in_trio_thread_ki.<locals>.check_run_in_trio_thread.<locals>.external_thread_fn)r@   ZwaitingrB   zwaited, joiningdonerD   )	r   r[   r4   rE   r/   r7   rF   r   join)rr   threadrN   )r\   rl   rk   r>   check_run_in_trio_thread   s   
z<test_run_in_trio_thread_ki.<locals>.check_run_in_trio_threadrn   ro   rD   )setr   run)rv   r=   rN   r>   test_run_in_trio_thread_ki~   s   
'ry   c                    sb   g t   d fdddfddd fd
d} t| }|  ddgks/J d S )Nr%   r&   c                     s,    d    tdd I d H  d S )Nsleepingc                 S  s   t jjS rZ   )r   ZAbortZ	SUCCEEDED_r=   r=   r>   r_          zMtest_await_in_trio_thread_while_main_exits.<locals>.trio_fn.<locals>.<lambda>)r3   rw   r   Zwait_task_rescheduledr=   )evr<   r=   r>   trio_fn   s   
z;test_await_in_trio_thread_while_main_exits.<locals>.trio_fnr\   _core.TrioTokenc                   s4   z	t | d W d S  tjy    d Y d S w )Nr0   	cancelled)r   r   	Cancelledr3   r\   )r<   r   r=   r>   	thread_fn   s
   z=test_await_in_trio_thread_while_main_exits.<locals>.thread_fnthreading.Threadc                    sB   t  } tj| fd}|    I d H  dgksJ |S )N)r@   argsrz   )r   r[   r4   rE   r/   wait)r\   ru   )r~   r<   r   r=   r>   main   s   z8test_await_in_trio_thread_while_main_exits.<locals>.mainrz   r   rD   )r\   r   r%   r&   )r%   r   )r   r   rx   rt   )r   ru   r=   )r~   r<   r   r   r>   *test_await_in_trio_thread_while_main_exits   s   
r   c                    s   d} d|  fddddfd
d t I d H  t d dI d H  t  d|  I d H  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namestrr%   r   c                 S  s   t  j| ks	J t  S rZ   )r4   r5   r   r   r=   r=   r>   r      s   z test_named_thread.<locals>.inner"Callable[[None], threading.Thread]c                   
   t  | S rZ   r   r   r   r=   r>   rC         
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   ru   rC   r=   r>   test_thread_name   s   z+test_named_thread.<locals>.test_thread_name fobiedooname_longer_than_15_characters   💙r   r   r%   r   r   r   r%   r   )r   r   r%   r&   r!   )Zendingr   r=   rC   r   r>   test_named_thread   s   r   ident
int | None
str | Nonec              
   C  s   dd l }dd l}|jd}|stdtj d d S ||}t|dd }|s*J |j	|j
|jg|_|j|_|jddd}| d u rFt } || |ddksPJ z|j W S  tyu } ztd	|jd
|  W Y d }~d S d }~ww )Nr   Zpthreadzno pthread on )pthread_getname_np       )sizezvalue: z, exception: )ctypesZctypes.utilutilZfind_libraryr7   ri   platformCDLLgetattrZc_void_pc_char_pZc_size_targtypesc_intrestypeZcreate_string_bufferr4   	get_identvaluedecodeUnicodeDecodeErrorrd   Zfail)r   r   Zlibpthread_pathZ
libpthreadr   Zname_bufferer=   r=   r>   _get_thread_name   s0   
&r   c                    s   ddddfdd	 d
} t  | I d H  t  | d dI d H  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   r   r%   r   c                 S  sN   t  }|d u rtjdkrtdtj  t S || d d ks#J t S )Nlinuxzno pthread OS support on    )r   ri   r   rd   skipr4   r5   )r   os_thread_namer=   r=   r>   r     s   z#test_named_thread_os.<locals>.innerr   c                   r   rZ   r   r   r   r=   r>   rC     r   ztest_named_thread_os.<locals>.fz4None from trio._tests.test_threads.test_named_threadr   r+   r   r&   c                   sR   |d u r| }t  || dI d H }t|j}|d usJ dtd|s'J d S )Nr   z'should skip earlier if this is the caser   )r!   r   r   r   r   )r   r+   ru   r   r   r=   r>   r      s   
z.test_named_thread_os.<locals>.test_thread_namer   r   r   r   ?)r+   r   r   rZ   )r   r   r+   r   r%   r&   r   )defaultr   r=   r   r>   test_named_thread_os  s   
		r   c                    sD   ddl m}  |  }|d u r tjdksJ tdtj  d S d S )Nr   )get_os_thread_name_funcr   zno pthread_setname_np on )Ztrio._core._thread_cacher   ri   r   rd   r   )r   kr=   r=   r>   test_has_pthread_setname_np0  s   r   c                    s   t  } ddd}t|dI d H \}}|dksJ || ks J dd	d
}tt}t|I d H  W d    n1 s<w   Y  t|jj |jjd | ksQJ d S )Nr9   r$   r%   tuple[T, threading.Thread]c                 S  s   | t  fS rZ   )r4   r5   )r9   r=   r=   r>   rC   <     z$test_run_in_worker_thread.<locals>.f   r	   c                   S  s   t t rZ   )rR   r4   r5   r=   r=   r=   r>   gC  r   z$test_run_in_worker_thread.<locals>.gr   )r9   r$   r%   r   )r%   r	   )	r4   r5   r!   rd   re   rR   r7   r   r   )rI   rC   r9   rG   r   excinfor=   r=   r>   test_run_in_worker_thread9  s   

r   c               	     s  d gdfdd d fd
d} g t  }t 4 I d H $}|| |d t I d H  dgks6J |j  W d   I d H  n1 I d H sKw   Y  d dksXJ |d  d dkrnt	
d d dkscg d d< t 4 I d H M}|| |d t I d H  |j  tjdd tdD ]	}t I d H  qW d    n1 sw   Y  dgksJ |d  W d   I d H  n1 I d H sw   Y  t }|  | |dI d H  W d    n1 sw   Y  |jsJ d S )Nqstdlib_queue.Queue[str]r%   r&   c                   s   d d< |    d d< d S )Nblockingr   finished)getr   )registerr=   r>   rC   O  s   z1test_run_in_worker_thread_cancellation.<locals>.fstdlib_queue.Queue[None]abandon_on_cancelboolc              	     s:    d zt | |dI d H W  d S  d w )Nr/   r   exit)r3   r!   )r   r   rC   r<   r=   r>   childU  s
   
z5test_run_in_worker_thread_cancellation.<locals>.childTr/   r   r   rB   FZshield
   )r   r   r%   r&   )r   r   r   r   r%   r&   )stdlib_queueQueuer   open_nursery
start_soonr#   cancel_scopecancelputtimer   r   rangerU   cancelled_caught)r   r   nurseryr|   scoper=   )rC   r<   r   r>   &test_run_in_worker_thread_cancellationL  sH   (



(
r   capfdpytest.CaptureFixture[str]monkeypatchr   c                   s   | tjdd t  t d
 fddd
fdd}t|  d   }| r:t	
d | s1|  \}}d	|vsFJ d	|vsLJ d S )NZIDLE_TIMEOUTrB   r%   r&   c                     s       t  d S rZ   )r   r   r4   r5   r=   )q1q2r=   r>   r     s   z6test_run_in_worker_thread_abandoned.<locals>.thread_fnc               	     sr   d fdd} t  4 I d H }||  t I d H  |j  W d   I d H  d S 1 I d H s2w   Y  d S )Nr%   r&   c                     s   t  ddI d H  d S )NTr   r   r=   r   r=   r>   r     s   z@test_run_in_worker_thread_abandoned.<locals>.main.<locals>.childrD   )r   r   r   r#   r   r   )r   r   r   r=   r>   r     s   
.z1test_run_in_worker_thread_abandoned.<locals>.mainzException in threadrD   )setattrr   _thread_cacher   r   rx   r   r   rF   r   r   Z
readouterr)r   r   r   ru   outerrr=   )r   r   r   r>   #test_run_in_worker_thread_abandoned  s   
	

r   MAX)rT      r   r   FTuse_default_limiterrL   r   c           
   	     s  d|  }t  t  |rt }|j}| |_d nt| }| }|zG dd dd_d_d_d_	t
 dfd	d
d fdd}t
 4 I d H d}td g }t|D ]}	|t  |||d  t I d H  qa rtd |d  I d H  t I d H  j	| ks| j| krtdI d H  j	| ks| j| ks  W d   I d H  n1 I d H sw   Y  j| ksJ  r|jdkrtdI d H  |jdksӈj|ksJ jdksJ W ||_d S ||_w )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>.staterL   ran
high_waterrm   parkedN)__name__
__module____qualname____annotations__r=   r=   r=   r>   state  s
   
 r   r   r   r   r%   r&   c                   s   t d t| jd %  jd7  _ jd7  _tjj_ jd7  _W d    n1 s5w   Y       jd8  _ jd8  _W d    n1 sYw   Y  t d d S )Nzthread_fn startr0   r   zthread_fn exiting)	r7   r    r   r   rm   maxr   r   r   r   )gatelockr   r\   r=   r>   r     s   z4test_run_in_worker_thread_limiter.<locals>.thread_fneventr   c                   sX   t  }t| dI d H  W d    n1 sw   Y  td|j |   d S )N)r   limiterzrun_thread finished, cancelled:)r   r   r!   r7   r   rw   )r   r   )r   limiter_argr   r=   r>   
run_thread  s   
z5test_run_in_worker_thread_limiter.<locals>.run_threadZspawningz'waiting for first cancellation to clearrB   )r   r   r%   r&   )r   r   r%   r&   )r4   r   Lockr   Ztotal_tokensr   r   r   rm   r   r   r[   r   r7   r   r3   r   r#   r   
statisticsZtasks_waitingr   rw   borrowed_tokens)
r   r   r   ZCOUNTcZorig_total_tokensr  r   eventsr|   r=   )r   r   r  r   r   r   r\   r>   !test_run_in_worker_thread_limiter  sZ   
(

r	  c                    sD   g  G  fddd} t dd |  dI d H   ddgks J d S )Nc                      s(   e Zd Zd
 fddZd
 fddZd	S )z?test_run_in_worker_thread_custom_limiter.<locals>.CustomLimiterborrowerr"   r%   r&   c                   s     d || _d S Nacquirer3   Z	_borrowerselfr
  rN   r=   r>   acquire_on_behalf_of&  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  rN   r=   r>   release_on_behalf_of*  s   
zTtest_run_in_worker_thread_custom_limiter.<locals>.CustomLimiter.release_on_behalf_ofNr
  r"   r%   r&   r   r   r   r  r  r=   rN   r=   r>   CustomLimiter%  s    r  c                   S  r^   rZ   r=   r=   r=   r=   r>   r_   0  r`   z:test_run_in_worker_thread_custom_limiter.<locals>.<lambda>r   r  r  r   )r  r=   rN   r>   (test_run_in_worker_thread_custom_limiter   s
   r  c                    s   g G fddd} |  }t t}tdd |dI d H  W d    n1 s*w   Y  |jjd u s7J ddgks?J g i  t t}t fdd|dI d H  W d    n1 s`w   Y  t|jjtsnJ ddgksvJ d S )	Nc                      s(   e Zd Zd fddZd fdd	Zd
S )zCtest_run_in_worker_thread_limiter_error.<locals>.BadCapacityLimiterr
  r"   r%   r&   c                   s     d d S r  )r3   r  rN   r=   r>   r  8  s   zXtest_run_in_worker_thread_limiter_error.<locals>.BadCapacityLimiter.acquire_on_behalf_ofr	   c                   s     d tr  )r3   rR   r  rN   r=   r>   r  ;     
zXtest_run_in_worker_thread_limiter_error.<locals>.BadCapacityLimiter.release_on_behalf_ofNr  )r
  r"   r%   r	   r  r=   rN   r=   r>   BadCapacityLimiter7  s    r  c                   S  r^   rZ   r=   r=   r=   r=   r>   r_   B  r`   z9test_run_in_worker_thread_limiter_error.<locals>.<lambda>r  r  r  c                     s    d S )Nr9   r=   r=   )dr=   r>   r_   K  r}   )rd   re   rR   r!   r   __context__
isinstancerX   )r  bsr   r=   )r  r<   r>   'test_run_in_worker_thread_limiter_error4  s    r  c                   s   ddd}|  tjjd| t }|jd	ksJ tt}t	d
d I d H  W d    n1 s2w   Y  dt
|jv s@J |jd	ksGJ d S )Nr  objectr   r%   r	   c                 W  s   t d)Nz!the engines canna take it captain)rf   )r  r   r=   r=   r>   	bad_startR  s   z:test_run_in_worker_thread_fail_to_spawn.<locals>.bad_startstart_thread_soonr   c                   S  r^   rZ   r=   r=   r=   r=   r>   r_   \  r`   z9test_run_in_worker_thread_fail_to_spawn.<locals>.<lambda>Zengines)r  r   r   r   r%   r	   )r   r   r   ZThreadCacher   r  rd   re   rf   r!   r   r   )r   r!  r   r   r=   r=   r>   'test_run_in_worker_thread_fail_to_spawnP  s   
r#  c                    2   ddd} t  }t| I d H }||ksJ d S )Nr%   r   c                  S     t tj} | S rZ   r    r   r[   callee_tokenr=   r=   r>   r   e  r  z5test_trio_to_thread_run_sync_token.<locals>.thread_fnr%   r   r   r[   r!   r   Zcaller_tokenr(  r=   r=   r>   "test_trio_to_thread_run_sync_tokenb  
   
r,  c                    sN   ddd} t jtdd t| I d H  W d    d S 1 s w   Y  d S )Nr%   r&   c                     ra   rZ   r=   r=   r=   r=   r>   async_fnp  rc   z=test_trio_to_thread_run_sync_expected_error.<locals>.async_fnzexpected a sync functionr   rD   )rd   re   	TypeErrorr!   r.  r=   r=   r>   +test_trio_to_thread_run_sync_expected_errorn  s
   
"r2  trio_test_contextvarzcontextvars.ContextVar[str]c                    s   t  } td ddd}t|I d H \}}|dksJ || ks$J ddd}t|I d H \}}}t }|dks=J |d	ksCJ |dksKJ d
t dksSJ d S )Nr   r%   tuple[str, threading.Thread]c                  S  sH   t  } ttj t  W d    n1 sw   Y  | t fS rZ   )	r3  r   rd   re   sniffioAsyncLibraryNotFoundErrorcurrent_async_libraryr4   r5   )r   r=   r=   r>   rC     s
   
z3test_trio_to_thread_run_sync_contextvars.<locals>.f!tuple[str, str, threading.Thread]c                  S  s\   t  } t d t  }ttj t  W d    n1 s"w   Y  | |t	 fS )Nworker)
r3  r   rw   rd   re   r5  r6  r7  r4   r5   )parent_valueinner_valuer=   r=   r>   r     s   

z3test_trio_to_thread_run_sync_contextvars.<locals>.gr9  zRThe contextvar value set on the worker would not propagate back to the main threadtrio)r%   r4  )r%   r8  )r4   r5   r3  rw   r!   r   r5  r7  )rI   rC   r   rG   r   r:  r;  current_valuer=   r=   r>   (test_trio_to_thread_run_sync_contextvars|  s    



r>  c                    s   ddd} t | I d H }t|tsJ ddd d fdd	}tjtd
d t |I d H  W d    d S 1 s:w   Y  d S )Nr%   floatc                  S  r%  rZ   )r    r   current_time)	trio_timer=   r=   r>   thread_fn_1  r  z3test_trio_from_thread_run_sync.<locals>.thread_fn_1r&   c                     ra   rZ   r=   r=   r=   r=   r>   r.    rc   z0test_trio_from_thread_run_sync.<locals>.async_fnc                     s   t   d S rZ   )r    r=   r1  r=   r>   thread_fn_2  r   z3test_trio_from_thread_run_sync.<locals>.thread_fn_2zexpected a synchronous functionr/  )r%   r?  rD   )r!   r  r?  rd   re   r0  )rB  rA  rC  r=   r1  r>   test_trio_from_thread_run_sync  s   

"rD  c                    s   g dfdd d fdd} t | I d H  ddgks!J dd	d
}tjtdd t t|I d H  W d    d S 1 sAw   Y  d S )Nr%   r&   c                     s   t    d d S )Nback in trio)r   r@  r3   r=   rN   r=   r>   back_in_trio_fn  s   z2test_trio_from_thread_run.<locals>.back_in_trio_fnc                     s    d t  d S )N	in thread)r3   r   r=   rF  r<   r=   r>   r     s   
z,test_trio_from_thread_run.<locals>.thread_fnrG  rE  c                   S  r^   rZ   r=   r=   r=   r=   r>   sync_fn     z*test_trio_from_thread_run.<locals>.sync_fnzappears to be synchronousr/  rD   )r!   rd   re   r0  r   )r   rI  r=   rH  r>   test_trio_from_thread_run  s   
"rK  c                    r$  )Nr%   r   c                  S  r%  rZ   r&  r'  r=   r=   r>   r     r  z.test_trio_from_thread_token.<locals>.thread_fnr)  r*  r+  r=   r=   r>   test_trio_from_thread_token  r-  rL  c                    s4   ddd} t  }t| |I d H }||ksJ d S )Nr\   r   r%   c                 S  s   t tj| d}|S )Nr0   r&  )r\   r(  r=   r=   r>   r     s   z4test_trio_from_thread_token_kwarg.<locals>.thread_fn)r\   r   r%   r   r*  r+  r=   r=   r>   !test_trio_from_thread_token_kwarg  s
   
rM  c                     s<   t t ttj W d    d S 1 sw   Y  d S rZ   rd   re   rf   r    r   r@  r=   r=   r=   r>   test_from_thread_no_token  s   "rO  c                    s   t d d	dd} t| I d H \}}}}}t  }||  kr&dks)J  J ||  kr9|  kr9dks<J  J t dksDJ |dksJJ d S )
Nr   r%   tuple[str, str, str, str, str]c                  S     t  } t d t  }ttj t  W d    n1 s"w   Y  ddd}t|\}}t  }ttj t  W d    n1 sKw   Y  | ||||fS )Nr9  r%   tuple[str, str]c                  S  s2   t  } t d t  }t dksJ | |fS Nback_in_mainr<  r3  r   rw   r5  r7  back_parent_valueback_current_valuer=   r=   r>   rT    s
   
zStest_trio_from_thread_run_sync_contextvars.<locals>.thread_fn.<locals>.back_in_mainr%   rR  )	r3  r   rw   rd   re   r5  r6  r7  r    )thread_parent_valuethread_current_valuerT  rW  rX  thread_after_valuer=   r=   r>   r     $   



z=test_trio_from_thread_run_sync_contextvars.<locals>.thread_fnr9  r<  rT  r%   rP  r3  rw   r!   r   r5  r7  r   rZ  r[  r\  rW  rX  r=  r=   r=   r>   *test_trio_from_thread_run_sync_contextvars  s   

 &ra  c                    s   t d d	dd} t| I d H \}}}}}t  }||  kr&dks)J  J ||  kr9|  kr9dks<J  J |dksBJ t dksJJ d S )
Nr   r%   rP  c                  S  rQ  )Nr9  r%   rR  c                    s4   t  } t d t  }t dksJ | |fS rS  rU  rV  r=   r=   r>   async_back_in_main#  s   
zTtest_trio_from_thread_run_contextvars.<locals>.thread_fn.<locals>.async_back_in_mainrY  )	r3  r   rw   rd   re   r5  r6  r7  r   )rZ  r[  rb  rW  rX  r\  r=   r=   r>   r     r]  z8test_trio_from_thread_run_contextvars.<locals>.thread_fnr9  rT  r<  r^  r_  r`  r=   r=   r>   %test_trio_from_thread_run_contextvars  s   

 &rc  c                   C  s>   t t ttjdd W d    d S 1 sw   Y  d S )NzNot TrioTokentyper0   rN  r=   r=   r=   r>   4test_run_fn_as_system_task_catched_badly_typed_tokenD  s   "rd  c                    sP   ddd} t  }tt t| |d W d    d S 1 s!w   Y  d S )Nr%   r&   c                   S  s   t  rZ   )AssertionErrorr=   r=   r=   r>   
not_calledM  s   z7test_from_thread_inside_trio_thread.<locals>.not_calledr0   rD   )r   r[   rd   re   rf   r    )rf  r-   r=   r=   r>   #test_from_thread_inside_trio_threadL  s   
"rg  zpypy 7.2.0 is buggy)reasonc                    sR   g g dfdd d fd
d} t | d t | d ddgks'J d S )Nr\   r.   r%   AsyncGenerator[None, None]c                  s  zId V  W t jdd5 zttttd| dI d H  W n t jy+    d Y nw  d W d    d S W d    d S 1 sDw   Y  d S t jdd3 zttttd| dI d H  W n t jyp    d Y nw  d W d    w W d    w 1 sw   Y  w )NTr   r   r0   r   clean)r   r   r!   r   r   r   ZRunFinishedErrorr3   r   rN   r=   r>   agenZ  s4   0z2test_from_thread_run_during_shutdown.<locals>.agenuse_system_taskr   r&   c                   s4     | r
t nd  d d I d H  d S )Nr  )r3   r   r[   asend)rl  )rk  saver=   r>   r   h  s   z2test_from_thread_run_during_shutdown.<locals>.mainTFr   rj  )r\   r.   r%   ri  )rl  r   r%   r&   )r   rx   )r   r=   )rk  r<   rn  r>   $test_from_thread_run_during_shutdownU  s   ro  c                    s6   t  } t| t jsJ t| }| | u sJ d S rZ   )r   r[   r  Z	TrioTokenweakrefref)r\   Zweak_referencer=   r=   r>   "test_trio_token_weak_referenceableq  s
   
rr  c                    sT   G dd d} t t tt|  dI d H  W d    d S 1 s#w   Y  d S )Nc                   @  s   e Zd ZdddZdS )z4test_unsafe_abandon_on_cancel_kwarg.<locals>.BadBoolr%   r   c                 S  s   t rZ   )NotImplementedError)r  r=   r=   r>   __bool__|  rJ  z=test_unsafe_abandon_on_cancel_kwarg.<locals>.BadBool.__bool__Nr%   r   )r   r   r   rt  r=   r=   r=   r>   BadBool{  s    rv  r   )rd   re   rs  r!   rL   )rv  r=   r=   r>   #test_unsafe_abandon_on_cancel_kwargx  s
   "rw  c                    sJ   t  } ddd}| ttt jI d H u sJ | tt|I d H u s#J d S )Nr%   
_core.Taskc                     s
   t  S rZ   )r   current_taskr=   r=   r=   r>   async_current_task  s   z8test_from_thread_reuses_task.<locals>.async_current_task)r%   rx  )r   ry  r!   r    r   )Ztaskrz  r=   r=   r>   test_from_thread_reuses_task  s
   
r{  c                    s.   d  d fdd}  t | I d H ksJ d S )Nr%   rL   c                     s   t   ttt jS rZ   )r4   r   r   r!   r=   tidr=   r>   get_tid_then_reenter  s   z6test_recursive_to_thread.<locals>.get_tid_then_reenter)r%   rL   r   )r~  r=   r|  r>   test_recursive_to_thread  s   r  c                    s  t  d fdd} t  t| I d H  W d    n1 s#w   Y   jr-J  r3J t  t| ddI d H  W d    n1 sKw   Y   jsUJ ttjddI d H rcJ dd
dd fdd}t  t|I d H  W d    n1 sw   Y   jrJ  rJ t  t|ddI d H  W d    n1 sw   Y   jsJ ttjddI d H rJ d fdd}t  tt	|I d H  W d    n1 sw   Y   jsJ d S )Nr%   r&   c                     sF   t  j zt t W n tjy   d Y d S w d d S NTF)r    r   r   r   r   r   r=   )r   queuer=   r>   
sync_check  s   
z3test_from_thread_host_cancelled.<locals>.sync_checkTr   r   timeoutr   c                     s   dS )NTr=   r=   r=   r=   r>   no_checkpoint  rc   z6test_from_thread_host_cancelled.<locals>.no_checkpointc                     sJ   t  j ztsJ W n tjy   d Y d S w d d S r  )r    r   r   r   r   r   r=   r   r  r  r=   r>   async_check  s   
z4test_from_thread_host_cancelled.<locals>.async_checkc                     sD       td t I d H  W d    d S 1 sw   Y  d S )Nr   )r   r   r   r=   r   r=   r>   async_time_bomb  s
   
"z8test_from_thread_host_cancelled.<locals>.async_time_bombrD   ru  )
r   r   r   r   r!   r   
get_nowaitr   r   r   )r  r  r  r=   r  r>   test_from_thread_host_cancelled  s<   
	




	



r  c               	     sr  t  dfdd} d fd	d
g t  t 4 I d H 0}|| dt  t I d H  d dks;J j	dddksEJ  
  W d   I d H  n1 I d H sYw   Y  d dksfJ g t  t }t 4 I d H 2}|| d| t I d H  d dksJ j	dddksJ |   
  W d   I d H  n1 I d H sw   Y  |jsJ dv sJ d dksJ d fdd
g t  t }t 4 I d H (}|| d| t I d H  d dksJ |   
  W d   I d H  n1 I d H sw   Y  |jsJ dv s#J d dks,J j	dddks7J d S )Nr   r   r   r   r%   r&   c              
     s   |3  d z&zt | dI d H W W  d W  d    S  tjy/    d  w  d w 1 s9w   Y  d S )Nr/   r   r   r   )r3   r!   r   r   )r   r   r   r=   r>   r     s   


z/test_from_thread_check_cancelled.<locals>.childc                     sB   zt   W n tjy   d Y nw d    t  S Nr   Not Cancelled)r   r   r   r   r   r=   r~   r   r=   r>   rC     s   

z+test_from_thread_check_cancelled.<locals>.fFr   r/   r   r  r  r   r   r  c                     sB       zt  W n tjy   d Y d S w d d S r  )r   r   r   r   r   r=   r  r=   r>   rC   
  s   
Tr   )r   r   r   r   r%   r&   rD   )r   r   r4   r   r   r   r   r   r#   r   rw   r   r   )r   r   r   r=   )r~   rC   r   r<   r>    test_from_thread_check_cancelled  sV   
(
(
	
*r  c                     s   t t t  W d    n1 sw   Y  t  tt fdd t t  jdd	  W d    d S 1 s@w   Y  d S )Nc                   s
     | S rZ   )r   r{   r   r=   r>   r_   &  s   
 zLtest_from_thread_check_cancelled_raises_in_foreign_threads.<locals>.<lambda>r   r  )
rd   re   rf   r   r   r   r   r"  r   unwrapr=   r=   r   r>   :test_from_thread_check_cancelled_raises_in_foreign_threads"  s   "r  c               
     s   ddd} t d; t 4 I d H }tdD ]}||  qW d   I d H  n1 I d H s0w   Y  W d    d S W d    d S 1 sHw   Y  d S )Nr%   r&   c                     s   	 t ttdddI d H  q)NTr   Fr   )r!   r   r   r=   r=   r=   r>   r   1  s   z+test_reentry_doesnt_deadlock.<locals>.childr      rD   )r   r   r   r   r   )r   r   r|   r=   r=   r>   test_reentry_doesnt_deadlock+  s   

*"r  c                     s   t t ttdddI d H  W d    n1 sw   Y  t t ttdddI d H  W d    d S 1 s;w   Y  d S )NTF)cancellabler   )rd   re   rR   r!   r   r=   r=   r=   r>   #test_cancellable_and_abandon_raises;  s   "r  c                     s   t t ttddI d H  W d    n1 sw   Y  t t ttddI d H  W d    d S 1 s9w   Y  d S )NF)r  T)rd   Zwarnsr   r!   r   r=   r=   r=   r>   test_cancellable_warnsC  s   "r  rD   rZ   )r   r   r%   r   )r   r   r   r   r%   r&   )r   rL   r   r   r   r   r%   r&   )r   r   r%   r&   )e
__future__r   Zcontextvarsr  r   r   ri   r4   r   rp  	functoolsr   typingr   r   r   r   r	   r
   r   r   r   rd   r5  outcomer   r   r   r   r   r   r   r   r   r   r   r   Z_core._tests.test_kir   Z_core._tests.tutilr   r   Z_threadsr   r   r   r    r!   Zlowlevelr"   testingr#   r   rE   r6   rK   r$   r]   rg   ry   r   r   r   r   r   r   r   r   ZmarkZparametrizer	  r  r  r#  r,  r2  Z
ContextVarr3  r   r>  rD  rK  rL  rM  rO  ra  rc  rd  rg  Zskipifro  rr  rw  r{  r  r  r  r  r  r  r  r=   r=   r=   r>   <module>   s    ,,

<

0

&
#
	

<%
p



	

%





+
+
	






;
N	
