
    i{                         d dl mZ  ej                     d dlZ	 d dlZn# e$ r	 d dl mZ Y nw xY wd dlZd dlZd dl	Z	d dl
Z
dZd Z G d de
j                  Zedk    r e
j                     dS dS )    )monkeyN)contextvarsc                 F     t          j                    fd            }|S )z*Needed to make reftracking test mode work.c                  L    t          j                    } |j        g| R i |S Nr   Contextrun)argskwargsctxfuncs      I/usr/local/lib/python3.11/dist-packages/gevent/tests/test__contextvars.pywrapperz!isolated_context.<locals>.wrapper   s3    !##swt-d---f---    )	functoolswraps)r   r   s   ` r   isolated_contextr      s8    _T. . . . . Nr   c                   4   e Zd Z eej        d          sej        j        Zd Ze	d             Z
d Zd Zd Zd Zd Ze	d	             Zd
 Zd Zd Ze	d             Ze	d             Ze	d             Ze	d             Ze	d             Zd Ze	d             ZdS )ContextTestassertRaisesRegexc                    |                      t                    5  t          j                     d d d            n# 1 swxY w Y   t          j        d          }|                     |j        d           |                      t                    5  d|_        d d d            n# 1 swxY w Y   |                     t          |          t          d                     d S )Naaabbb)	assertRaises	TypeErrorr   
ContextVarassertEqualnameAttributeErrorassertNotEqualhash)selfcs     r   test_context_var_new_1z"ContextTest.test_context_var_new_1*   s>   y)) 	% 	%"$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% "5))'''~.. 	 	AF	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	DGGT%[[11111s   ;??B##B'*B'c                    t          j        d          }|                     dt          |                     t          j        dd          }|                     dt          |                     g }t          j        d|          }|                    |           |                     dt          |                     |                     dt          |                     |                    d          }|                     t          |          t          |                     |                     dt          |                     |                    |           |                     dt          |                     d S )Na{   default123z...   z used )r   r   assertInreprappendsetassertNotInreset)r#   r$   lstts       r   test_context_var_repr_1z#ContextTest.test_context_var_repr_1:   s2   "3''c477###"3444eT!WW%%%"3444

1eT!WW%%%eT#YY'''EE!HHd1ggtAww'''477+++	


