
    Bi?                        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 d dlZd dlmZ	 ddl
mZ ddl
mZ ddl
mZ dd	l
mZ dd
l
mZ ddlmZ ddlmZ  G d de      Zd Zd Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zedk(  r ej<                          yy)    N)ABCMeta)abstractmethod)greenlet   )TestCase)RUNNING_ON_MANYLINUX)PY313)PY314)RUNNING_ON_FREETHREAD_BUILD)fails_leakcheck)ignores_leakcheckc                       e Zd Zy)	SomeErrorN__name__
__module____qualname__     b/home/ubuntu/crypto_trading_bot/.venv/lib/python3.12/site-packages/greenlet/tests/test_greenlet.pyr   r      s    r   r   c                     	 t        j                         j                  j                          t        #  | j	                  t        j                         d           xY wNr   )r   
getcurrentparentswitchappendsysexc_infor   seens    r   fmainr!   #   sJ    $$++- OCLLN1%&s	   ,4 )Ac                 F    d }t        ||       }|j                  |       y )Nc                     | Nr   )excs    r   crasherzsend_exception.<locals>.crasher/   s    	r   r   RawGreenletr   )gr%   r&   g1s       r   send_exceptionr,   ,   s    	WQ	'BIIcNr   c                      e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zed
        Zd Zd Zd Zd Z ej&                  exr ed      d        Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"e#d1d       Z$e#d        Z%d Z&e#d         Z'd! Z(d" Z)d# Z*d$ Z+d% Z,d& Z- ej&                  exr ed'      d(        Z.d) Z/d* Z0d+ Z1d, Z2d- Z3d. Z4d/ Z5y0)2TestGreenletc                    g fd}t        |      }j                  d       |j                          j                  d       |j                          j                  d       | j                  t	        t        d                   y )Nc                       j                  d       t        j                         j                  j	                           j                  d       y )Nr      r   r   r   r   r   lsts   r   fz'TestGreenlet._do_simple_test.<locals>.f:   s4    JJqM!((//1JJqMr   r            )r)   r   r   assertEquallistrange)selfr5   r*   r4   s      @r   _do_simple_testzTestGreenlet._do_simple_test7   sc    	 N

1	


1	


1d58n-r   c                 $    | j                          y r$   )r=   r<   s    r   test_simplezTestGreenlet.test_simpleF   s    r   c                     t               }| j                  t              5 }|j                          d d d        | j	                  dt        j                               y # 1 sw Y   /xY wNrun)r)   assertRaisesAttributeErrorr   assertInstr	exceptionr<   r*   r%   s      r   (test_switch_no_run_raises_AttributeErrorz5TestGreenlet.test_switch_no_run_raises_AttributeErrorI   sP    M~. 	#HHJ	 	eS/0	 	s   AA(c                     t               }| j                  t              5 }|j                  t               d d d        | j                  dt        j                               y # 1 sw Y   /xY wrB   )r)   rD   rE   throwr   rF   rG   rH   rI   s      r   'test_throw_no_run_raises_AttributeErrorz4TestGreenlet.test_throw_no_run_raises_AttributeErrorP   sS    M~. 	#GGI	 	eS/0	 	s   A$$A-c                     t        d       }| j                  |       | j                  |j                  t	        j
                                y )Nr'   )r)   assertIsNotNoneassertIsr   r   r   r<   r*   s     r   test_parent_equals_Nonez$TestGreenlet.test_parent_equals_NoneW   s5    t$Qahh 3 3 56r   c                 t    t        d       }| j                  |       | j                  |j                         y )N)rC   )r)   rO   assertIsNonerC   rQ   s     r   test_run_equals_Nonez!TestGreenlet.test_run_equals_None\   s,    D!Q!%% r   c                    g fd}t        |      }t        |      }|j                          | j                  t              d       |j                          | j                  t              d       |j                          | j                  t              d       | j                  |j                  d       |j                          | j                  t              d       | j                  |j                  d       y )Nc                       j                  d       t        j                         j                  j	                           j                  ddg       y )Nr   )r   r   r   r   r   extendr3   s   r   r5   z)TestGreenlet.test_two_children.<locals>.fd   s9    JJqM!((//1JJ1vr   r   r6   r7   T   )r)   r   r9   lendead)r<   r5   r*   hr4   s       @r   test_two_childrenzTestGreenlet.test_two_childrena   s    	 NN	
