
    ,h                       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jj                  e6e7   f   f      Z8 ed      Z9d=dZ:d=dZ;d=dZ<d=dZ=d=dZ>d>d?dZ?d=dZ@d=dZAd=dZBd=dZC	 	 	 	 	 	 d@dZDej                  j                  dg d      ej                  j                  dddg      ej                  j                  dddg      	 	 	 	 	 	 	 	 dAd                      ZGd=d!ZHd=d"ZI	 	 	 	 dBd#ZJd=d$ZKd=d%ZL ej                  d&      ZNd'eOd&<   d=d(ZPd=d)ZQd=d*ZRd=d+ZSd=d,ZTd=d-ZUd=d.ZVd=d/ZWd=d0ZXd=d1ZYd=d2ZZd=d3Z[d=d4Z\d=d5Z]d=d6Z^d=d7Z_d=d8Z`d=d9Zaed=d:       Zbd=d;Zcd=d<Zdy)C    )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Tc                   K   t        j                         	 d
	 	 	 	 	 	 	 	 	 dfd} t        j                         }dd} | t        |d|       d {    dd} | t        |dt
        f|       d {    dd} | t        |d|       d {    dd	} | t        |dt        f|       d {    y 7 e7 E7 +7 w)Nc                   K   g d fd}t        j                  |d      }|j                          |j                         r/t	        d       t        d       d {    |j                         r/d|fdf|gk(  sJ y 7 $w)	Nc                    	 j                  dt        j                         f              } j                  d| f       y # t        $ r1}t	        |       j                  dt        |      f       Y d }~y d }~ww xY w)Nstart
trio_tokengoterror)append	threadingcurrent_threadBaseExceptionprinttype)xexcdo_in_trio_threadfnrecordr(   s     Z/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/trio/_tests/test_threads.pythreadfnz<test_do_in_trio_thread.<locals>.check_case.<locals>.threadfnB   so    4w	(@(@(BCD%b&ZHuaj)  4c
wS	2334s   AA 	B'A<<BT)targetdaemonyawn{Gz?r&   freturnNone)r,   Threadr&   is_aliver/   r   )r3   r4   expectedr(   r7   child_threadr5   trio_threads   `` `  @r6   
check_casez*test_do_in_trio_thread.<locals>.check_case:   s       	4 	4 !''xE##%&M+ ##% 7L1C3ExPPPP s   AB
#B$B
9B
c                z    t        j                         rJ | j                  dt        j                         f       y)Nr<   r
   )r   currently_ki_protectedr+   r,   r-   r5   s    r6   f1z"test_do_in_trio_thread.<locals>.f1T   s.    //11sI44678    )r)   r
   r'   c                    t        j                         rJ | j                  dt        j                         f       t
        Nr<   )r   rG   r+   r,   r-   
ValueErrorrH   s    r6   f2z"test_do_in_trio_thread.<locals>.f2[   s1    //11sI44678rJ   r*   c                   K   t        j                         rJ t        j                          d {    | j                  dt	        j
                         f       y7 *w)Nr<      )r   rG   
checkpointr+   r,   r-   rH   s    r6   f3z"test_do_in_trio_thread.<locals>.f3b   sI     //11   sI44678 	!s   .AA+A)r)   rP   c                   K   t        j                         rJ t        j                          d {    | j                  dt	        j
                         f       t        7 /wrL   )r   rG   rQ   r+   r,   r-   KeyErrorrH   s    r6   f4z"test_do_in_trio_thread.<locals>.f4j   sK     //11   sI44678 	!s   .A"A 0A"N)
r3   zCallable[..., threading.Thread]r4   zCallable[..., T | Awaitable[T]]rB   ztuple[str, T]r(   _core.TrioToken | Noner>   r?   )r5   
RecordTyper>   int)r5   rX   r>   r   )r,   r-   r   current_trio_tokenr   rM   r   rT   )rE   tokenrI   rN   rR   rU   rD   s         @r6   test_do_in_trio_threadr\   7   s     **,K .2	Q:Q+Q  Q +	Q
 
Q0 $$&E
 )2ze
LLL
 )2/DQV
WWW _b*
GGG _b7H*=%
PPP/ M X H QsH   ACB:!C6B<7CB>!C4C 5C<C>C Cc                    K   t        j                  t              5  t        d        d d d        dd} t        j                  t              5  t	        |        d d d        y # 1 sw Y   ;xY w# 1 sw Y   y xY ww)Nc                      y rV    r_   rJ   r6   <lambda>z9test_do_in_trio_thread_from_trio_thread.<locals>.<lambda>u       rJ   c                    K   y wrV   r_   r_   rJ   r6   fooz4test_do_in_trio_thread_from_trio_thread.<locals>.foow   	        r=   )pytestraisesRuntimeErrorr   r   )rc   s    r6   'test_do_in_trio_thread_from_trio_threadri   s   sc     	|	$ +\*+ 
|	$  + + s3   A;A#%A;A/	A;#A,(A;/A84A;c                 b    t               dfd} t        j                  |        ddhk(  sJ y )Nc                 |  K   t        j                         d	dd	fdd	fd} t        j                  |       }|j	                          t        d       |j                         r$t        d       d {    |j                         r$t        d       |j                          t        d       y 7 <w)
Nc                     t        d       t        j                         rJ t        d       	 t                dd l} t        d | j
                                y # dd l} t        d | j
                                w xY w)Nzin Trio threadr   r   finally)r/   r   rG   r   sysexc_info)rn   s    r6   trio_thread_fnzTtest_run_in_trio_thread_ki.<locals>.check_run_in_trio_thread.<locals>.trio_thread_fn   sZ    "#3355)1	i0 i0s   
A !A9c                    K             y wrV   r_   )rp   s   r6   trio_thread_afnzUtest_run_in_trio_thread_ki.<locals>.check_run_in_trio_thread.<locals>.trio_thread_afn   s     s   	c                     	 t        d       t               	 t	               y # t        $ r t        d        j                  d       Y 6w xY w# t        $ r t        d        j                  d       Y y w xY w)Nrunningr'   ok1ok2)r/   r   KeyboardInterruptaddr   )r5   r[   rr   rp   s   r6   external_thread_fnzXtest_run_in_trio_thread_ki.<locals>.check_run_in_trio_thread.<locals>.external_thread_fn   sp    "i $^F"EB	 % "e