hQ(((((r   c                    |                      t                    5  t          j        d           d d d            n# 1 swxY w Y   |                      t                    5  t          j        dd           d d d            n# 1 swxY w Y   |                      t                    5  t          j        d           d d d            n# 1 swxY w Y   t          j        di i  d S )Nr,   r'    )r   r   r   r	   )r#   s    r   test_context_new_1zContextTest.test_context_new_1]   s   y)) 	# 	#"""	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#y)) 	( 	(Q''''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(y)) 	% 	%!$$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%!!b!!!!!s3   <A A !BBB)CCCc                    t          j                    }|                     t          d          5  |d          d d d            n# 1 swxY w Y   |                     t          d          5  d|v  d d d            n# 1 swxY w Y   |                     t          d          5  |                    d           d d d            d S # 1 swxY w Y   d S )NzContextVar key was expectedr,   )r   r	   r   r   getr#   r   s     r   test_context_typerrors_1z$ContextTest.test_context_typerrors_1f   s   !####I/LMM 	 	FF	 	 	 	 	 	 	 	 	 	 	 	 	 	 	##I/LMM 	 	HH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	##I/LMM 	 	GGAJJJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s5   	AAA*A;;A?A?!CCCc                 l    t          j                    }|                     |t           j                   d S r   )r   copy_contextassertIsInstancer	   r<   s     r   test_context_get_context_1z&ContextTest.test_context_get_context_1p   s/    &((c;#677777r   c                 Z   t          j                    }d }|t          j        |          fD ]}|                     |                    |          dddif           |                     |                    |d          dddif           |                     |                    |d          dddd	f           |                     |                    |d
d          dddd	f           i }|                      |j        |d
fi |dddif           |                     |i            d S )Nc                      d|d<   | dz  } | |fS )Nfoospambarr8   r   r   s     r   r   z,ContextTest.test_context_run_2.<locals>.func~   s    "F6NHD<r   rF   rE   rD   r,   )r,   rG      r7   )r'   rE      )rJ   rG   )r   r	   r   partialr   r
   )r#   r   r   fr'   s        r   test_context_run_2zContextTest.test_context_run_2{   sw   !##	  	  	 
 	)$//0 	$ 	$A SWWQZZ(VUO)DEEESWWQ]]Z&%,IJJJQE2235 5 5 2##Au5568 8 8 A2####vuo.0 0 0 Q####%	$ 	$r   c                    t          j                    }d }|                     t                    5  |                    |           d d d            n# 1 swxY w Y   |                     t                    5  |                    |dd           d d d            n# 1 swxY w Y   |                     t                    5  |                    |ddd           d d d            d S # 1 swxY w Y   d S )Nc                      ddz   d S )Nr,   r   r8   rH   s     r   r   z,ContextTest.test_context_run_3.<locals>.func   s    EEEEr   r,   rI   r(   r7   )r   r	   r   ZeroDivisionErrorr
   )r#   r   r   s      r   test_context_run_3zContextTest.test_context_run_3   s   !##	 	 	 011 	 	GGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	011 	  	 GGD!Q	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 011 	' 	'GGD!Q#G&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	's5   AAA8BB #B C((C,/C,c                 X    t          j                    }t          j                    t          j        d           fd fd}|                    |          }                     ||                                |         d                                |           d S )Nvarc                  X                                              d                      d S r   )assertIsNoner;   r#   rS   s   r   func2z-ContextTest.test_context_run_4.<locals>.func2   s'    cggdmm,,,,,r   c                                                              d                                          d                                                                                  d           d           t          j                    }                     t          |           d                               |          d           | S )NrE   r,   )rU   r;   r0   r
   r   r   r?   len)curctx2rW   r#   rS   s    r   func1z-ContextTest.test_context_run_4.<locals>.func1   s    cggdmm,,,GGFOOOHHUOOOSWWT]]F333*,,CSXXq)))SXv...Jr   rE   )r   r	   r   r
   r   r-   )r#   ctx1r\   returned_ctxr[   rW   rS   s   `   @@@r   test_context_run_4zContextTest.test_context_run_4   s    "$$"$$$U++	- 	- 	- 	- 	- 	-		 		 		 		 		 		 		 		 xx|,,,c*F333c<(((((r   c                 B    t          j                    }t          j        d           fd}                     t                    5  |                    |           d d d            n# 1 swxY w Y                                            d                      d S )NrS   c                                                                d                                          d           ddz   d S )NrE   r,   r   )rU   r;   r0   rV   s   r   r   z,ContextTest.test_context_run_5.<locals>.func   s=    cggdmm,,,GGFOOOEEEEr   )r   r	   r   r   rP   r
   rU   r;   )r#   r   r   rS   s   `  @r   test_context_run_5zContextTest.test_context_run_5   s    !##$U++	 	 	 	 	 	
 011 	 	GGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	#''$--(((((s   