S1%	
S1%	
S1%&	
S1%&r   c                    g fdfd}| j                  t        j                  |      t        sdnd       t	        |      }| j                  t        j                  |      t        sdnd       |j                          | j                  g d       | j                  t        j                  |      t        sd       y d       y )Nc                       j                  d       t        j                         j                  j	                          y )Nbr2   r3   s   r   r5   z3TestGreenlet.test_two_recursive_children.<locals>.fx   s)    JJsO!((//1r   c                      j                  d       t              } | j                          j                  d       y )Nac)r   r)   r   )r*   r5   r4   s    r   r*   z3TestGreenlet.test_two_recursive_children.<locals>.g|   s+    JJsOAAHHJJJsOr   r6   r   )rb   r`   rc   )r9   r   getrefcountr
   r)   r   )r<   r*   r5   r4   s     @@r   test_two_recursive_childrenz(TestGreenlet.test_two_recursive_childrenu   s    	2	
 	+eQCN 	+eQC	
o.+eQCCr   c                      g  fd}t        d      D cg c]  }t        j                  |       }}|D ]  }|j                           |D ]  }|j	                  d         j                  t              t        |             y c c}w )Nc                  H     j                          j                  d       y NT)r=   r   )r<   successs   r   r5   z$TestGreenlet.test_threads.<locals>.f   s      "NN4 r   
   target)r;   	threadingThreadstartjoinr9   rZ   )r<   r5   ithsthri   s   `    @r   test_threadszTestGreenlet.test_threads   s    	! 499=ayq)== 	BHHJ	 	BGGBK	Ws3x0 >s   Bc                 x    d d dfd	}t        d      D ]  } |        t        j                           y )Nc                     d }t        j                   |      }|j                          | j                  |       |j                          y )Nc                  ^    	 t        j                         j                  j                          -r$   r   r   r   r   r   r   r   bodyzPTestGreenlet.test_switching_many_threads.<locals>.creates_greenlet.<locals>.body   s%    '')00779 r   )r   r   r   wait)	greenletswait_to_die_untilry   r*   s       r   creates_greenletzBTestGreenlet.test_switching_many_threads.<locals>.creates_greenlet   s<    : !!$'AHHJQ""$r   c                     |j                          |j                         s?t        |       D ]  }	 |j                  s|j	                          ! |j                         s>y y # t
        j                  $ r Y Jw xY wr$   )rz   is_setr:   r[   r   r   error)r{   wait_to_check_until
quit_afterr*   s       r   switches_from_other_threadzLTestGreenlet.test_switching_many_threads.<locals>.switches_from_other_thread   sk    $$& '')i A vvHHJ !'')
 $>>  s   A!!A76A7c           	      d   g }t        j                         }t        j                         }t        |       D cg c]  }t        j                  ||f       }}t        |       D cg c]  }t        j                  	|||f       }}||z   D ]  }|j	                           t        |      | k  r$t        j                  d       t        |      | k  r$|j                          |D ]  }|j                  d        |j                          |D ]  }|j                  d        y c c}w c c}w )Nrl   argsg-C6?g      ?)
rm   Eventr;   rn   ro   rZ   timesleepsetrp   )
thread_countr{   greenlets_all_createdall_threads_dead_creators	switcherstr}   r   s
           r   run_itz8TestGreenlet.test_switching_many_threads.<locals>.run_it   sB   I$-OO$5!(0 &	    (8	@U@W XH  &    (B'02G'7'9:I  * 	 i.</

6" i.</!%%' s  " s5s    D((!D-   )   )r;   gccollect)r<   r   r   r}   r   s      @@r   test_switching_many_threadsz(TestGreenlet.test_switching_many_threads   s6    	%
	 	H r 	AHJJL	r   c                    g }t        t              }t        t              }|j                  |       |j                  |       ||_        | j	                  |g        | j                  t        |j                         | j	                  |t        g       |j                         }| j	                  |d       | j	                  |t        g       |j                  d      }| j	                  |d       | j	                  |t        g       y )Nr      )r)   r!   r   r   r9   rD   r   )r<   r    r+   g2values        r   test_exceptionzTestGreenlet.test_exception   s    
		$
		$	r" 	)RYY/	{+		#	{+		"#	{+r   c                     g }t        t              }|j                  |       | j                  t        t
        |t               | j                  |t        g       y r$   )r)   r!   r   rD   KeyErrorr,   r9   )r<   r    r+   s      r   test_send_exceptionz TestGreenlet.test_send_exception   sB    
		$(NBAz*r   c                    g }t        t              }t        t              }|j                  |       |j                  |       | j                  |g        ~t	        j
                          | j                  |t        j                  g       ~t	        j
                          | j                  |t        j                  t        j                  g       y r$   )r)   r!   r   r9   r   r   r   GreenletExit)r<   r    r+   r   s       r   test_dealloczTestGreenlet.test_dealloc   s    
		$
		$r"


 5 567


 5 5x7L7LMNr   c                 <   d }t        |      }|j                          g t        j                  }fd}|t        _        	 ~|t        _        | j	                  t              d       | j                  d   j                  t               y # |t        _        w xY w)Nc                      	 t        j                         j                  j                          y # t         j                  $ r t
        d w xY wr$   )r   r   r   r   r   r   r   r   r   rC   zHTestGreenlet.test_dealloc_catches_GreenletExit_throws_other.<locals>.run
  s?    *##%,,335(( *T)*s	   ,/ A
c                 (    j                  |        y r$   r   )
unraisableunraisable_eventss    r   _capturezMTestGreenlet.test_dealloc_catches_GreenletExit_throws_other.<locals>._capture  s    $$Z0r   r   r   )	r)   r   r   unraisablehookr9   rZ   assertIsInstance	exc_valuer   )r<   rC   r*   old_hookr   r   s        @r   .test_dealloc_catches_GreenletExit_throws_otherz;TestGreenlet.test_dealloc_catches_GreenletExit_throws_other	  s    	* 	
%%	1%	*!)C./3/2<<iH "*Cs   B Bz=Sometimes flaky (getting one GreenletExit in the second list)c                 2   g g t        j                         t        j                         t        j                         t        j                         fd}t        j                  |      }|j                          j	                  d       | j                  g        | j                  t              d       d d = t        st        j                          | j                  g        j                          j	                  d       	 | j                  t        j                  g       j                          |j                  d       d d = d d = y # j                          |j                  d       d d = d d = w xY w)Nc                  >   t        t              } | j                         j                  |        ~ t	        j
                          j                          j                  d       t                j                          j                  d       t                y )Nr1   )r)   r!   r   r   r   r   r   rz   )r+   /bg_glet_created_running_and_no_longer_ref_in_bgbg_should_be_clearfg_ref_releasedok_to_exit_bg_threadr    somerefs    r   r5   z1TestGreenlet.test_dealloc_other_thread.<locals>.f1  sn    U#BIIdONN2JJL;??A  #M""$ %%a(Mr   rk   rj   r   r1   )rm   r   rn   ro   rz   r9   rZ   r   r   r   r   r   r   rp   )	r<   r5   r   r   r   r   r   r    r   s	      @@@@@@r   test_dealloc_other_threadz&TestGreenlet.test_dealloc_other_thread!  sF    :C//:K7#//+&__.(0	 	 A&		7<<R@r"Wq)AJ* JJL 	r""	TH$9$9#:; $$&FF2JQ
 !$$&FF2JQ
s   !E) )-Fc                 @     fd}t        |      }|j                         } j                  ||j                  u         j                  |       |j                         } j	                  |        j                  |d        j                  |j                  d        y )Nc                      t        j                  d      } j                  | j                  d        t	        j
                         j                  j                  |        y)Nr   meaning of life)r   	_getframer9   f_backr   r   r   r   )r5   r<   s    r   f1z#TestGreenlet.test_frame.<locals>.f1`  sE    a AQXXt,!((//2$r   r   )r)   r   
assertTruegr_frameassertFalser9   )r<   r   r*   framefrom_gs   `    r   
test_framezTestGreenlet.test_frame_  s}    	%
 O
+,!23T*r   c                     d }t        j                  |d      }t        j                  |d      }|j                          |j                          |j                  d       |j                  d       y )Nc                 B     t         fd      }|j                          y )Nc                  .    t        j                         S r$   )r   r   xs   r   <lambda>z>TestGreenlet.test_thread_bug.<locals>.runner.<locals>.<lambda>q  s    DJJqM r   r(   )r   r*   s   ` r   runnerz,TestGreenlet.test_thread_bug.<locals>.runnerp  s    12AHHJr   )g?r   )g333333?rj   )rm   rn   ro   rp   )r<   r   t1t2s       r   test_thread_bugzTestGreenlet.test_thread_bugo  sU    	 V&9V&9