5!"
 % "e

5!"s    * A %AA%A=<A=)r8   waitingr;   zwaited, joiningdoner=   )	r   rZ   r,   r@   r&   r/   rA   r   join)ry   threadr[   rr   rp   r5   s     @@@r6   check_run_in_trio_threadz<test_run_in_trio_thread_ki.<locals>.check_run_in_trio_thread   s     ((*		1		" 	" !!);<ioo+ oo f s   A7B<=B:>B<(B<ru   rv   r=   )setr   run)r~   r5   s    @r6   test_run_in_trio_thread_kir   ~   s3     UF%N 
II&'eU^###rJ   c                     g t               dfddfddfd} t        j                  |       }|j                          ddgk(  sJ y )	Nc                    K   j                  d        j                          t        j                  d        d {    y 7 w)Nsleepingc                6    t         j                  j                  S rV   )r   Abort	SUCCEEDED)_s    r6   r`   zMtest_await_in_trio_thread_while_main_exits.<locals>.trio_fn.<locals>.<lambda>   s    EKK4I4I rJ   )r+   r   r   wait_task_rescheduled)evr5   s   r6   trio_fnz;test_await_in_trio_thread_while_main_exits.<locals>.trio_fn   s1     j!
))*IJJJs   ;AAAc                v    	 t        |        y # t        j                  $ r j                  d       Y y w xY w)Nr'   	cancelled)r   r   	Cancelledr+   )r[   r5   r   s    r6   	thread_fnz=test_await_in_trio_thread_while_main_exits.<locals>.thread_fn   s1    	'G6 	'MM+&	's    $88c                    K   t        j                         } t        j                  | f      }|j	                          j                          d {    dgk(  sJ |S 7 w)N)r8   argsr   )r   rZ   r,   r@   r&   wait)r[   r}   r   r5   r   s     r6   mainz8test_await_in_trio_thread_while_main_exits.<locals>.main   sV     ((*!!%Bggi*%% 	s   AA$A"A$r   r   r=   )r[   _core.TrioTokenr>   r?   )r>   threading.Thread)r   r   r   r|   )r   r}   r   r5   r   r   s     @@@@r6   *test_await_in_trio_thread_while_main_exitsr      sJ    F	BK
' YYt_F
KKMj+....rJ   c                 b  K   d} d| z   fdddfdt               d {    t        d        d {    t         d| z                d {    dfd} |d       d {     |d	       d {     |d
       d {     |d       d {    y 7 |7 i7 O7 ;7 -7 7 w)Nz0 from trio._tests.test_threads.test_named_threadinnerc                p    t        j                         j                  | k(  sJ t        j                         S rV   )r,   r-   name)r   s    r6   r   z test_named_thread.<locals>.inner   s+    '')..$66''))rJ   c                    t        |       S rV   r   r   r   s    r6   r<   ztest_named_thread.<locals>.f       ud##rJ   thread_namer?   c                   K   t         |       |        d {   }t        j                  d|j                        sJ y 7 'w)Nr   Trio thread [0-9]*)r   rematchr   )r   r}   r<   s     r6   test_thread_namez+test_named_thread.<locals>.test_thread_name   s9     )!D'tDDxx-v{{;;; Es   AA(A fobiedooname_longer_than_15_characters   💙r   strr>   r   r   r   r>   zCallable[[], threading.Thread])r   r   r>   r?   r   )endingr   r<   r   s     @@r6   test_named_threadr      s     ?F!F* *$ U
###
U
555 Qv/
000< 2

:
&&&
;
<<<
6
""" $5 1 &<"sy    B/B!B/B#B/B%B/+B',B/;B)<B/B+B/B-B/#B/%B/'B/)B/+B/-B/c                z   dd l }dd l}|j                  j                  d      }|sd}	 |j	                  |      }t        |dd       }|sJ |j                  |j                  |j                  g|_        |j                  |_        |j!                  dd      }| t#        j$                         }  || |d      dk(  sJ 	 |j&                  j)                         S # t
        $ r t        dt        j                          Y y w xY w# t*        $ r/}t-        j.                  d	|j&                  d
|        Y d }~y d }~ww xY w)Nr   pthreadzlibc.sozno pthread on pthread_getname_nprJ      )sizezvalue: z, exception: )ctypesctypes.utilutilfind_libraryCDLL	Exceptionr/   rn   platformgetattrc_void_pc_char_pc_size_targtypesc_intrestypecreate_string_bufferr,   	get_identvaluedecodeUnicodeDecodeErrorrf   fail)identr   libpthread_path
libpthreadr   name_bufferes          r6   _get_thread_namer      s8   kk..y9O $[[1

 !-A4H  	#
 "(--c-;K}##%e["5::E  ''))-  s||n-..  Egk//2-sCDDEs)   C =D %C?>C?	D:%D55D:c                 :  K   dddfdd} t         |              d {    t         |       d        d {    ddfd} |d       d {     |d       d {     |d       d {     |d	d
       d {    y 7 i7 P7 ;7 -7 7 w)Nc                    t               }|Mt        j                  dk7  r:t        j                  dt        j                          t        j                         S || d d k(  sJ t        j                         S )Nlinuxzno pthread OS support on    )r   rn   r   rf   skipr,   r-   )r   os_thread_names     r6   r   z#test_named_thread_os.<locals>.inner  sf    )+!cllg&=KK3CLL>BC '')) "T#2Y..''))rJ   c                    t        |       S rV   r   r   s    r6   r<   ztest_named_thread_os.<locals>.f  r   rJ   z4None from trio._tests.test_threads.test_named_threadr   c                   K   || }t         |      |        d {   }t        |j                        }|J d       t        j                  d|      sJ y 7 ;w)Nr   z'should skip earlier if this is the caser   )r   r   r   r   r   )r   rB   r}   r   r<   s       r6   r   z.test_named_thread_os.<locals>.test_thread_name(  s]     H)!H+4HH)&,,7)T+TTxx-~>>>	 Is   AA<Ar   r   r   r   ?)rB   r   r   rV   )r   r   rB   
str | Noner>   r?   r   )defaultr   r<   r   s     @@r6   test_named_thread_osr     s     *$ EG
QwZ
(((
QwZT
:::? 2

:
&&&
;
<<<
6C
000# ): &<0si   !BBB BBBB'B(B7B8B	B
BBBBBBc                     ddl m}   |        }|<t        j                  dk7  sJ t	        j
                  dt        j                          y y )Nr   )get_os_thread_name_funcr   zno pthread_setname_np on )trio._core._thread_cacher   rn   r   rf   r   )r   ks     r6   test_has_pthread_setname_npr   8  s>    @!Ay||w&&/~>? rJ   c                   K   t        j                         } dd}t        |d       d {   \  }}|dk(  sJ || k7  sJ dd}t        j                  t
        d      5 }t        |       d {    d d d        t        j                  j                         |j                  j                  d   | k7  sJ y 7 7 L# 1 sw Y   KxY ww)	Nc                .    | t        j                         fS rV   )r,   r-   )r1   s    r6   r<   z$test_run_in_worker_thread.<locals>.fD  s    9++-..rJ      c                 <    t        t        j                               rV   )rM   r,   r-   r_   rJ   r6   gz$test_run_in_worker_thread.<locals>.gK  s    11344rJ   z1^<Thread\(Trio thread \d+, started daemon \d+\)>$r   r   )r1   r"   r>   ztuple[T, threading.Thread])r>   r   )	r,   r-   r   rf   rg   rM   r/   r   r   )rD   r<   r1   rC   r   excinfos         r6   test_run_in_worker_threadr   A  s     **,K/ /q!44OA|6M;&&5 
B
 $ 
 ###	$
 
'--

==a K/// 5 	$	$ $s:   (C	B94C	B=.B;/B=3AC	;B==CC	c                   K   d gdfddfd} g t        j                         }t        j                         4 d {   }|j	                  | |d       t                d {    dgk(  sJ |j                  j                          d d d       d {    d   dk7  sJ |j                  d        d   dk7  rt        j                  d       d   dk7  rg d d<   t        j                         4 d {   }|j	                  | |d       t                d {    |j                  j                          t        j                  d	      5  t        d
      D ]  }t        j                          d {      	 d d d        dgk(  sJ |j                  d        d d d       d {    t        j                         5 }|j                           | |d       d {    d d d        j                  sJ y 7 7 7 q# 1 d {  7  sw Y   xY w7 %7 7 # 1 sw Y   xY w7 # 1 d {  7  sw Y   xY w7 e# 1 sw Y   dxY ww)Nc                :    dd<   | j                          dd<   y )Nblockingr   finished)get)qregisters    r6   r<   z1test_run_in_worker_thread_cancellation.<locals>.fZ  s     	 rJ   c                   K   j                  d       	 t        | |       d {   j                  d       S 7 # j                  d       w xY ww)Nr&   abandon_on_cancelexit)r+   r   )r   r   r<   r5   s     r6   childz5test_run_in_worker_thread_cancellation.<locals>.child`  sG     g	"+AqDUVVMM&! WMM&!s%   A? =? A? AATr&   r   r   r;   Fshield
   )r   stdlib_queue.Queue[None]r>   r?   )r   r   r   boolr>   r?   )stdlib_queueQueuer   open_nursery
start_soonr   cancel_scopecancelputtimer   r   rangerQ   cancelled_caught)r   r   nurseryr   scoper<   r5   r   s        @@@r6   &test_run_in_worker_thread_cancellationr  W  s0    "&H!" F"."4"4"6A!!# & &w5!T* %&&&'""##%& & A;*$$EE$K
1+
#

4 1+
# FHQK!!# 	 	w5!U+$&&&##%d+ 	)2Y )&&((()	) '""	d	 	 
			 Auo !!!!I&
 	'& & & &&	& )	) 	)		 	 	 	  	 s  ?IHI!H)H	*&HIHAI!I H%I!H;%H(&4H;&H- H+
H-!H;)I4H95II*I+I/I	HIH"HH"	I(H;+H--H6	2H;9I;III	IIIIc                   |j                  t        j                  dd       t        j                         t        j                         dfddfd}t        j
                  |       j                  d        j                         }|j                         r&t        j                  d       |j                         r&| j                         \  }}d|vsJ d|vsJ y )NIDLE_TIMEOUTr;   c                 l     j                          j                  t        j                                y rV   )r   r   r,   r-   )q1q2s   r6   r   z6test_run_in_worker_thread_abandoned.<locals>.thread_fn  s!    

y'')*rJ   c                 $  K   dfd} t        j                         4 d {   }|j                  |        t                d {    |j                  j                          d d d       d {    y 7 R7 17 	# 1 d {  7  sw Y   y xY ww)Nc                 <   K   t         d       d {    y 7 w)NTr   r   )r   s   r6   r   z@test_run_in_worker_thread_abandoned.<locals>.main.<locals>.child  s     $Y$GGGs   r=   )r   r   r   r   r   r   )r   r  r   s     r6   r   z1test_run_in_worker_thread_abandoned.<locals>.main  s|     	H %%' 	* 	*7u%(***  '')	* 	* 	**	* 	* 	* 	*sV   BA5BA;A7A;$B/A90B7A;9B;BBB	BzException in threadr=   )setattrr   _thread_cacher   r   r   r   r   rA   r   r   
readouterr)	capfdmonkeypatchr   r}   outerrr  r	  r   s	         @@@r6   #test_run_in_worker_thread_abandonedr    s     ++^TB#/#5#5#7B/;/A/A/CB+* 
IIdOFF4L VVXF
//


4 //
 !HC ++ +++rJ   MAX)rP      r   r   FTuse_default_limiterc                4  
K   d| z  }t        j                         
t        j                         |r t               }|j                  }| |_        d nt        |       }| }|	  G d d      d_        d_        d_        d_	        t        j                         d
fddfd}t        j                         4 d {   }t        d       g }t        |      D ]B  }	|j                  t                      |j!                  ||d          t#                d {    D r8t        d	       |d   j%                          d {    t#                d {    j                  | k7  s|j'                         j(                  | k7  rAt+        d
       d {    j                  | k7  r#|j'                         j(                  | k7  rA
j-                          d d d       d {    j                  | k(  sJ r2|j.                  dkD  r#t+        d
       d {    |j.                  dkD  r#j                  |k(  sJ j                  dk(  sJ 	 ||_        y 7 7 A7 7 7 7 # 1 d {  7  sw Y   xY w7 d# ||_        w xY ww)Nr
   c                  6    e Zd ZU ded<   ded<   ded<   ded<   y)0test_run_in_worker_thread_limiter.<locals>.staterY   ran
high_waterrt   parkedN)__name__
__module____qualname____annotations__r_   rJ   r6   stater    s    HOLKrJ   r!  r   c                   t        d       t        | j                         5  xj                  dz  c_        xj                  dz  c_        t        j                  j                        _        xj                  dz  c_        d d d        j                          5  xj                  dz  c_        xj                  dz  c_        d d d        t        d       y # 1 sw Y   ZxY w# 1 sw Y   !xY w)Nzthread_fn startr'   r   zthread_fn exiting)	r/   r   r   r  rt   maxr  r  r   )r   gatelockr!  r[   s    r6   r   z4test_run_in_worker_thread_limiter.<locals>.thread_fn  s    #$ !4!4G "		Q	"#&u'7'7#G  !" IIK #!"# %&" "# #s   A%C$%+C0$C-0C9c                   K   t        j                         5 }t        |       d {    d d d        t        dj                         | j                          y 7 3# 1 sw Y   2xY ww)N)r   limiterzrun_thread finished, cancelled:)r   r   r   r/   r  r   )eventr   r   limiter_argr   s     r6   
run_threadz5test_run_in_worker_thread_limiter.<locals>.run_thread  si     ""$ ( &,'	   3\5R5RSIIK s+   A-A!AA!/A-A!!A*&A-spawningz'waiting for first cancellation to clearr;   )r   r   r>   r?   )r(  r   r>   r?   )r,   r   Lockr   total_tokensr   r  r  rt   r  r   rZ   r   r/   r   r+   r   r   r   
statisticstasks_waitingr   r   borrowed_tokens)r  r   r  COUNTcorig_total_tokensr*  r  eventsr   r$  r)  r%  r!  r   r[   s    `        @@@@@@r6   !test_run_in_worker_thread_limiterr6    sV     GE??D>>D*,NNC [+	 	 	((*	' 	' 		 %%' 	 	7*F5\ /eg&"":vbz:,.../ ?@Qinn&&&,... #q||~'C'Cs'JDk!! #q||~'C'Cs'J HHJ5	 	8 3&& ##a'Dk!! ##a' yyE!!}}!!!*Q	 / '. "1	 	 	 	F "
 +s   AJ)A#J I%J AI5'I(()I5I+I5#I.$>I5"I1#I57I5I5%J 0I314J %J
&J :"J J%J (I5+I5.I51I53J 5J;I><JJ 	JJc                 v   K   g  G fdd      } t        d  |               d {    ddgk(  sJ y 7 w)Nc                  &    e Zd Zd fdZd fdZy)?test_run_in_worker_thread_custom_limiter.<locals>.CustomLimiterc                >   K   j                  d       || _        y wNacquirer+   	_borrowerselfborrowerr5   s     r6   acquire_on_behalf_ofzTtest_run_in_worker_thread_custom_limiter.<locals>.CustomLimiter.acquire_on_behalf_of6  s     MM)$%DNs   c                J    j                  d       || j                  k(  sJ y )Nreleaser=  r?  s     r6   release_on_behalf_ofzTtest_run_in_worker_thread_custom_limiter.<locals>.CustomLimiter.release_on_behalf_of:  s!    MM)$t~~---rJ   NrA  r!   r>   r?   r  r  r  rB  rE  rH   s   r6   CustomLimiterr9  5  s    	&	.rJ   rH  c                      y rV   r_   r_   rJ   r6   r`   z:test_run_in_worker_thread_custom_limiter.<locals>.<lambda>@  ra   rJ   r'  r<  rD  r   )rH  r5   s    @r6   (test_run_in_worker_thread_custom_limiterrK  0  sE      F. . \=?
CCCi++++ Ds   %979c                   K   g  G fdd      }  |        }t        j                  t        d      5 }t        d |       d {    d d d        j                  j
                  J ddgk(  sJ g i t        j                  t        d      5 }t        fd	|       d {    d d d        t        |j                  j
                  t              sJ ddgk(  sJ y 7 # 1 sw Y   xY w7 J# 1 sw Y   IxY ww)
Nc                  &    e Zd Zd fdZd fdZy)Ctest_run_in_worker_thread_limiter_error.<locals>.BadCapacityLimiterc                0   K   j                  d       y wr;  )r+   r?  s     r6   rB  zXtest_run_in_worker_thread_limiter_error.<locals>.BadCapacityLimiter.acquire_on_behalf_ofH  s     MM)$s   c                <    j                  d       t        d      )NrD  zrelease on behalf)r+   rM   r?  s     r6   rE  zXtest_run_in_worker_thread_limiter_error.<locals>.BadCapacityLimiter.release_on_behalf_ofK  s    MM)$011rJ   NrF  )rA  r!   r>   r   rG  rH   s   r6   BadCapacityLimiterrN  G  s    	%	2rJ   rQ  z^release on behalf$r   c                      y rV   r_   r_   rJ   r6   r`   z9test_run_in_worker_thread_limiter_error.<locals>.<lambda>R  ra   rJ   rJ  r<  rD  c                      d   S )Nr1   r_   )ds   r6   r`   z9test_run_in_worker_thread_limiter_error.<locals>.<lambda>[  s    3 rJ   )rf   rg   rM   r   r   __context__
isinstancerT   )rQ  bsr   rT  r5   s      @@r6   'test_run_in_worker_thread_limiter_errorrX  D  s     F2 2 
	B	z)?	@ ;G r:::;==$$,,i+++F A	z)?	@ =G <<<=gmm//::i++++ 	;; ; 	== =sY   1DC'C%C'ADC5(C3)C5-8D%C''C0,D3C55C>:Dc                |  K   dd}| j                  t        j                  j                  d|       t	               }|j
                  dk(  sJ t        j                  t              5 }t        d        d {    d d d        dt        j                        v sJ |j
                  dk(  sJ y 7 7# 1 sw Y   6xY ww)Nc                    t        d      )Nz!the engines canna take it captain)rh   )r@  r   s     r6   	bad_startz:test_run_in_worker_thread_fail_to_spawn.<locals>.bad_startd  s    >??rJ   start_thread_soonr   c                      y rV   r_   r_   rJ   r6   r`   z9test_run_in_worker_thread_fail_to_spawn.<locals>.<lambda>n  ra   rJ   engines)r@  objectr   r_  r>   r   )r  r   r  ThreadCacher   r1  rf   rg   rh   r   r   r   )r  r[  r'  r   s       r6   'test_run_in_worker_thread_fail_to_spawnra  `  s     @ ++779LiX,.G""a'' 
|	$ / .../GMM***""a''' 	// /s0   A$B<&B06B.7B0;3B<.B00B95B<c                 t   K   dd} t        j                         }t        |        d {   }||k(  sJ y 7 w)Nc                 8    t        t        j                        } | S rV   r   r   rZ   callee_tokens    r6   r   z5test_trio_to_thread_run_sync_token.<locals>.thread_fnw      +E,D,DErJ   r>   r   r   rZ   r   r   caller_tokenrf  s      r6   "test_trio_to_thread_run_sync_tokenrl  t  ;      ++-L+I66L<''' 7   '868c                    K   dd} t        j                  t        d      5  t        |        d {    d d d        y 7 # 1 sw Y   y xY ww)Nc                    K   y wrV   r_   r_   rJ   r6   async_fnz=test_trio_to_thread_run_sync_expected_error.<locals>.async_fn  rd   re   zexpected a sync functionr   r=   )rf   rg   	TypeErrorr   rq  s    r6   +test_trio_to_thread_run_sync_expected_errorrt    sG      
y(B	C + ***+ +*+ +s)    AA?A	AAA
Atrio_test_contextvarzcontextvars.ContextVar[str]c                 z  K   t        j                         } t        j                  d       dd}t	        |       d {   \  }}|dk(  sJ || k7  sJ dd}t	        |       d {   \  }}}t        j                         }|dk(  sJ |dk(  sJ |dk(  sJ d       t        j                         dk(  sJ y 7 x7 Rw)	Nr   c                     t         j                         } t        j                  t        j
                        5  t	        j                          d d d        | t        j                         fS # 1 sw Y   xY wrV   )	ru  r   rf   rg   sniffioAsyncLibraryNotFoundErrorcurrent_async_libraryr,   r-   )r   s    r6   r<   z3test_trio_to_thread_run_sync_contextvars.<locals>.f  sW    $((*]]7<<= 	,))+	,y//122	, 	,s   A++A4c                 B   t         j                         } t         j                  d       t         j                         }t        j                  t
        j                        5  t        j                          d d d        | |t        j                         fS # 1 sw Y    xY w)Nworker)
ru  r   r   rf   rg   rx  ry  rz  r,   r-   )parent_valueinner_values     r6   r   z3test_trio_to_thread_run_sync_contextvars.<locals>.g  s}    +//1  **..0]]7<<= 	,))+	, $$&
 	
	, 	,s   !BBr|  zRThe contextvar value set on the worker would not propagate back to the main threadtrio)r>   ztuple[str, threading.Thread])r>   z!tuple[str, str, threading.Thread])r,   r-   ru  r   r   r   rx  rz  )rD   r<   r   rC   r   r}  r~  current_values           r6   (test_trio_to_thread_run_sync_contextvarsr    s     **,KV$3 !31 55E<F?;&&

 5Gq4I.I+L+|(,,.M6!!(""F" 	 ((*f4441 6  /Js"   <B;B7'B;&B9'AB;9B;c                   K   dd} t        |        d {   }t        |t              sJ dddfd}t        j                  t
        d      5  t        |       d {    d d d        y 7 X7 # 1 sw Y   y xY ww)Nc                 8    t        t        j                        } | S rV   )r   r   current_time)	trio_times    r6   thread_fn_1z3test_trio_from_thread_run_sync.<locals>.thread_fn_1  s    (););<	rJ   c                    K   y wrV   r_   r_   rJ   r6   rq  z0test_trio_from_thread_run_sync.<locals>.async_fn  rd   re   c                     t                y rV   )r   rs  s   r6   thread_fn_2z3test_trio_from_thread_run_sync.<locals>.thread_fn_2  s    X&rJ   zexpected a synchronous functionr   )r>   floatr=   )r   rV  r  rf   rg   rr  )r  r  r  rq  s      @r6   test_trio_from_thread_run_syncr    sv      )55Ii''' 
y(I	J . ---. . 6 	.. .s9   A?A/;A?A3!A1"A3&
A?1A33A<8A?c                   K   g dfddfd} t        |        d {    ddgk(  sJ dd}t        j                  t        d      5  t        t        |       d {    d d d        y 7 N7 # 1 sw Y   y xY ww)	Nc                 X   K   t        j                           j                  d       y w)Nback in trio)r   r  r+   rH   s   r6   back_in_trio_fnz2test_trio_from_thread_run.<locals>.back_in_trio_fn  s     n%s   '*c                 >    j                  d       t                y )N	in thread)r+   r   )r  r5   s   r6   r   z,test_trio_from_thread_run.<locals>.thread_fn  s    k"(rJ   r  r  c                      y rV   r_   r_   rJ   r6   sync_fnz*test_trio_from_thread_run.<locals>.sync_fn  s    rJ   zappears to be synchronousr   r=   )r   rf   rg   rr  r   )r   r  r  r5   s     @@r6   test_trio_from_thread_runr    s      F&) Y
'''k>222 
y(C	D ; ':::; ; ( 	;; ;s9   BA1,BA5#A3$A5(
B3A55A>:Bc                 t   K   dd} t        j                         }t        |        d {   }||k(  sJ y 7 w)Nc                 8    t        t        j                        } | S rV   rd  re  s    r6   r   z.test_trio_from_thread_token.<locals>.thread_fn  rg  rJ   rh  ri  rj  s      r6   test_trio_from_thread_tokenr    rm  rn  c                 v   K   dd} t        j                         }t        | |       d {   }||k(  sJ y 7 w)Nc                <    t        t        j                  |       }|S )Nr'   rd  )r[   rf  s     r6   r   z4test_trio_from_thread_token_kwarg.<locals>.thread_fn  s    +E,D,DQVWrJ   )r[   r   r>   r   ri  rj  s      r6   !test_trio_from_thread_token_kwargr    s>      ++-L+I|DDL<''' Es   (979c                    K   t        j                  t              5  t        t        j
                         d d d        y # 1 sw Y   y xY wwrV   rf   rg   rh   r   r   r  r_   rJ   r6   test_from_thread_no_tokenr    s6      
|	$ 1U//01 1 1s   A?	AAAc                 (  K   t         j                  d       dd} t        |        d {   \  }}}}}t         j                         }||cxk(  rdk(  sJ  J ||cxk(  r|cxk(  rdk(  sJ  J t	        j
                         dk(  sJ |dk(  sJ y 7 ew)Nr   c                    t         j                         } t         j                  d       t         j                         }t        j                  t
        j                        5  t        j                          d d d        dd}t        |      \  }}t         j                         }t        j                  t
        j                        5  t        j                          d d d        | ||||fS # 1 sw Y   vxY w# 1 sw Y   xY w)Nr|  c                     t         j                         } t         j                  d       t         j                         }t        j                         dk(  sJ | |fS Nback_in_mainr  ru  r   r   rx  rz  back_parent_valueback_current_values     r6   r  zStest_trio_from_thread_run_sync_contextvars.<locals>.thread_fn.<locals>.back_in_main
  sN     4 8 8 : $$^4!5!9!9!;002f<<$&888rJ   r>   ztuple[str, str])	ru  r   r   rf   rg   rx  ry  rz  r   )thread_parent_valuethread_current_valuer  r  r  thread_after_values         r6   r   z=test_trio_from_thread_run_sync_contextvars.<locals>.thread_fn  s    2668  *3779]]7<<= 	,))+	,	9 1E\0R--1557]]7<<= 	,))+	,   
 	
	, 	,	, 	,   !C+C7+C47D r|  r  r  r>   ztuple[str, str, str, str, str]ru  r   r   r   rx  rz  r   r  r  r  r  r  r  s          r6   *test_trio_from_thread_run_sync_contextvarsr     s     V$
@ !++(,,.M/969999#4V8JVhVVVV((*f44/// 	,   (BBA&Bc                 (  K   t         j                  d       dd} t        |        d {   \  }}}}}t         j                         }||cxk(  rdk(  sJ  J ||cxk(  r|cxk(  rdk(  sJ  J |dk(  sJ t	        j
                         dk(  sJ y 7 ew)Nr   c                    t         j                         } t         j                  d       t         j                         }t        j                  t
        j                        5  t        j                          d d d        dd}t        |      \  }}t         j                         }t        j                  t
        j                        5  t        j                          d d d        | ||||fS # 1 sw Y   vxY w# 1 sw Y   xY w)Nr|  c                    K   t         j                         } t         j                  d       t         j                         }t        j                         dk(  sJ | |fS wr  r  r  s     r6   async_back_in_mainzTtest_trio_from_thread_run_contextvars.<locals>.thread_fn.<locals>.async_back_in_main5  sR      4 8 8 : $$^4!5!9!9!;002f<<$&888s   AAr  )	ru  r   r   rf   rg   rx  ry  rz  r   )r  r  r  r  r  r  s         r6   r   z8test_trio_from_thread_run_contextvars.<locals>.thread_fn.  s    2668  *3779]]7<<= 	,))+	,	9 1@@R0S--1557]]7<<= 	,))+	,   
 	
	, 	,	, 	,r  r|  r  r  r  r  r  s          r6   %test_trio_from_thread_run_contextvarsr  +  s     V$
@ !++(,,.M/969999#4V8JVhVVVV//((*f444 	,r  c                     t        j                  t              5  t        t        j
                  d       d d d        y # 1 sw Y   y xY w)NzNot TrioTokentyper'   r  r_   rJ   r6   3test_run_fn_as_system_task_caught_badly_typed_tokenr  V  s7    	|	$ 
*	

 
 
s	   ?Ac                    K   dd} t        j                         }t        j                  t              5  t        | |       d d d        y # 1 sw Y   y xY ww)Nc                     t               rV   )AssertionErrorr_   rJ   r6   
not_calledz7test_from_thread_inside_trio_thread.<locals>.not_called_  s    rJ   r'   r=   )r   rZ   rf   rg   rh   r   )r  r(   s     r6   #test_from_thread_inside_trio_threadr  ^  sI      ))+J	|	$ @ZJ?@ @ @s   2AA	AAAc                     g g dfddfd} t        j                  | d       t        j                  | d       ddgk(  sJ y )	Nc               h  K   	 d  t        j                  d      5  	 t        t        t        t
        d|              d {    j                  d       d d d        y 7 # t         j                  $ r j                  d       Y 1w xY w# 1 sw Y   y xY w# t        j                  d      5  	 t        t        t        t
        d|              d {  7   j                  d       n*# t         j                  $ r j                  d       Y nw xY wd d d        w # 1 sw Y   w xY wxY ww)NTr   r   r'   cleanr   )r   r   r   r   r   r   r+   RunFinishedError)r[   r5   s    r6   agenz2test_from_thread_run_during_shutdown.<locals>.agenk  s	    	+""$/ ++,eL   MM'*+ + -- .MM*-.+ +""$/ ++,eL   MM'* -- .MM*-.+ + +s   D2B D2B#A&A$A&
B	D2$A&&$B
BBBBD2D/3D#5#C0C
C0D#0$D	D#D	D#	D/#D,(D//D2c                   K   j                   | rt        j                         nd              d   j                  d        d {    y 7 w)Nr,  )r+   r   rZ   asend)use_system_taskr  saves    r6   r   z2test_from_thread_run_during_shutdown.<locals>.mainy  s;     D113DQR2hnnT"""s   AAA	ATFr   r  )r[   rW   r>   zAsyncGenerator[None, None])r  r   r>   r?   )r   r   )r   r  r5   r  s    @@@r6   $test_from_thread_run_during_shutdownr  g  sH    DF+# 
IIdD	IIdEj'****rJ   c                    K   t        j                         } t        | t         j                        sJ t	        j
                  |       }|  |       u sJ y wrV   )r   rZ   rV  	TrioTokenweakrefref)r[   weak_references     r6   "test_trio_token_weak_referenceabler    sD     $$&EeU__--[['NN$$$$s   AAc                    K    G d d      } t        j                  t              5  t        t         |               d {    d d d        y 7 # 1 sw Y   y xY ww)Nc                      e Zd ZddZy)4test_unsafe_abandon_on_cancel_kwarg.<locals>.BadBoolc                    t         rV   )NotImplementedError)r@  s    r6   __bool__z=test_unsafe_abandon_on_cancel_kwarg.<locals>.BadBool.__bool__  s    %%rJ   Nr>   r   )r  r  r  r  r_   rJ   r6   BadBoolr    s    	&rJ   r  r   )rf   rg   r  r   rY   )r  s    r6   #test_unsafe_abandon_on_cancel_kwargr    sS     & & 
*	+ C 	BBBC CBC Cs.   $AA AA	AAAAc                    K   t        j                         } dd}| t        t        t         j                         d {   u sJ | t        t        |       d {   u sJ y 7 $7 
w)Nc                 2   K   t        j                         S wrV   )r   current_taskr_   rJ   r6   async_current_taskz8test_from_thread_reuses_task.<locals>.async_current_task  s     !!##s   )r>   z
_core.Task)r   r  r   r   r   )taskr  s     r6   test_from_thread_reuses_taskr    s\     D$ +,@%BTBTUUUU+O=OPPPPP VPs!   ;A&A"A&A$	A&$A&c                 R   K   d dfd} t        |        d {   k7  sJ y 7 	w)Nc                 h    t        j                          t        t        t         j                        S rV   )r,   r   r   r   )tids   r6   get_tid_then_reenterz6test_recursive_to_thread.<locals>.get_tid_then_reenter  s%    !!#193F3FGGrJ   )r>   rY   r   )r  r  s    @r6   test_recursive_to_threadr    s/     
CH
 *+?@@@@@@s   '%