A,,A03A0c                      t          j                    t          j        dd           fd}                    |           d S )Nr'   r   r)   c                  t                                                         d                                                                                              d                                                                 d                                                               d           d S )Nr   *   )r   r;   rU   r0   )r$   r   r#   s   r   funz+ContextTest.test_context_run_6.<locals>.fun   s    QUUWWa(((cggajj)))EE"IIIQUUWWb)))SWWQZZ,,,,,r   r   r	   r   r
   r#   rf   r$   r   s   ` @@r   test_context_run_6zContextTest.test_context_run_6   s`    !##"3222	- 	- 	- 	- 	- 	- 	- 	r   c                 j     t          j                     fd                               d S )Nc                                           t          d          5                                  d d d            d S # 1 swxY w Y   d S )Nzis already entered)r   RuntimeErrorr
   )r   rf   r#   s   r   rf   z+ContextTest.test_context_run_7.<locals>.fun   s    ''6JKK                   s   A  AAr   )r#   r   rf   s   `@@r   test_context_run_7zContextTest.test_context_run_7   sK    !##	 	 	 	 	 	 	 	r   c                    t          j        d          }|                     t                    5  |                                 d d d            n# 1 swxY w Y   |                     |                    d                      |                    d          }|                     |                                d           |                     |                    d           d           |                     |j	        |j
                   |                     |j	        t           j        j
                   |                     |j        |           |                    d          }|                     |                                d           |                     |                    d           d           |                     |j	        d           |                    |           |                     |                                d           |                     |                    d           d           |                    d           |                     t          d          5  |                    |           d d d            n# 1 swxY w Y   |                     |                                d           t          j                    }|                     ||           |                    |           |                     t          d          5  |                    |           d d d            n# 1 swxY w Y   |                     |                    d                      |                     ||           |                     ||         d           |                     |                    |d          d           |                     t%          |          d           |                     t'          |                                          |dfg           |                     t'          |                                          dg           |                     t'          |                                          |g           |                     t'          |          |g           t          j                    }|                     ||           |                     t0                    5  ||          d d d            n# 1 swxY w Y   |                     |                    |d          d           |                     t%          |          d           |                     t'          |          g            d S )	Nr$   re   rE   spam2zhas already been usedaar,   r   )r   r   r   LookupErrorr;   rU   r0   r   assertIs	old_valueMISSINGTokenrS   r2   r   rl   r?   r-   rY   listitemsvalueskeysr1   KeyError)r#   r$   t0r4   r]   r[   s         r   test_context_getset_1z!ContextTest.test_context_getset_1   sc   "3''{++ 	 	EEGGG	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	!%%++&&&UU2YY"%%%tb)))blBJ///blK$5$=>>>bfa   EE&MM&)))tf---b)))	


"%%%tb)))	g##L2IJJ 	 	GGAJJJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	'***'))a	##L2IJJ 	 	GGBKKK	 	 	 	 	 	 	 	 	 	 	 	 	 	 	!%%++&&&aa'***!T**G444TA&&&djjll++q'l^<<<dkkmm,,wi888diikk**QC000daS)))'))D!!!x(( 	 	GG	 	 	 	 	 	 	 	 	 	 	 	 	 	 	!T**D111TA&&&dR(((((sG   AAA%JJJL55L9<L92	TTTc                    t          j        d          }t          j        d          }|                    d          }|                     t          d          5  |                    |           d d d            d S # 1 swxY w Y   d S )Nv1v2re   zby a different)r   r   r0   r   
ValueErrorr2   )r#   r~   r   t1s       r   test_context_getset_2z!ContextTest.test_context_getset_2  s    #D))#D))VVBZZ##J0@AA 	 	HHRLLL	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A<<B B c                      t          j        dd          t          j                     fd}                    |           d S )Nr$   re   r)   c                                                                          d                               t                    5            d d d            n# 1 swxY w Y                                                                                                         d          d                                                               t                                                    g                                d          }                      t                                                    g                                         d           	                    |                                 t                                                    g                                t                    5            d d d            d S # 1 swxY w Y   d S )Nre   rE   r,   )
r   r;   r   rz   rU   r1   rv   ry   r0   r2   )r4   r$   r   r#   s    r   rf   z.ContextTest.test_context_getset_3.<locals>.fun&  s   QUUWWb)))""8,,  A              cggajj)))SWWQ//888Q$$$T#((**--r222aAT#((**--s333SVQ'''GGAJJJT#((**--r222""8,,  A                 s$   	AA A	G""G&)G&)r   r   r	   r
   rh   s   ` @@r   test_context_getset_3z!ContextTest.test_context_getset_3!  s`    "3333!##	 	 	 	 	 	 	$ 	r   c                     t          j        dd          }t          j                    }|                    |j        d          }|                     t          d          5  |                    |           d d d            d S # 1 swxY w Y   d S )Nr$   re   r)   r,   zdifferent Context)r   r   r	   r
   r0   r   r   r2   )r#   r$   r   toks       r   test_context_getset_4z!ContextTest.test_context_getset_4:  s    "3333!##ggaeQ##J0CDD 	 	GGCLLL	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s    BB