r   c                 l      fd}t        |      j                  dd      } j                  |d       y )Nc                 N    j                  | d       j                  |d       y)Nr7   r6   *   )r9   )rb   r`   r<   s     r   rC   z,TestGreenlet.test_switch_kwargs.<locals>.run{  s&    Q"Q"r   r7   r6   )rb   r`   r   r)   r   r9   )r<   rC   r   s   `  r   test_switch_kwargszTestGreenlet.test_switch_kwargsz  s4    	 ##a1#-Br   c                     d }t        |      }| j                  ddi|j                  d             | j                  dddif|j                                | j                  d|j                                y )Nc                     t        j                         j                  j                  |        t        j                         j                  j                  dd       | | dz  fS )Nr   r6   r1   rx   r   s    r   rC   z6TestGreenlet.test_switch_kwargs_to_parent.<locals>.run  sR    !((//!/4!((//Q/7a1f9r   r   r1   )r6   )r1   	   )r)   r9   r   )r<   rC   r*   s      r   test_switch_kwargs_to_parentz)TestGreenlet.test_switch_kwargs_to_parent  sd    	 #q188A;/$a)188:6,r   c                    i t        j                         t        j                         fd}t        j                  |      }|j                          j	                  d       | j                  t        j                        5  d   j                          d d d        j                          |j                  d       j                          y # 1 sw Y   ;xY w)Nc                  f    t        d       d<    j                          j                  d       y )Nc                       y r$   r   r   r   r   r   zITestGreenlet.test_switch_to_another_thread.<locals>.run.<locals>.<lambda>      r   r*   rj   r)   r   rz   created_eventdata
done_events   r   rC   z7TestGreenlet.test_switch_to_another_thread.<locals>.run  )    #L1DIOOBr   rk   rj   r*   )rm   r   rn   ro   rz   rD   r   r   r   r   rp   clear)r<   rC   threadr   r   r   s      @@@r   test_switch_to_another_threadz*TestGreenlet.test_switch_to_another_thread  s    !)__&
	  !!-2x~~. 	I	B

	 	s   CC"c                 P      fd} fdt        |      j                          y )Nc                      	 t        d      #  t        j                         } t              j	                          j                  | t        j                                Y y xY w)Nfun)
ValueErrorr   r   r)   r   r9   )r   r\   r<   s    r   r5   z&TestGreenlet.test_exc_state.<locals>.f  sK    ; '';<<>A%%'  3<<>:s
    AA#c                  N     j                  t        j                         d       y )N)NNN)r9   r   r   r?   s   r   r\   z&TestGreenlet.test_exc_state.<locals>.h  s    S\\^-?@r   r(   )r<   r5   r\   s   ` @r   test_exc_statezTestGreenlet.test_exc_state  s     	;	A 	Ar   c                    d }d }d }t        |      }| j                  |j                  i        |j                          | j                  |j                  d       | j                  |j                  ddi       |j                  |_        | j                  |j                  ddi       | j                  t        ||       | j                  t        ||d       y )Nc                  6    dt        j                         _        y Nr   )r   r   testr   r   r   r5   z*TestGreenlet.test_instance_dict.<locals>.f  s    )+H!&r   c                     | ` y r$   __dict__)r*   s    r   deldictz0TestGreenlet.test_instance_dict.<locals>.deldict  s    
r   c                     || _         y r$   r   )r*   r   s     r   setdictz0TestGreenlet.test_instance_dict.<locals>.setdict  s	    AJr   r   r   )r)   r9   r   r   r   rD   	TypeError)r<   r5   r   r   r*   s        r   test_instance_dictzTestGreenlet.test_instance_dict  s    	,		NR(	
$fb\2ZZ
fb\2)Wa0)Wa4r   c                 p    g fd}t        |      }|j                          | j                  dg       y )Nc                  `     j                  t        t        j                         d             y rB   )r   hasattrr   r   )has_runs   r   funcz;TestGreenlet.test_running_greenlet_has_no_run.<locals>.func  s"    NN++-u5r   Fr   )r<   r   r*   r   s      @r    test_running_greenlet_has_no_runz-TestGreenlet.test_running_greenlet_has_no_run  s4    	
 	