'c                 |  K   t        j                         d	fd} t        j                         5 t	        |        d {    d d d        j
                  rJ j                         rJ t        j                         5 t	        | d       d {    d d d        j
                  sJ t	        t        j                  d             d {   rJ d
dd	fd}t        j                         5 t	        |       d {    d d d        j
                  rJ j                         rJ t        j                         5 t	        |d       d {    d d d        j
                  sJ t	        t        j                  d             d {   rJ d	fd}t        j                         5 t	        t        |       d {    d d d        j
                  sJ y 7 # 1 sw Y   xY w7 e# 1 sw Y   exY w7 77 # 1 sw Y   xY w7 # 1 sw Y   xY w7 7 ^# 1 sw Y   ]xY ww)Nc                     t         j                         	 t        t               j                  d       y # t        j
                  $ r j                  d       Y y w xY wNFT)r   r   r   r   r   r   )r   queues   r6   
sync_checkz3test_from_thread_host_cancelled.<locals>.sync_check  sL    \001	 & IIe  	IIdO	s   9 $A A Tr   r   timeoutc                    K   ywNTr_   r_   rJ   r6   no_checkpointz6test_from_thread_host_cancelled.<locals>.no_checkpoint  s	     re   c                     t         j                         	 t              sJ 	 j                  d       y # t        j
                  $ r j                  d       Y y w xY wr  )r   r   r   r   r   r   )r   r  r  s   r6   async_checkz4test_from_thread_host_cancelled.<locals>.async_check  sR    \001	"=111 IIe  	IIdO	s   8 $AAc                    K    j                          t        d      5  t                d {    d d d        y 7 # 1 sw Y   y xY ww)Nr   )r   r   r   )r   s   r6   async_time_bombz8test_from_thread_host_cancelled.<locals>.async_time_bomb  sB     ^ 	"/!!	" 	"!	" 	"s%   A	=;=	A	=AA	r=   r  )