Bc                     t          j        dd                              g             fd}t          j                                        |                                                                g            d S )Nr$   re   r)   c                                            g                                                                 d                                                                dg           d S )Nre   )r0   r;   r/   r   r$   r#   s   r   rf   z.ContextTest.test_context_getset_5.<locals>.funI  sR    EE"IIIEEGGNN2QUUWWrd+++++r   )r   r   r0   r?   r
   r   r;   )r#   rf   r$   s   ` @r   test_context_getset_5z!ContextTest.test_context_getset_5D  s    "3333	b				, 	, 	, 	, 	, 	,
 	 ""&&s+++"%%%%%r   c                      t          j                    t          j        dd           fd} fd                    |           d S )Nr$   re   r)   c                                          d                                           }                     |          d                                d                                        d                               |          d           |                                                             d                               |          d           d S )N
         )r0   copyr   r
   )r[   r$   r]   ctx2_funr#   s    r   ctx1_funz1ContextTest.test_context_copy_1.<locals>.ctx1_funU  s    EE"III99;;DT!Wb)))EE"IIIT!Wb)))T!Wb)))HHXT!Wb)))T!Wb)))))r   c                                                                            d                                d                                                                 d           d S )Nr   r   )r   r;   r0   r   s   r   r   z1ContextTest.test_context_copy_1.<locals>.ctx2_func  sT    QUUWWb)))EE"IIIQUUWWb)))))r   rg   )r#   r   r$   r]   r   s   ` @@@r   test_context_copy_1zContextTest.test_context_copy_1Q  s    "$$"3333	* 	* 	* 	* 	* 	* 	* 	*	* 	* 	* 	* 	* 	*
 	r   c           	      n    t          j        d           fd}t          j                            d          5 }t          |                    |t          d                              }d d d            n# 1 swxY w Y                        |t          t          d                               d S )Ncvarc                     t          d          D ]l}                    | |z              t          j        t	          j        dd                                                                         | |z              m| S )Nr   gMbP?g?)ranger0   timesleeprandomuniformr   r;   )numir   r#   s     r   subz/ContextTest.test_context_threads_1.<locals>.subn  st    2YY 6 6q!!!
6>%66777  S1W5555Jr   r   )max_workers)	r   r   
concurrentfuturesThreadPoolExecutorrv   mapr   r   )r#   r   tpresultsr   s   `   @r   test_context_threads_1z"ContextTest.test_context_threads_1j  s    %f--	 	 	 	 	 	 22r2BB 	3b266#uRyy1122G	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	$uRyy//22222s   1A::A>A>N)__name__
__module____qualname__hasattrunittestTestCaseassertRaisesRegexpr   r%   r   r5   r9   r=   rA   rM   rQ   r_   rb   ri   rm   r|   r   r   r   r   r   r   r8   r   r   r   r   %   s       78$&9:: A$-@2 2 2  ) ) )D" " "  8 8 8$ $ $8' ' ' ) ) )0) ) )     3) 3) 3)j      0    
& 
& 
&  2 3 3 3 3 3r   r   __main__)geventr   	patch_allconcurrent.futuresr   r   ImportErrorr   r   r   r   hamtr   r   r   r   mainr8   r   r   <module>r      s                 # # # #""""""""#          S3 S3 S3 S3 S3(# S3 S3 S3t  zHMOOOOO s     ..