5'*r   c                     dd l }| j                  t        |j                   t                      | j                  t        |j                  t                      y r   )copyrD   r   r)   deepcopy)r<   r   s     r   test_deepcopyzTestGreenlet.test_deepcopy  s5    )TYY>)T]]KMBr   c                 4   t        d       t        j                         g fd}t        |      }|j                          ~| j	                         | j                  d          | j                  d   j                         d d = d d y )Nc                       y r$   r   r   r   r   r   z;TestGreenlet.test_parent_restored_on_kill.<locals>.<lambda>  r   r   c                     	 j                          y # t        j                  $ rc j                  t        j                         j
                         j                  t        j                                 j                          Y y w xY wr$   )r   r   r   r   r   r   )hubmainresults   r   workerz9TestGreenlet.test_parent_restored_on_kill.<locals>.worker  s_    (( h113::;h1134

	s    A3B
	B
r'   r   r   )r)   r   r   r   r   rP   r   )r<   r  r*   r  r  r  s      @@@r   test_parent_restored_on_killz)TestGreenlet.test_parent_restored_on_kill  s    ,'""$	 s+	
fQi&fQi&&, 1Ir   c                     t               }t        d |      }| j                  t              5  |j                          d d d        y # 1 sw Y   y xY w)Nc                       y r$   r   r   r   r   r   z9TestGreenlet.test_parent_return_failure.<locals>.<lambda>  r   r   r'   )r)   rD   rE   r   )r<   r+   r   s      r   test_parent_return_failurez'TestGreenlet.test_parent_return_failure  sA    ]b1~. 	IIK	 	 	s   AAc                      G d dt               } |d       }| j                  t        |j                  t                      y )Nc                       e Zd Zd Zy)>TestGreenlet.test_throw_exception_not_lost.<locals>.mygreenletc                 J    	 t         #  Y nxY wt        j                  | |      S r$   )	Exceptionr)   __getattribute__r<   names     r   r  zOTestGreenlet.test_throw_exception_not_lost.<locals>.mygreenlet.__getattribute__  s)    #O"33D$??s    Nr   r   r   r  r   r   r   
mygreenletr    s    @r   r  c                       y r$   r   r   r   r   r   z<TestGreenlet.test_throw_exception_not_lost.<locals>.<lambda>  r   r   )r)   rD   r   rL   )r<   r  r*   s      r   test_throw_exception_not_lostz*TestGreenlet.test_throw_exception_not_lost  s4    	@ 	@ |$)QWWik:r   c                    g d fd}t        j                  |      }|j                          |j                  d       ~r| j	                          | j                  t        j                        5 }d   j                  t               d d d        s0t        j                        }| j                  |dk(  xs d|v        n%| j                  t        j                        d       t        d   j                  d      r;| j                  t               5  d   j                  j#                          d d d        sKt        d   j                  j$                  d      rFd   j                  j$                  j#                          n| j'                  d   j                         ~d d d = d	| _        y # 1 sw Y   0xY w# 1 sw Y   xY w)
Nc                  \    t        j                         j                  j                          y r$   rx   r   r   r   r  zGTestGreenlet._do_test_throw_to_dead_thread_doesnt_crash.<locals>.worker       !((//1r   c                      t              } | j                          j                  |        r%| j                          t        j                          y y r$   )r)   r   r   r   r   )r*   r  wait_for_cleanupr  s    r   creatorzHTestGreenlet._do_test_throw_to_dead_thread_doesnt_crash.<locals>.creator  s?    F#AHHJMM!
##%  r   rk   rj   r   Bcannot switch to a different thread (which happens to have exited)zCannot switchr   T)rm   rn   ro   rp   wait_for_pending_cleanupsrD   r   r   rL   r   rG   rH   r   r9   r   r   RuntimeErrorr   f_localsrT   expect_greenlet_leak)r<   r  r  r   r%   sr  r  s    `    @@r   *_do_test_throw_to_dead_thread_doesnt_crashz7TestGreenlet._do_test_throw_to_dead_thread_doesnt_crash  s   	2	& G,			r
 **,x~~. 	'#1IOOI&	'  CMM"AOOYY ("a'
 CMM"T
 6!9%%w/""<0 +q	""((*+  vay))22G<q	""++113fQi0011I %)!C	' 	'"+ +s   9G G G
Gc                 $    | j                          y r$   r"  r?   s    r   &test_throw_to_dead_thread_doesnt_crashz3TestGreenlet.test_throw_to_dead_thread_doesnt_crash7  s    779r   c                 &    | j                  d       y rh   r$  r?   s    r   +test_throw_to_dead_thread_doesnt_crash_waitz8TestGreenlet.test_throw_to_dead_thread_doesnt_crash_wait;  s    77=r   c                      G d dt               } |       }| j                  |j                  d      d       d| _        y )Nc                       e Zd Zd Zd Zd Zy)7TestGreenlet.test_recursive_startup.<locals>.convolutedc                 <    t        j                  |        d| _        y r   )r)   __init__countr?   s    r   r,  z@TestGreenlet.test_recursive_startup.<locals>.convoluted.__init__A  s    $$T*
r   c                     |dk(  r'| j                   dk(  rd| _         | j                  d       t        j                  | |      S )NrC   r   r   +   )r-  r   r)   r  r  s     r   r  zHTestGreenlet.test_recursive_startup.<locals>.convoluted.__getattribute__D  s8    5=TZZ1_!"DJKKO"33D$??r   c                 <    	 | j                   j                  |       r$   r   r   )r<   r   s     r   rC   z;TestGreenlet.test_recursive_startup.<locals>.convoluted.runI  s    KK&&u- r   N)r   r   r   r,  r  rC   r   r   r   
convolutedr*  @  s    @
.r   r2  r   r/  T)r)   r9   r   r   )r<   r2  r*   s      r   test_recursive_startupz#TestGreenlet.test_recursive_startup>  s9    	. 	. L"r*
 %)!r   c                 L   t        j                         j                          t        j                         j                           G fddt              }d }fd}t	        j
                         }t        |      g |       d   _        d   j                          t        j                  |      }|j                          j                          j                          | j                  t	        j
                         |       |j                  d       y )Nc                       e Zd Z fdZy);TestGreenlet.test_threaded_updatecurrent.<locals>.finalizedc                 F    j                          j                          y r$   releaseacquire)r<   lock1lock2s    r   __del__zCTestGreenlet.test_threaded_updatecurrent.<locals>.finalized.__del__\  s     r   Nr   r   r   r=  r;  r<  s   r   	finalizedr6  [  s     r   r@  c                  \    t        j                         j                  j                          y r$   rx   r   r   r   deallocatorz=TestGreenlet.test_threaded_updatecurrent.<locals>.deallocatorc  r  r   c                      j                          t        j                           d= j                          j                          t        j                          j                          y r   r:  r   r   r9  )r*   r;  r<  s   r   fthreadz9TestGreenlet.test_threaded_updatecurrent.<locals>.fthreade  sF    MMO!!MMOMMO!MMOr   r   rk   rj   )rm   Lockr:  objectr   r   r)   bombr   rn   ro   r9  r9   rp   )	r<   r@  rB  rE  r  r   r*   r;  r<  s	         @@@r   test_threaded_updatecurrentz(TestGreenlet.test_threaded_updatecurrentT  s      	  	 	2	 ""$%&K!		!G,		 	,,.5	r
r   c                     g fdd t              gd   j                          t        d         j                         }| j                         | j                  |d       y )Nc                     t        j                         j                  j                         } d= t        j                         j                  _        	 t        j                         j                  j                  |        j	                  t        j                                y # j	                  t        j                                w xY wr   )r   r   r   r   r   )r   	initiatorr    r  s    r   r  z>TestGreenlet.test_dealloc_switch_args_not_lost.<locals>.worker  s    '')00779Eq	'224;;I3##%,,33E:H//12H//12s   -B& &%Cc                       yr   r   r   r   r   rL  zATestGreenlet.test_dealloc_switch_args_not_lost.<locals>.initiator      r   r   r   )r)   r   r   r9   )r<   r   rL  r    r  s     @@@r   !test_dealloc_switch_args_not_lostz.TestGreenlet.test_dealloc_switch_args_not_lost  sk    	3	 f%&q		6!95	  "#r   c                     d  G d dt               } |       t        d      fd}t        |      }| j                  |j	                                y )Nc                      | |i | y r$   r   )r   rb   ks      r   _applyz0TestGreenlet.test_tuple_subclass.<locals>._apply  s    !MqMr   c                       e Zd Zd Zy)1TestGreenlet.test_tuple_subclass.<locals>.mytuplec                 p    t        j                         j                          t        j	                  |       S r$   )r   r   r   tuple__len__r?   s    r   rX  z9TestGreenlet.test_tuple_subclass.<locals>.mytuple.__len__  s&    ##%,,.}}T**r   N)r   r   r   rX  r   r   r   mytuplerU    s    +r   rY  r   )rb   c                  f      t        j                         j                  j                         y r$   rx   )rS  r   kwargss   r   switchapplyz5TestGreenlet.test_tuple_subclass.<locals>.switchapply  s$    8&&(//66fEr   )rW  dictr)   r9   r   )r<   rY  r\  r*   rS  r   r[  s       @@@r   test_tuple_subclassz TestGreenlet.test_tuple_subclass  sL    		+e 	+ y	F$V,r   c                     t        dt        fdt        d       i      } G d d|      } G d d|      } |        | j                  t        |       y )NAbstractSubclassrC   c                      y r$   r   r?   s    r   r   z7TestGreenlet.test_abstract_subclasses.<locals>.<lambda>  r   r   c                       e Zd Zy):TestGreenlet.test_abstract_subclasses.<locals>.BadSubclassNr   r   r   r   BadSubclassrc        r   rd  c                       e Zd Zd Zy);TestGreenlet.test_abstract_subclasses.<locals>.GoodSubclassc                      y r$   r   r?   s    r   rC   z?TestGreenlet.test_abstract_subclasses.<locals>.GoodSubclass.run  s    r   N)r   r   r   rC   r   r   r   GoodSubclassrg    s    r   ri  )r   r)   r   rD   r   )r<   r`  rd  ri  s       r   test_abstract_subclassesz%TestGreenlet.test_abstract_subclasses  sT    "NN#4568
	* 		+ 	 	)[1r   c                      t        j                         sy t        j                         d   dk  ry  fd}t        d      D ]  } |       s y  y )Nr   2   c                    	
 t        j                         

j                          t        j                         j                          dg
fd} t        j                  |       }|j	                           G 
fddt
              	 G 	fddt
              }g }t        dz        }t        j                         } |       }|D ]>  }d }d	   r|j                  d
        yt               }d	   r n>|j                  |       @ t        j                          d	   r|j                  d
       yj                  |j                  |       |D ]  }j                  |j                  |         y)NFc                  n     j                          t        j                          j                          y r$   rD  r?  s   r   another_threadzWTestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.another_thread  s!    ##%r   rk   c                       e Zd Z fdZy)TTestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.gc_callbackc                 P    j                          j                          dd<   y )NTr   r8  )r<   r;  r<  recycleds    r   r=  z\TestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.gc_callback.__del__  s    MMOMMO"&HQKr   Nr>  )r;  r<  rs  s   r   gc_callbackrq    s    'r   rt  c                       e Zd Z fdZy)PTestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.garbagec                 ,    | | _                 | _        y r$   )cyclecallback)r<   rt  s    r   r,  zYTestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.garbage.__init__  s    !%DJ$/MDMr   N)r   r   r   r,  )rt  s   r   garbagerv    s    2r   rz  r6   r   rj   T)rm   rF  r:  rn   ro   rG  r;   r   r   rp   r)   r   r   r   r9   r   )ro  r   rz  lr   currentr*   r   lastrt  r;  r<  rs  Nr<   s            @@@@r   attemptz?TestGreenlet.test_implicit_parent_with_threads.<locals>.attempt  s9   NN$EMMONN$EMMOwH    7AGGI' 'f '