r   r   r   r   r   r  
get_nowaitr   r   r   )r  r  r  r   r  r  s      @@@r6   test_from_thread_host_cancelledr    s    &2&8&8&:E 
			 - ,,,- ,,,!!				 E tDDDE ((('		1(EFFFF 
			 . ---. ,,,!!				 F EEEF ((('		1(EFFFF"
 
			 C /BBBC ((((W 	-- - 	EE E G 	.. . 	FF F G 	CC Cs   0H<G.G+G.	<H<G>G;G>9H<H&H<;H
HH<H<H HH !9H<H, H<;H0H.H0H<+G..G83H<;G>>H	H<HHH<H  H)%H<.H00H95H<c                   K   t        j                         dfd} dfdg t        j                         t	        j
                         4 d {   }|j                  | dt	        j                                t                d {    d   dk(  sJ j                  d      dk(  sJ j                          d d d       d {    d   d	k(  sJ g t        j                         t	        j                         }t	        j
                         4 d {   }|j                  | d|       t                d {    d   dk(  sJ j                  d      dk(  sJ |j                          j                          d d d       d {    |j                  sJ d
v sJ d   d	k(  sJ dfdg t        j                         t	        j                         }t	        j
                         4 d {   }|j                  | d|       t                d {    d   dk(  sJ |j                          j                          d d d       d {    |j                  sJ d
v sJ d   d	k(  sJ j                  d      dk(  sJ y 7 E7 7 # 1 d {  7  sw Y   xY w7 7 y7 +# 1 d {  7  sw Y   <xY w7 7 7 |# 1 d {  7  sw Y   xY ww)Nc                .  K   |5  j                  d       	 t        |        d {   j                  d       cd d d        S 7 # t        j                  $ r j                  d        w xY w# j                  d       w xY w# 1 sw Y   y xY ww)Nr&   r   r   r   )r+   r   r   r   )r   r  r<   r5   s     r6   r   z/test_from_thread_check_cancelled.<locals>.child  s      	&MM'"&/EVWW
 f%	& 	& X?? h' f%	& 	&sM   BB	A
AA
B	
BA

&A00A33BB		BBc                     	 t                j                  d        j	                          t               S # t        j                  $ r j                  d       Y @w xY wNNot Cancelledr   )r   r   r   r   r   r   r   s   r6   r<   z+test_from_thread_check_cancelled.<locals>.f  sO    	#') EE/"
	*,,  	EE+	s   
8 $AAFr   r&   r   r  r  r   r   r,  c                      j                          	 t                j                  d       y # t        j                  $ r j                  d       Y y w xY wr  )r   r   r   r   r   r  s   r6   r<   z+test_from_thread_check_cancelled.<locals>.f  sF    
		#') EE/"  	EE+	s   
/ $AATr   )r   r   r  r   r>   r?   r=   )r   r   r,   r   r   r   r   r   r   r   r   r   r  )r   r  r  r   r<   r   r5   s      @@@@r6    test_from_thread_check_cancelledr    s    !-!3!3!5A	&- F		B!!#  w5%):):)<=$&&&ayG##uuQu?22
  !9 F		BE!!#  w5%/$&&&ayG##uuQu?22
  !!!v":# F		BE!!#  w5$.$&&&ayG##
  !!!v":555{***a&   &   2&   s  AK5JK53J%J5J%K5J"AK5 J;!K5$!KJ>AKK5KA+K5KK5!K 'K(.K K5!K";K5J%"K5%J8+J.,J83	K5>KK5K
KK	K5K K5 K2&K)'K2.K5c                 t    t        j                  t              5  t                d d d        t	        j
                          t        j                  t         fd       t        j                  t              5   j                  d      j                          d d d        y # 1 sw Y   }xY w# 1 sw Y   y xY w)Nc                &    j                  |       S rV   )r   )r   r   s    r6   r`   zLtest_from_thread_check_cancelled_raises_in_foreign_threads.<locals>.<lambda>6  s    1558 rJ   r   r  )
rf   rg   rh   r   r   r   r   r\  r   unwrap)r   s   @r6   :test_from_thread_check_cancelled_raises_in_foreign_threadsr  2  s    	|	$ &#%&-9-?-?-AA	79KL	|	$ "	a!" "	& &" "s   B"8!B."B+.B7c                 "  K   dd} t        d      5  t        j                         4 d {   }t        d      D ]  }|j	                  |         d d d       d {    d d d        y 7 >7 # 1 d {  7  sw Y   xY w# 1 sw Y   y xY ww)Nc                 P   K   	 t        t        t        dd       d {     7 w)Nr   Fr   )r   r   r   r_   rJ   r6   r   z+test_reentry_doesnt_deadlock.<locals>.childA  s&     $_eQRWXXX Xs   &$&r
      r=   )r   r   r   r   r   )r   r  r   s      r6   test_reentry_doesnt_deadlockr   ;  s     Y 
q	 *%%' 	* 	*71X *""5)*	* 	** *	* 	* 	* 	* 	** *sa   BBA*B"A.BA,B!	B*B,B.B 	4A75B 	<BBBc                 ,  K   dt               } t               }t               }t               }dd}dfd}t        j                         4 d {   }|j                  || |       |j                  |||       t	                d {    |j                  |       t	                d {    rJ t               dk(  sJ | j                          |j                          d {    t	                d {    rJ t               dk(  sJ |j                          |j                          d {    t	                d {    sJ t               dk(  sJ d d d       d {    y 7 7 7 7 7 ~7 E7 57 # 1 d {  7  sw Y   y xY ww)	NFc                d    K   d fd}t        |       d {    |j                          y 7 w)Nc                 0    t         j                         y rV   )r   r   )r   s   r6   r}   zCtest_wait_all_threads_completed.<locals>.wait_event.<locals>.threadT  s    AFF#rJ   r=   )r   r   )r   e_exitr}   s   `  r6   
wait_eventz3test_wait_all_threads_completed.<locals>.wait_eventS  s*     	$ !(((

 	)s   0.0c                 :   K   t                d {    d y 7 wr  )r   )no_threads_lefts   r6   wait_no_threads_leftz=test_wait_all_threads_completed.<locals>.wait_no_threads_leftZ  s     (*** 	+s   r
   r   r   )r   r   r  r   r>   r?   r=   )r   r   r   r   r   r   r   r   )e1e2	e1_exited	e2_exitedr  r  r  r  s          @r6   test_wait_all_threads_completedr  K  s`    O	B	BII
 !!# * *w:r95:r95$&&&/0$&&&"""$))
nn$&&&"""$))
nn$&&&"$))'* * * 	'&
 	&
 	&#* * * *s   AFE.F4E?E1	"E?+E3,:E?&E5'E?8E79:E?3E94E?E;E?F(E=)F1E?3E?5E?7E?9E?;E?=F?FFFFc                 R   K   t                d {    t               dk(  sJ y 7 w)Nr   )r   r   r_   rJ   r6   *test_wait_all_threads_completed_no_threadsr  u  s(     
$
&&& A%%% 's   '%'r=   rV   )r   z
int | Noner>   r   )r  zpytest.CaptureFixture[str]r  pytest.MonkeyPatchr>   r?   )r  rY   r   r   r  r   r>   r?   )r  r  r>   r?   )e
__future__r   contextvarsr  r   r   rn   r,   r   r  	functoolsr   typingr   r   r   r	   rf   rx  r   r   r   r   r   r   r   r   r   _core._tests.test_kir   _core._tests.tutilr   _threadsr   r   r   r   r   r   r   testingr   collections.abcr   r   r   outcomer    lowlevelr!   listtupler   r@   r0   r.   rX   r"   r\   ri   r   r   r   r   r   r   r   r  r  markparametrizer6  rK  rX  ra  rl  rt  
ContextVarru  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r_   rJ   r6   <module>r!     sP   "   	 
       	 	 	 + %   -CC%U9#3#3T-5H#HIIJK
CL9Qx-$`/8#:'E\ 1F@0,6"x#,%#,##, 
#,L 
+E4=1.>q+	q+q+ q+ 
	q+ ? 2 ,q+h,(,8(#(	((	(+ 5KK4J4J5 1 
"5J.*;0	(	(1(0V(5V
@+6%CQA8)vK+\" * *'*T&rJ   