2& 2 Aac
A))+G	A A;FF2J "}A; 

A;FF2JT[['2 4  734r   r8   )r   	isenabledget_thresholdr;   )r<   r  r   r~  s   `  @r   !test_implicit_parent_with_threadsz.TestGreenlet.test_implicit_parent_with_threads  sO    ||~q!r6,	Z q 	Ay	r   c                 x    ddl m ddl m  G d dt              }t	        j
                  |      }g  _         fd}t        d      D ]  } ||      j                            `t        r& j                  t	        j
                  |      |       y  j                  t	        j
                  |      |       y )Nr   r   )r   c                       e Zd Zy)TTestGreenlet.test_issue_245_reference_counting_subclass_no_threads.<locals>.GreenletNr   r   r   r   Greenletr  	  re  r   r  c                      	         j                   j                          y #  $ r# j                  j                                 Y y w xY wr$   )r   r   gletsr   )r   r   r<   s   r   greenlet_mainzYTestGreenlet.test_issue_245_reference_counting_subclass_no_threads.<locals>.greenlet_main  s?    0##**, 0

!!*,/0s   # %A
Arj   )r   r   r   r)   r   rd   r  r;   r   r   assertGreaterEqualr9   )r<   r  initial_refsr  r   r   r   s   `    @@r   5test_issue_245_reference_counting_subclass_no_threadszBTestGreenlet.test_issue_245_reference_counting_subclass_no_threads   s     	()	{ 	 x0 
	0 r 	-A]#**,	- J& ##COOH$=|LS__X6Er   z;The manylinux images appear to hang on this test on 3.13rc2c                    ddl m} ddl m} ddlm  G d dt
              g  |       fdfd}t        j                        }g }|d	z   }t        rd
}t        |      D ]5  } |       } |||f      }	|j                  |       |	j                          7 |D ]  }
|
j                  d        d d = j                          | j                          | j                  t        j                        |       y )Nr   )rn   )r   r  c                       e Zd Zy)STestGreenlet.test_issue_245_reference_counting_subclass_threads.<locals>.MyGreenletNr   r   r   r   
MyGreenletr  8  re  r   r  c                  D             j                   j                          y r$   r1  r  s   r   r  zVTestGreenlet.test_issue_245_reference_counting_subclass_threads.<locals>.greenlet_main>  s    L&&(r   c                            }j                  |       |j                          ~| j                          j                  d                y )Nrj   )r   r   r   rz   )greenlet_running_eventminer  r   r  r  ref_cleareds     r   thread_mainzTTestGreenlet.test_issue_245_reference_counting_subclass_threads.<locals>.thread_mainA  sH    m,DLLKKM"&&( R  Lr   -   rl  r   rj   )rm   rn   r   r   r   r)   r   rd   r   r;   r   ro   rz   r   r  r9   )r<   rn   r   r  r  thread_ready_eventsr   r   eventr   r   r  r   r  r  r  s              @@@@@r   2test_issue_245_reference_counting_subclass_threadsz?TestGreenlet.test_issue_245_reference_counting_subclass_threads*  s     	%#'	 	 g	)	 	8 z2 #b(& L|$ 	AGE;eX>F&&u-LLN		 . 	 JOOB	  !H 	&&(4lCr   c                     d }t        |      }d }t        ||      }| j                  t              5  |j                          d d d        y # 1 sw Y   y xY w)Nc                       y)N   r   r   r   r   no_argsz\TestGreenlet.test_falling_off_end_switches_to_unstarted_parent_raises_error.<locals>.no_argsx  rN  r   c                       yr   r   r   r   r   leafzYTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_raises_error.<locals>.leaf}  rN  r   )r)   rD   r   r   )r<   r  parent_never_startedr  childs        r   >test_falling_off_end_switches_to_unstarted_parent_raises_errorzKTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_raises_errorw  sV    	  +73	 D"67 y) 	LLN	 	 	s   AAc                     d }t        |      }d }t        ||      }|j                         }| j                  |d       y )Nc                 
    | dfS N   r   r   s    r   one_argzUTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_works.<locals>.one_arg  s    r7Nr   c                       yr   r   r   r   r   r  zRTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_works.<locals>.leaf  rN  r   )r   r  r   )r<   r  r  r  r  r  s         r   7test_falling_off_end_switches_to_unstarted_parent_workszDTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_works  sC    	  +73	 D"67*r   c                     G d dt               } |       }g t        fd|      }| j                  t              5  |j                          d d d        | j	                  dg       | j                  t              5  |j                          d d d        | j	                  dg       y # 1 sw Y   ^xY w# 1 sw Y   )xY w)Nc                       e Zd Zd Zy)XTestGreenlet.test_switch_to_dead_greenlet_with_unstarted_perverse_parent.<locals>.Parentc                     |dk(  rt         y rB   )r   r  s     r   r  ziTestGreenlet.test_switch_to_dead_greenlet_with_unstarted_perverse_parent.<locals>.Parent.__getattribute__  s    5=#O !r   Nr  r   r   r   Parentr    s    $r   r  c                  &     j                  d      S r   r   r   s   r   r   zZTestGreenlet.test_switch_to_dead_greenlet_with_unstarted_perverse_parent.<locals>.<lambda>      DKKO r   r   )r)   rD   r   r   r9   )r<   r  r  r  r    s       @r   ;test_switch_to_dead_greenlet_with_unstarted_perverse_parentzHTestGreenlet.test_switch_to_dead_greenlet_with_unstarted_perverse_parent  s    	$[ 	$  &x35IJ y) 	LLN	 	t$y) 	LLN	t$	 	
	 	s   B,?B8,B58Cc                     g t        fd      }t        fd      }|j                          | j                  dg       ||_        |j                         }| j	                  |       | j                  ddg       y )Nc                  &     j                  d      S r  r   r   s   r   r   zDTestGreenlet.test_switch_to_dead_greenlet_reparent.<locals>.<lambda>  s    4;;r? r   c                  &     j                  d      S r   r   r   s   r   r   zDTestGreenlet.test_switch_to_dead_greenlet_reparent.<locals>.<lambda>  r  r   r   r  )r)   r   r9   r   rT   )r<   r  r  r  r    s       @r   %test_switch_to_dead_greenlet_reparentz2TestGreenlet.test_switch_to_dead_greenlet_reparent  sn    *+BC34t$+&!Bx(r   c                 d   t        j                         fd}fdt        |      }|j                          t        d       }|j                          | j	                  |j
                         | j                  |j
                  j                  j                  d       | j	                  |j
                  j                         | j                  |j
                  j                  j                  j                  d       | j                  |j
                  j                  j                         y )Nc                                y r$   r   inners   r   outerzHTestGreenlet.test_can_access_f_back_of_suspended_greenlet.<locals>.outer  s    Gr   c                  N     j                  t        j                  d             y r   )r   r   r   r  s   r   r  zHTestGreenlet.test_can_access_f_back_of_suspended_greenlet.<locals>.inner  s    KKa()r   c                       y r$   r   r   r   r   r   zKTestGreenlet.test_can_access_f_back_of_suspended_greenlet.<locals>.<lambda>  r   r   r  r  )r   r   r)   r   rO   r   r9   f_codeco_namer   rT   )r<   r  r  	unrelatedr  r  s       @@r   ,test_can_access_f_back_of_suspended_greenletz9TestGreenlet.test_can_access_f_back_of_suspended_greenlet  s     ""$		* % 

  -	 	S\\*,,44g>S\\001,,33;;WE#,,--445r   c                    ddl m ddlm fdt	              }|j                  d       |j                  }t        d      D ]-  }| j                  |j                  d   |       |j                  }/ | j                  |j                  d   d       | j                  |j                         | j                  |j                  d      d	       y )
Nr   )partialr   )_test_extension_cppc                     | dkD  r| j                   | dz
              z  S t        j                         j                  j	                         S )Nr   r   )	test_callr   r   r   r   )vr  r  recurses    r   r  z@TestGreenlet.test_get_stack_with_nested_c_calls.<locals>.recurse  sJ    1u.88!a%9PQQQ&&(//6688r   r8   r  rj   i  )	functoolsr   r  r)   r   r   r;   r9   r  r   rT   )r<   grr   rq   r  r  r  s       @@@r   "test_get_stack_with_nested_c_callsz/TestGreenlet.test_get_stack_with_nested_c_calls  s    %)	9
 !
		!q 	!AU^^C0!4LLE	! 	,a0%,,'2-r   c                 4   t        j                         fd}fdt        |      }|j                         }t        d       }|j                          | j	                  |j
                  j                  d       | j                  |j                         y )Nc                  <      t        j                  d             y r   )r   r   r  s   r   r  z6TestGreenlet.test_frames_always_exposed.<locals>.outer  s    #--"#r   c                 (    j                  |        y r$   )r   )r   r  s    r   r  z6TestGreenlet.test_frames_always_exposed.<locals>.inner  s    KKr   c                       y r$   r   r   r   r   r   z9TestGreenlet.test_frames_always_exposed.<locals>.<lambda>  r   r   r  )	r   r   r)   r   r9   r  r  rT   r   )r<   r  r  r   r  r  r  s        @@r   test_frames_always_exposedz'TestGreenlet.test_frames_always_exposed  sx     ""$	$	 		  -	--w7%,,'r   N)F)6r   r   r   r=   r@   rJ   rM   rR   rU   r]   re   rt   r   r   r   r   r   r   unittestskipIfr	   r   r   r   r   r   r   r   r   r   r   r   r  r	  r  r   r"  r%  r'  r3  rI  rO  r^  rj  r  r  r  r  r  r  r  r  r  r  r   r   r   r.   r.   5   s{   .117
!
'(D,1 = =~,8+OI0 X__&&G55n+ 	 -& 5"	+C
6	; 7) 7)r : :> ) )*-^$4-82 5n(FT X__&&EDDDDL+%*)6:.&(r   r.   c                   H    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)TestGreenletSetParentErrorsc                 
   i t        j                         t        j                         fd}d }t        j                  |      }|j                          j	                  d       t        |      }|j                          | j                  t              5 }d   |_	        d d d        j                          |j                  d       | j                  t        j                        d       y # 1 sw Y   PxY w)Nc                  f    t        d       d<    j                          j                  d       y )Nc                       y r$   r   r   r   r   r   zQTestGreenletSetParentErrors.test_threaded_reparent.<locals>.run.<locals>.<lambda>  r   r   r*   rj   r   r   s   r   rC   z?TestGreenletSetParentErrors.test_threaded_reparent.<locals>.run  r   r   c                  \    t        j                         j                  j                          y r$   rx   r   r   r   blankzATestGreenletSetParentErrors.test_threaded_reparent.<locals>.blank  r  r   rk   rj   r*   z&parent cannot be on a different thread)rm   r   rn   ro   rz   r)   r   rD   r   r   r   rp   r9   rG   rH   )	r<   rC   r  r   r*   r%   r   r   r   s	         @@@r   test_threaded_reparentz2TestGreenletSetParentErrors.test_threaded_reparent  s    !)__&
	 
	2 !!-2	
z* 	!cCyAH	!BS]]+-UV	! 	!s   C99Dc                    g fd}t        j                  |      }|j                          |j                  d       | j	                  | j
                  dz           G fddt              } |d       }| j                  t        j                        5 }|j                          d d d        | j                  t        j                        d	       d d = y # 1 sw Y   4xY w)
Nc                  `    t        d       } j                  |        | j                          y )Nc                       y r$   r   r   r   r   r   zYTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.worker.<locals>.<lambda>  r   r   )r)   r   r   )r*   anothers    r   r  zGTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.worker  s"    L)ANN1HHJr   rk   rj   r   )initial_main_greenletsc                       e Zd Z fdZy)KTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.convolutedc                 N    |dk(  r
d   | _         t        j                  | |      S NrC   r   r   r)   r  r<   r  r  s     r   r  z\TestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.convoluted.__getattribute__8  (    5=")!*DK"33D$??r   Nr  r  s   r   r2  r  7  	    @r   r2  c                       y r$   r   r   r   r   r   zITestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.<lambda><  r   r   r  )rm   rn   ro   rp   r  main_greenlets_before_testr)   rD   r   r   r   r9   rG   rH   )r<   r  r   r2  r*   r%   r  s         @r   test_unexpected_reparentingz7TestGreenletSetParentErrors.test_unexpected_reparenting  s    	 F+			r
& 	&&d>]>]`a>a&b	@ 	@
 |$x~~. 	#HHJ	S]]+]	_AJ		 	s   CC%c                    g t        j                         t        j                         fd} G fddt              }t        j                  |      }|j	                          j                  d       	  |d       }| j                  t        j                        5 }|j                          d d d        | j                  dt        j                               | j                  dt        |j                               | j                  d	t        |j                               j                          |j                  d       d d = y # 1 sw Y   xY w# j                          |j                  d       d d = w xY w)
Nc                      t        d       } j                  |        | j                          j                          j	                  d       y )Nc                       y r$   r   r   r   r   r   zhTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.worker.<locals>.<lambda>J  r   r   rj   )r)   r   r   r   rz   )r*   r  keep_main_aliveswitched_to_greenlets    r   r  zVTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.workerI  s<    L)ANN1HHJ $$&  $r   c                       e Zd Z fdZy)ZTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.convolutedc                 N    |dk(  r
d   | _         t        j                  | |      S r  r  r  s     r   r  zkTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.convoluted.__getattribute__P  r  r   Nr  r  s   r   r2  r  O  r  r   r2  rk   rj   c                       y r$   r   r   r   r   r   zXTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.<lambda>Z  r   r   z#Cannot switch to a different threadExpectedCurrent)rm   r   r)   rn   ro   rz   rD   r   r   r   rF   rG   rH   r   rp   )	r<   r  r2  r   r*   r%   r  r  r  s	         @@@r   *test_unexpected_reparenting_thread_runningzFTestGreenletSetParentErrors.test_unexpected_reparenting_thread_runningC  s%    (0#//+	%	@ 	@ F+		!!"%	<(A""8>>2 c
MM?S]]ASTMM*c#--&89MM)S%78!FF2J
  !FF2J
s%   >(E! &E7A7E! EE! !(F	c                    t        d       }| j                  |j                  t        j                                | j                  t              5 }|`d d d        | j                  t        j                        d       y # 1 sw Y   /xY w)Nc                       y r$   r   r   r   r   r   zGTestGreenletSetParentErrors.test_cannot_delete_parent.<locals>.<lambda>h  r   r   can't delete attribute)
r)   rP   r   r   r   rD   rE   r9   rG   rH   )r<   r  r%   s      r   test_cannot_delete_parentz5TestGreenletSetParentErrors.test_cannot_delete_parentg  sk    \*fmmX%8%8%:;~. 	#	S]]+-EF	 	s   BB
c                     | j                  t              5 }t        j                         `d d d        | j                  t        j                        d       y # 1 sw Y   /xY w)Nr  rD   rE   r   r   r   r9   rG   rH   r<   r%   s     r   !test_cannot_delete_parent_of_mainz=TestGreenletSetParentErrors.test_cannot_delete_parent_of_maino  sP    ~. 	-###%,	-S]]+-EF	- 	-s   AA"c                 ^    | j                  t        j                         j                         y r$   )rT   r   r   r   r?   s    r   !test_main_greenlet_parent_is_nonez=TestGreenletSetParentErrors.test_main_greenlet_parent_is_noneu  s    (--/667r   c                      d } fd}t        |      } j                  |        ||       |j                           j                  |        ||       |j                          y )Nc                  \    t        j                         j                  j                          y r$   rx   r   r   r   bgzCTestGreenletSetParentErrors.test_set_parent_wrong_types.<locals>.bgz  s    !((//1r   c                     d ddfD ]b  }j                  t              5 }|| _        d d d        j                  t	        j
                        dt        |      j                  z          d y # 1 sw Y   GxY w)Nr   42z4GreenletChecker: Expected any type of greenlet, not )rD   r   r   r9   rG   rH   typer   )gletpr%   r<   s      r   checkzFTestGreenletSetParentErrors.test_set_parent_wrong_types.<locals>.check~  sy    1dD( _&&y1 $S"#DK$   &JTRSWM]M]]_	_$ $s   A..A7	)r)   r   r   r   )r<   r  r  r*   s   `   r   test_set_parent_wrong_typesz7TestGreenletSetParentErrors.test_set_parent_wrong_typesy  sW    	2	_ Oa 	

a 	

r   c                     t        d       }| j                  t              5 }||_        d d d        | j	                  t        j                        d       y # 1 sw Y   /xY w)Nc                       y r$   r   r   r   r   r   z@TestGreenletSetParentErrors.test_trivial_cycle.<locals>.<lambda>  r   r   cyclic parent chain)r)   rD   r   r   r9   rG   rH   )r<   r  r%   s      r   test_trivial_cyclez.TestGreenletSetParentErrors.test_trivial_cycle  sR    <(z* 	cDK	S]]+-BC	 	s   AA!c                     | j                  t              5 }t        j                         t        j                         _        d d d        | j                  t        j                        d       y # 1 sw Y   /xY w)Nz(cannot set the parent of a main greenletr   r  s     r   test_trivial_cycle_mainz3TestGreenletSetParentErrors.test_trivial_cycle_main  s_    ~. 	A#+3+>+>+@H!(	AS]]+-WX	A 	As   ,A00A9c                    t        d       }t        d       }t        d       }||_        ||_        | j                  t              5 }||_        d d d        | j	                  t        j                        d       y # 1 sw Y   /xY w)Nc                       y r$   r   r   r   r   r   z?TestGreenletSetParentErrors.test_deeper_cycle.<locals>.<lambda>  r   r   c                       y r$   r   r   r   r   r   z?TestGreenletSetParentErrors.test_deeper_cycle.<locals>.<lambda>  r   r   c                       y r$   r   r   r   r   r   z?TestGreenletSetParentErrors.test_deeper_cycle.<locals>.<lambda>  r   r   r  )r)   r   rD   r   r9   rG   rH   )r<   r+   r   g3r%   s        r   test_deeper_cyclez-TestGreenletSetParentErrors.test_deeper_cycle  st    &&&		z* 	cBI	S]]+-BC	 	s   A>>BN)r   r   r   r  r  r  r  r  r  r  r  r  r  r   r   r   r  r     s<    W2'R"HGG88DY	Dr   r  c                   J    e Zd Z eed      sd
dZd Zd Zd Zd Z	d Z
d	 Zy)TestReprassertEndsWithNc                 L    | j                  |j                  |      |||f       y r$   )r   endswith)r<   r!  suffixmsgs       r   r  zTestRepr.assertEndsWith  s     OOAJJv.FC0@Ar   c                 b    t        t        j                               }| j                  |d       y )N current active started main>)reprr   r   r  r<   rs     r   test_main_while_runningz TestRepr.test_main_while_running  s&    $$&'A>?r   c                     t        j                         fd}t        |      }|j                         }| j	                  |d       y )Nc                      t               S r$   )r$  r  s   r   rC   z-TestRepr.test_main_in_background.<locals>.run  s    :r    suspended active started main>)r   r   r)   r   r  )r<   rC   r*   r&  r  s       @r   test_main_in_backgroundz TestRepr.test_main_in_background  s>    ""$	 HHJA@Ar   c                 N    t        t                     }| j                  |d       y )Nz	 pending>)r$  r)   r  r%  s     r   test_initialzTestRepr.test_initial  s    A{+r   c                 &   t        j                          G fddt        j                        } |       }|j	                          |j                  d       | j                  |j                  d       | j                  |j                  d       t        d      D ]  }t        j                  d        t        d      D ]N  }| j                  |j                  j                         t        |j                        }| j                  |d       P y )	Nc                   $    e Zd ZdxZZdZ fdZy)/TestRepr.test_main_from_other_thread.<locals>.TNc                     t              | _        t        j                         | _        t        | j                        | _        y r$   )r$  original_mainr   r   	main_gletr  )r<   r  s    r   rC   z3TestRepr.test_main_from_other_thread.<locals>.T.run  s0    %)$Z"!)!4!4!6#'#7 r   )r   r   r   r2  r  r3  rC   r  s   r   Tr0    s    *..MKI8r   r4  rj   r*  r#  r1   gMbP?z (thread exited) dead>)r   r   rm   rn   ro   rp   r  r2  r  r;   r   r   r   r3  r[   r$  )r<   r4  r   r   r&  r  s        @r   test_main_from_other_threadz$TestRepr.test_main_from_other_thread  s    ""$	8	   	8 C			r
AOO-NOAMM+JK q 	AJJu	 q 	=AOOAKK,,-Q[[!A#;<	=r   c                    t        d       }|j                          | j                  t        |      d       | j	                  dt        |             | j	                  dt        |             | j	                  dt        |             y )Nc                       y r$   r   r   r   r   r   z$TestRepr.test_dead.<locals>.<lambda>  r   r   z dead>	suspendedstartedactive)r)   r   r  r$  assertNotInrQ   s     r   	test_deadzTestRepr.test_dead  se    %	
DGX.d1g.DG,47+r   c                     t        d       }t        d       }t        j                         }|||fD ]0  }| j                  |t               | j                  |t               2 y )Nc                       y r$   r   r   r   r   r   z>TestRepr.test_formatting_produces_native_str.<locals>.<lambda>  r   r   c                       y r$   r   r   r   r   r   z>TestRepr.test_formatting_produces_native_str.<locals>.<lambda>  r   r   )r)   r   r   r   rG   )r<   g_deadg_not_startedg_curr*   s        r   #test_formatting_produces_native_strz,TestRepr.test_formatting_produces_native_str  sf     \*#L1##%- 		A!! !!		r   r$   )r   r   r   r   r   r  r'  r+  r-  r5  r<  rC  r   r   r   r  r    s4    8-.	B@B,!=F,r   r  c                       e Zd Zd Zd Zd Zy)TestMainGreenletc                     dt        t        j                               v sJ t        t        j                               }dt        |      vsJ |S )Nr  )r$  r   r   r
  )r<   r   s     r   _check_current_is_mainz'TestMainGreenlet._check_current_is_main  sH    h1134444$$&'T!W$$$r   c                 b    | j                         }t        d|fi       }| j                  |       y )Nsubclass)rG  r
  rO   )r<   	main_typerI  s      r   )test_main_greenlet_type_can_be_subclassedz:TestMainGreenlet.test_main_greenlet_type_can_be_subclassed  s.    //1	
YL"5X&r   c                 t    | j                          | j                  t        j                         t               y r$   )rG  r   r   r   r)   r?   s    r   test_main_greenlet_is_greenletz/TestMainGreenlet.test_main_greenlet_is_greenlet  s'    ##%h113[Ar   N)r   r   r   rG  rK  rM  r   r   r   rE  rE    s    '
Br   rE  c                   <    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
y	)
TestBrokenGreenletsc                     d }t         j                  j                  |      }d|_        | j	                  t
        d      5  |j                          d d d        y # 1 sw Y   y xY w)Nc                      t        d      )NzNever get here)AssertionErrorr   r   r   r   z<TestBrokenGreenlets.test_failed_to_initialstub.<locals>.func  s     !122r   Tz;Failed to switch stacks into a greenlet for the first time.)r   	_greenletUnswitchableGreenletforce_switch_errorassertRaisesRegexSystemErrorr   )r<   r   r*   s      r   test_failed_to_initialstubz.TestBrokenGreenlets.test_failed_to_initialstub  sY    	3 33D9###K$ac 	HHJ	 	 	s    AA#c                    g fd}t         j                  j                  |      }|j                          | j	                  dg       |j                          | j	                  ddg       d|_        | j                  t        d      5  |j                          d d d        d|_        |j                          | j	                  g d       y # 1 sw Y   5xY w)Nc                      j                  d       t        j                         j                  j	                           j                  d       t        j                         j                  j	                           j                  d       y )Nr   r6   r1   r2   )runss   r   r   zDTestBrokenGreenlets.test_failed_to_switch_into_running.<locals>.func,  sZ    KKN!((//1KKN!((//1KKNr   r   r6   Tz0Failed to switch stacks into a running greenlet.F)r   r6   r1   )r   rS  rT  r   r9   rU  rV  rW  )r<   r   r*   r[  s      @r   "test_failed_to_switch_into_runningz6TestBrokenGreenlets.test_failed_to_switch_into_running*  s    	 33D9	
s#	
1v&###K$VX 	HHJ	  %	
y)	 	s   CCc                     | j                  d      }| j                  d|j                         | j                  |j                  | j	                                y )Nzfail_slp_switch.pyzfail_slp_switch is running)assertScriptRaisesrF   output
returncode,get_expected_returncodes_for_aborted_processr<   exs     r   &test_failed_to_slp_switch_into_runningz:TestBrokenGreenlets.test_failed_to_slp_switch_into_runningF  sA    $$%9:2BII>bmmT%V%V%XYr   c                     | j                  d      }| j                  d|       | j                  d|       | j                  d|       | j                  d|       | j                  d|       | j                  d|       y )Nzfail_switch_two_greenlets.pyz	In g1_runTRACEzLEAVE TRACEzFalling off end of mainzFalling off end of g1_runzFalling off end of g2
run_scriptrF   r<   r_  s     r   #test_reentrant_switch_two_greenletsz7TestBrokenGreenlets.test_reentrant_switch_two_greenletsL  sh    !?@k6*gv&mV,/816:-v6r   c                     | j                  dd      }| j                  d|j                         | j                  d|j                         y )Nzfail_switch_three_greenlets.py)r   )	exitcodesr   zpositional arguments)r^  rF   r_  rb  s     r   %test_reentrant_switch_three_greenletsz9TestBrokenGreenlets.test_reentrant_switch_three_greenletsV  s@     $$%EQU$Vk299-,bii8r   c                 J    | j                  d      }| j                  d|       y )Nzfail_switch_three_greenlets2.pya  RESULTS: [('trace', 'switch'), ('trace', 'switch'), ('g2 arg', 'g2 from tracefunc'), ('trace', 'switch'), ('main g1', 'from g2_run'), ('trace', 'switch'), ('g1 arg', 'g1 from main'), ('trace', 'switch'), ('main g2', 'from g1_run'), ('trace', 'switch'), ('g1 from parent', 'g1 from main 2'), ('trace', 'switch'), ('main g1.2', 'g1 done'), ('trace', 'switch'), ('g2 from parent', ()), ('trace', 'switch'), ('main g2.2', 'g2 done')]rg  ri  s     r   &test_reentrant_switch_three_greenlets2z:TestBrokenGreenlets.test_reentrant_switch_three_greenlets2_  s)     !BC= 		
r   c                 J    | j                  d      }| j                  d|       y )Nz#fail_initialstub_already_started.pya!  RESULTS: ['Begin C', 'Switch to b from B.__getattribute__ in C', ('Begin B', ()), '_B_run switching to main', ('main from c', 'From B'), 'B.__getattribute__ back from main in C', ('Begin A', (None,)), ('A dead?', True, 'B dead?', True, 'C dead?', False), 'C done', ('main from c.2', None)]rg  ri  s     r   4test_reentrant_switch_GreenletAlreadyStartedInPythonzHTestBrokenGreenlets.test_reentrant_switch_GreenletAlreadyStartedInPythonq  s'    !FG1
 	
r   c                 J    | j                  d      }| j                  d|       y )Nzfail_clearing_run_switches.pyzRESULTS [('G.__getattribute__', 'run'), ('RunCallable', '__del__'), ('main: g.switch()', 'from RunCallable'), ('run_func', 'enter')]rg  ri  s     r   *test_reentrant_switch_run_callable_has_delz>TestBrokenGreenlets.test_reentrant_switch_run_callable_has_del}  s'    !@A 	
r   N)r   r   r   rX  r\  rd  rj  rm  ro  rq  rs  r   r   r   rO  rO    s+    
*8Z79
$


r   rO  c                   L    e Zd Z ej                   eed      d      d        Zy)
TestModule_is_gil_enabledz,Needs 3.13 and above for sys._is_gil_enabledc                     t         r$| j                  t        j                                y | j	                  t        j                                y r$   )r   r   r   rv  r   r?   s    r   test_no_gil_on_free_threadedz'TestModule.test_no_gil_on_free_threaded  s3     'S0023OOC//12r   N)r   r   r   r  
skipUnlessr   r   rx  r   r   r   ru  ru    s0    X&78GI3I3r   ru  __main__)r   r   r   rm   r  abcr   r   r   r)   r  r   r   r	   r
   r   	leakcheckr   r   r  r   r!   r,   r.   r  r  rE  rO  ru  r   r  r   r   r   <module>r}     s    	 
       ,  "   ) & (		 	H(8 H(VjD( jDZSx SlBx B.k
( k
Z3 3 zHMMO r   