
    Ng                     l   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  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edk    r ej                     dS dS )    N)ABCMeta)abstractmethod)greenlet   )TestCase)RUNNING_ON_MANYLINUX)PY313)fails_leakcheckc                       e Zd ZdS )	SomeErrorN__name__
__module____qualname__     X/var/www/html/ai-engine/env/lib/python3.11/site-packages/greenlet/tests/test_greenlet.pyr   r      s        Dr   r   c                     	 t          j                    j                                         n2#  |                     t          j                    d                     xY wt          Nr   )r   
getcurrentparentswitchappendsysexc_infor   seens    r   fmainr       sV    $++----CLNN1%&&&
Os	   *- /Ac                 X    d }t          ||           }|                    |           d S )Nc                     | Nr   )excs    r   crasherzsend_exception.<locals>.crasher,   s    	r   r   RawGreenletr   )gr"   r#   g1s       r   send_exceptionr)   )   s6      	WQ	'	'	'BIIcNNNNNr   c                      e 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d Zd Z ej        eo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oed'          d(             Z,d) Z-d* Z.d+ Z/d, Z0d- Z1d. Z2d/ Z3d0S )2TestGreenletc                 b   g fd}t          |          }                    d           |                                                     d           |                                                     d           |                     t	          t          d                               d S )Nc                                            d           t          j                    j                                                               d           d S )Nr      r   r   r   r   r   lsts   r   fz'TestGreenlet._do_simple_test.<locals>.f7   sC    JJqMMM!!(//111JJqMMMMMr   r            )r&   r   r   assertEquallistrange)selfr2   r'   r1   s      @r   _do_simple_testzTestGreenlet._do_simple_test4   s    	 	 	 	 	 NN

1	




1	




1d588nn-----r   c                 .    |                                   d S r!   )r:   r9   s    r   test_simplezTestGreenlet.test_simpleC   s    r   c                     t                      }|                     t                    5 }|                                 d d d            n# 1 swxY w Y   |                     dt          |j                             d S Nrun)r&   assertRaisesAttributeErrorr   assertInstr	exceptionr9   r'   r"   s      r   (test_switch_no_run_raises_AttributeErrorz5TestGreenlet.test_switch_no_run_raises_AttributeErrorF   s    MM~.. 	#HHJJJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	eS//00000s   A

AAc                 
   t                      }|                     t                    5 }|                    t                     d d d            n# 1 swxY w Y   |                     dt          |j                             d S r?   )r&   rA   rB   throwr   rC   rD   rE   rF   s      r   'test_throw_no_run_raises_AttributeErrorz4TestGreenlet.test_throw_no_run_raises_AttributeErrorM   s    MM~.. 	#GGI	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	eS//00000s   AAAc                     t          d           }|                     |           |                     |j        t	          j                               d S )Nr$   )r&   assertIsNotNoneassertIsr   r   r   r9   r'   s     r   test_parent_equals_Nonez$TestGreenlet.test_parent_equals_NoneT   sK    t$$$Qah 3 5 566666r   c                     t          d           }|                     |           |                     |j                   d S )N)r@   )r&   rL   assertIsNoner@   rN   s     r   test_run_equals_Nonez!TestGreenlet.test_run_equals_NoneY   sB    D!!!Q!%     r   c                 v   g fd}t          |          }t          |          }|                                 |                     t                    d           |                                 |                     t                    d           |                                 |                     t                    d           |                     |j        d           |                                 |                     t                    d           |                     |j        d           d S )Nc                                            d           t          j                    j                                                              ddg           d S )Nr   )r   r   r   r   r   extendr0   s   r   r2   z)TestGreenlet.test_two_children.<locals>.fa   sL    JJqMMM!!(//111JJ1vr   r   r3   r4   T   )r&   r   r6   lendead)r9   r2   r'   hr1   s       @r   test_two_childrenzTestGreenlet.test_two_children^   s   	 	 	 	 	 NNNN	


S1%%%	


S1%%%	


S1%%%&&&	


S1%%%&&&&&r   c                 :   g fdfd}t          |          }|                     t          j        |          d           |                                 |                     g d           |                     t          j        |          d           d S )Nc                                            d           t          j                    j                                         d S )Nbr/   r0   s   r   r2   z3TestGreenlet.test_two_recursive_children.<locals>.fu   s6    JJsOOO!!(//11111r   c                                           d           t                    } |                                                       d           d S )Nac)r   r&   r   )r'   r2   r1   s    r   r'   z3TestGreenlet.test_two_recursive_children.<locals>.gy   s=    JJsOOOAAHHJJJJJsOOOOOr   r3   )r_   r]   r`   )r&   r6   r   getrefcountr   )r9   r'   r2   r1   s     @@r   test_two_recursive_childrenz(TestGreenlet.test_two_recursive_childrenr   s    	2 	2 	2 	2 	2	 	 	 	 	 	 NN++Q///	


ooo...++Q/////r   c                     g  fdfdt          d          D             }|D ]}|                                 |D ]}|                    d                                t	                    t	          |                     d S )Nc                  Z                                                           d           d S NT)r:   r   )r9   successs   r   r2   z$TestGreenlet.test_threads.<locals>.f   s-      """NN4     r   c                 :    g | ]}t          j                   S )target)	threadingThread).0ir2   s     r   
<listcomp>z-TestGreenlet.test_threads.<locals>.<listcomp>   s'    ===ayq)))===r   
   )r8   startjoinr6   rW   )r9   thsthr2   rf   s   `  @@r   test_threadszTestGreenlet.test_threads   s    	! 	! 	! 	! 	! 	! >===599=== 	 	BHHJJJJ 	 	BGGBKKKKWs3xx00000r   c                 z   g }t          t                    }t          t                    }|                    |           |                    |           ||_        |                     |g            |                     t          |j                   |                     |t          g           |                                }|                     |d           |                     |t          g           |                    d          }|                     |d           |                     |t          g           d S )Nr      )r&   r   r   r   r6   rA   r   )r9   r   r(   g2values        r   test_exceptionzTestGreenlet.test_exception   s   
		$
		$	r""" 	)RY///	{+++		###	{+++		"###	{+++++r   c                     g }t          t                    }|                    |           |                     t          t
          |t                     |                     |t          g           d S r!   )r&   r   r   rA   KeyErrorr)   r6   )r9   r   r(   s      r   test_send_exceptionz TestGreenlet.test_send_exception   s[    
		$(NBAAAz*****r   c                    g }t          t                    }t          t                    }|                    |           |                    |           |                     |g            ~t	          j                     |                     |t          j        g           ~t	          j                     |                     |t          j        t          j        g           d S r!   )r&   r   r   r6   gccollectr   GreenletExit)r9   r   r(   rw   s       r   test_dealloczTestGreenlet.test_dealloc   s    
		$
		$r"""

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

 5x7LMNNNNNr   c                    d }t          |          }|                                 t          j        }ddlm}  |            x}t          _        	 ~|t          _        n# |t          _        w xY w|                                }|                     d|           |                     d|           |                     d|           d S )Nc                      	 t          j                    j                                         d S # t           j        $ r	 t
          d w xY wr!   )r   r   r   r   r   r   r   r   r   r@   zHTestGreenlet.test_dealloc_catches_GreenletExit_throws_other.<locals>.run   sQ    *#%%,3355555( * * *T)*s	   *. Ar   )StringIO	Exceptionignoredr   )r&   r   r   stderrior   getvaluerC   )r9   r@   r'   	oldstderrr   r   vs          r   .test_dealloc_catches_GreenletExit_throws_otherz;TestGreenlet.test_dealloc_catches_GreenletExit_throws_other   s    	* 	* 	* 	



 J	&hjj(	#"CJJCJ""""OOk1%%%i###k1%%%%%s   A A,z=Sometimes flaky (getting one GreenletExit in the second list)c                 |   g g t          j                    t          j                    t          j                    t          j                    fd}t          j        |          }|                                                     d           |                     g            |                     t                    d           d d = t          j                     |                     g            	                                                     d           	 |                     t          j        g           	                                 |                    d           d d = d d = d S # 	                                 |                    d           d d = d d = w xY w)Nc                     t          t                    } |                                                    |            ~ t	          j                                                                          d           t                                                                            d           t                       d S )Nr.   )r&   r   r   r   r~   r   setwait)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   r2   z1TestGreenlet.test_dealloc_other_thread.<locals>.f   s    U##BIIdOOONN2JLLL;??AAA  ###MMM""$$$ %%a(((MMMMMr   rh   ro   r   r.   )rj   Eventrk   rp   r   r6   rW   r~   r   r   r   r   rq   )	r9   r2   tr   r   r   r   r   r   s	      @@@@@@r   test_dealloc_other_threadz&TestGreenlet.test_dealloc_other_thread   s    :C/:K:K7#/++&_..(00	 	 	 	 	 	 	 	 	 	 A&&&				7<<R@@@r"""Wq)))AAAJ

r""""""	TH$9#:;;; $$&&&FF2JJJQQQ


 !$$&&&FF2JJJQQQ
NNNNs   0!F 5F;c                 n     fd}t          |          }|                                }                     ||j        u                                 |           |                                }                     |                                |d                                |j        d            d S )Nc                      t          j        d          }                     | j        d            t	          j                    j                            |            dS )Nr   meaning of life)r   	_getframer6   f_backr   r   r   r   )r2   r9   s    r   f1z#TestGreenlet.test_frame.<locals>.f1  sP    a  AQXt,,,!!(//222$$r   r   )r&   r   
assertTruegr_frameassertFalser6   )r9   r   r'   framefrom_gs   `    r   
test_framezTestGreenlet.test_frame  s    	% 	% 	% 	% 	%
 OO

+,,,!2333T*****r   c                    d }t          j        |d          }t          j        |d          }|                                 |                                 |                    d           |                    d           d S )Nc                 T     t           fd          }|                                 d S )Nc                  ,    t          j                   S r!   )timesleepxs   r   <lambda>z>TestGreenlet.test_thread_bug.<locals>.runner.<locals>.<lambda>$  s    DJqMM r   r%   )r   r'   s   ` r   runnerz,TestGreenlet.test_thread_bug.<locals>.runner#  s+    111122AHHJJJJJr   )g?ri   args)g333333?ro   )rj   rk   rp   rq   )r9   r   t1t2s       r   test_thread_bugzTestGreenlet.test_thread_bug"  sy    	 	 	 V&999V&999









r   c                       fd}t          |                              dd          }                     |d           d S )Nc                 `                         | d                                |d           dS )Nr4   r3   *   )r6   )r_   r]   r9   s     r   r@   z,TestGreenlet.test_switch_kwargs.<locals>.run.  s4    Q"""Q"""2r   r4   r3   )r_   r]   r   r&   r   r6   )r9   r@   r   s   `  r   test_switch_kwargszTestGreenlet.test_switch_kwargs-  sZ    	 	 	 	 	 ##a1#--Br   c                 (   d }t          |          }|                     ddi|                    d                     |                     dddif|                                           |                     d|                                           d S )Nc                     t          j                    j                            |            t          j                    j                            dd           | | dz  fS )Nr   r3   r.   r   r   r   r   r   s    r   r@   z6TestGreenlet.test_switch_kwargs_to_parent.<locals>.run6  sX    !!(//!/444!!(//Q/777a1f9r   r   r.   )r3   )r.   	   )r&   r6   r   )r9   r@   r'   s      r   test_switch_kwargs_to_parentz)TestGreenlet.test_switch_kwargs_to_parent5  s    	 	 	 #q188A;;///$a)188::666,,,,,r   c                    i t          j                    t          j                    fd}t          j        |          }|                                                     d           |                     t          j                  5  d                                          d d d            n# 1 swxY w Y   	                                 |
                    d                                            d S )Nc                      t          d           d<                                                         d           d S )Nc                      d S r!   r   r   r   r   r   zITestGreenlet.test_switch_to_another_thread.<locals>.run.<locals>.<lambda>E      D r   r'   ro   r&   r   r   created_eventdata
done_events   r   r@   z7TestGreenlet.test_switch_to_another_thread.<locals>.runD  @    #LL11DIOOBr   rh   ro   r'   )rj   r   rk   rp   r   rA   r   errorr   r   rq   clear)r9   r@   threadr   r   r   s      @@@r   test_switch_to_another_threadz*TestGreenlet.test_switch_to_another_thread?  s.   !))_&&
	  	  	  	  	  	  	  !---2x~.. 	 	I	 	 	 	 	 	 	 	 	 	 	 	 	 	 	B

s   B77B;>B;c                 b      fd} fdt          |                                           d S )Nc                      	 t          d          #  t          j                    } t                                                                         | t          j                               Y d S xY w)Nfun)
ValueErrorr   r   r&   r   r6   )r   rY   r9   s    r   r2   z&TestGreenlet.test_exc_state.<locals>.fS  sg    ; ''';<>>A%%'''  3<>>::::::s
    AA2c                  V                          t          j                    d           d S )N)NNN)r6   r   r   r<   s   r   rY   z&TestGreenlet.test_exc_state.<locals>.h[  s&    S\^^-?@@@@@r   r%   )r9   r2   rY   s   ` @r   test_exc_statezTestGreenlet.test_exc_stateR  s`    	; 	; 	; 	; 	; 	;	A 	A 	A 	A 	A 	Ar   c                    d }d }d }t          |          }|                     |j        i            |                                 |                     |j        d           |                     |j        ddi           |j        |_        |                     |j        ddi           |                     t          ||           |                     t          ||d           d S )Nc                  6    dt          j                    _        d S Nr   )r   r   testr   r   r   r2   z*TestGreenlet.test_instance_dict.<locals>.fa  s    )+H!!&&&r   c                 
    | ` d S r!   __dict__)r'   s    r   deldictz0TestGreenlet.test_instance_dict.<locals>.deldictc  s    


r   c                     || _         d S r!   r   )r'   rx   s     r   setdictz0TestGreenlet.test_instance_dict.<locals>.setdicte  s    AJJJr   r   r   )r&   r6   r   r   r   rA   	TypeError)r9   r2   r   r   r'   s        r   test_instance_dictzTestGreenlet.test_instance_dict`  s    	, 	, 	,	 	 		 	 	NNR(((	


$$$fb\222Z
fb\222)Wa000)Wa44444r   c                     g fd}t          |          }|                                 |                     dg           d S )Nc                  p                          t          t          j                    d                     d S r?   )r   hasattrr   r   )has_runs   r   funcz;TestGreenlet.test_running_greenlet_has_no_run.<locals>.funcs  s9    NN+--u55    r   Fr   )r9   r   r'   r   s      @r    test_running_greenlet_has_no_runz-TestGreenlet.test_running_greenlet_has_no_runq  s[    	 	 	 	 	
 	


5'*****r   c                     dd l }|                     t          |j         t                                 |                     t          |j        t                                 d S r   )copyrA   r   r&   deepcopy)r9   r   s     r   test_deepcopyzTestGreenlet.test_deepcopy|  sM    )TY>>>)T]KMMBBBBBr   c                 f   t          d           t          j                    g fd}t          |          }|                                 ~|                                |                     d                    |                     d         j                   d d = d d d S )Nc                      d S r!   r   r   r   r   r   z;TestGreenlet.test_parent_restored_on_kill.<locals>.<lambda>  s    $ r   c                  (   	                                   d S # t          j        $ ri                     t          j                    j                                       t          j                                                                  Y d S 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33:;;;h133444

	s    A4BBr$   r   r   )r&   r   r   r   r   rM   r   )r9   r   r'   r   r   r   s      @@@r   test_parent_restored_on_killz)TestGreenlet.test_parent_restored_on_kill  s    ,,''"$$	 	 	 	 	 	 	 s+++	


fQi&&&fQi&,,, 111Ir   c                     t                      }t          d |          }|                     t                    5  |                                 d d d            d S # 1 swxY w Y   d S )Nc                      d S r!   r   r   r   r   r   z9TestGreenlet.test_parent_return_failure.<locals>.<lambda>       r   r$   )r&   rA   rB   r   )r9   r(   rw   s      r   test_parent_return_failurez'TestGreenlet.test_parent_return_failure  s    ]]b111~.. 	 	IIKKK	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AA!$A!c                      G d dt                     } |d           }|                     t          |j        t                                 d S )Nc                       e Zd Zd ZdS )>TestGreenlet.test_throw_exception_not_lost.<locals>.mygreenletc                 J    	 t           #  Y nxY wt          j        | |          S r!   )r   r&   __getattribute__r9   names     r   r   zOTestGreenlet.test_throw_exception_not_lost.<locals>.mygreenlet.__getattribute__  s-    #OD"3D$???s   	 Nr   r   r   r   r   r   r   
mygreenletr     s(        @ @ @ @ @r   r   c                      d S r!   r   r   r   r   r   z<TestGreenlet.test_throw_exception_not_lost.<locals>.<lambda>      t r   )r&   rA   r   rI   )r9   r   r'   s      r   test_throw_exception_not_lostz*TestGreenlet.test_throw_exception_not_lost  sk    	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ J||$$)QWikk:::::r   Fc                    g d fd}t          j        |          }|                                 |                    d           ~r|                                  |                     t          j                  5 }d                             t                     d d d            n# 1 swxY w Y   s2t          |j                  }|                     |dk    pd|v            n(|                     t          |j                  d           t          d         j        d          rQ|                     t                     5  d         j                                         d d d            n# 1 swxY w Y   sEt          d         j        j        d          r$d         j        j                                         n |                     d         j                   ~d d d = d	| _        d S )
Nc                  Z    t          j                    j                                         d S r!   r   r   r   r   r   zGTestGreenlet._do_test_throw_to_dead_thread_doesnt_crash.<locals>.worker  &    !!(//11111r   c                      t                    } |                                                      |            r)|                                  t          j                     d S d S r!   )r&   r   r   r   r   )r'   r   wait_for_cleanupr   s    r   creatorzHTestGreenlet._do_test_throw_to_dead_thread_doesnt_crash.<locals>.creator  sc    F##AHHJJJMM! &


#%%%%%& &r   rh   ro   r   Bcannot switch to a different thread (which happens to have exited)zCannot switchr   T)rj   rk   rp   rq   wait_for_pending_cleanupsrA   r   r   rI   r   rD   rE   r   r6   r   r   RuntimeErrorr   f_localsrQ   expect_greenlet_leak)r9   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 	2 	2	& 	& 	& 	& 	& 	& 	& G,,,					r


  	-**,,,x~.. 	'#1IOOI&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	'   
	CM""AOOYY ("a'   
 CM""T  
 6!9%w// 	+""<00 + +q	"((***+ + + + + + + + + + + + + + +   	2vay)2G<< 4q	"+11333fQi0111111I %)!!!s$   !B11B58B5 E99E= E=c                 .    |                                   d S r!   r  r<   s    r   &test_throw_to_dead_thread_doesnt_crashz3TestGreenlet.test_throw_to_dead_thread_doesnt_crash  s    7799999r   c                 0    |                      d           d S re   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                     } |            }|                     |                    d          d           d| _        d S )Nc                        e Zd Zd Zd Zd ZdS )7TestGreenlet.test_recursive_startup.<locals>.convolutedc                 <    t          j        |            d| _        d S r   )r&   __init__countr<   s    r   r  z@TestGreenlet.test_recursive_startup.<locals>.convoluted.__init__  s    $T***


r   c                     |dk    r'| j         dk    rd| _         |                     d           t          j        | |          S )Nr@   r   r   +   )r  r   r&   r   r   s     r   r   zHTestGreenlet.test_recursive_startup.<locals>.convoluted.__getattribute__  s>    5==TZ1__!"DJKKOOO"3D$???r   c                 :    	 | j                             |           r!   r   r   )r9   rx   s     r   r@   z;TestGreenlet.test_recursive_startup.<locals>.convoluted.run  s"    .K&&u---.r   N)r   r   r   r  r   r@   r   r   r   
convolutedr    sD          @ @ @
. . . . .r   r  r   r  T)r&   r6   r   r	  )r9   r  r'   s      r   test_recursive_startupz#TestGreenlet.test_recursive_startup  si    	. 	. 	. 	. 	. 	. 	. 	. JLL"r***
 %)!!!r   c                    t          j                                                     t          j                                                      G fddt                    }d }fd}t	          j                    }t          |          g |            d         _        d                                          t          j	        |          }|
                                                                                                   |                     t	          j                    |           |                    d           d S )Nc                       e Zd Z fdZdS );TestGreenlet.test_threaded_updatecurrent.<locals>.finalizedc                 X                                                                       d S r!   releaseacquire)r9   lock1lock2s    r   __del__zCTestGreenlet.test_threaded_updatecurrent.<locals>.finalized.__del__  s#     r   Nr   r   r   r&  r$  r%  s   r   	finalizedr    s3                       r   r)  c                  Z    t          j                    j                                         d S r!   r   r   r   r   deallocatorz=TestGreenlet.test_threaded_updatecurrent.<locals>.deallocator  r  r   c                                                        t          j                      d=                                                                    t          j                                                      d S r   r#  r   r   r"  )r'   r$  r%  s   r   fthreadz9TestGreenlet.test_threaded_updatecurrent.<locals>.fthread  s`    MMOOO!!!!MMOOOMMOOO!!!MMOOOOOr   r   rh   ro   )rj   Lockr#  objectr   r   r&   bombr   rk   rp   r"  r6   rq   )	r9   r)  r+  r.  r   r   r'   r$  r%  s	         @@@r   test_threaded_updatecurrentz(TestGreenlet.test_threaded_updatecurrent  sT       	  	  	  	  	  	  	  	  	  	  	 	2 	2 	2	 	 	 	 	 	 	 "$$%%&IKK!		!G,,,				 	,..555	r




r   c                 "   g fdd t                    gd                                          t          d                                                   }|                                |                     |d           d S )Nc                     t          j                    j                                        } d= t          j                    j        _        	 t          j                    j                            |                                t          j                               d S #                     t          j                               w xY wr   )r   r   r   r   r   )rx   	initiatorr   r   s    r   r   z>TestGreenlet.test_dealloc_switch_args_not_lost.<locals>.worker8  s    '))07799Eq	'244;I3#%%,33E:::H/1122222H/112222s   +B   (Cc                      dS r   r   r   r   r   r5  zATestGreenlet.test_dealloc_switch_args_not_lost.<locals>.initiatorE      2r   r   r   )r&   r   r   r6   )r9   rx   r5  r   r   s     @@@r   !test_dealloc_switch_args_not_lostz.TestGreenlet.test_dealloc_switch_args_not_lost6  s    	3 	3 	3 	3 	3 	3 	3	 	 	 f%%&q		6!955	  ""#####r   c                     d  G d dt                     } |            t          d          fd}t          |          }|                     |                                           d S )Nc                      | |i | d S r!   r   )r   r_   ks      r   _applyz0TestGreenlet.test_tuple_subclass.<locals>._apply^  s    D!MqMMMMMr   c                       e Zd Zd ZdS )1TestGreenlet.test_tuple_subclass.<locals>.mytuplec                     t          j                                                     t                              |           S r!   )r   r   r   tuple__len__r<   s    r   rA  z9TestGreenlet.test_tuple_subclass.<locals>.mytuple.__len__b  s/    #%%,,...}}T***r   N)r   r   r   rA  r   r   r   mytupler>  a  s#        + + + + +r   rB  r   )r_   c                  X      t          j                    j        j                   d S r!   r   )r<  r   kwargss   r   switchapplyz5TestGreenlet.test_tuple_subclass.<locals>.switchapplyg  s,    F8&((/6fEEEEEr   )r@  dictr&   r6   r   )r9   rB  rE  r'   r<  r   rD  s       @@@r   test_tuple_subclassz TestGreenlet.test_tuple_subclassP  s    	 	 		+ 	+ 	+ 	+ 	+e 	+ 	+ 	+ wyy	F 	F 	F 	F 	F 	F 	F$$V,,,,,r   c                     t          dt          fdt          d           i          } G d d|          } G d d|          } |             |                     t          |           d S )NAbstractSubclassr@   c                     d S r!   r   r<   s    r   r   z7TestGreenlet.test_abstract_subclasses.<locals>.<lambda>p  s     r   c                       e Zd ZdS ):TestGreenlet.test_abstract_subclasses.<locals>.BadSubclassNr   r   r   r   BadSubclassrL  r          Dr   rM  c                       e Zd Zd ZdS );TestGreenlet.test_abstract_subclasses.<locals>.GoodSubclassc                     d S r!   r   r<   s    r   r@   z?TestGreenlet.test_abstract_subclasses.<locals>.GoodSubclass.runv  s    r   N)r   r   r   r@   r   r   r   GoodSubclassrP  u  s#            r   rR  )r   r&   r   rA   r   )r9   rI  rM  rR  s       r   test_abstract_subclassesz%TestGreenlet.test_abstract_subclassesl  s    "NN#4#45568 8
	 	 	 	 	* 	 	 		 	 	 	 	+ 	 	 	 	)[11111r   c                      t          j                    sd S t          j                    d         dk     rd S  fd}t          d          D ]} |            r d S d S )Nr   2   c                  L  	
 t          j                    

                                 t          j                                                     dg
fd} t          j        |           }|                                  G 
fddt
                    	 G 	fddt
                    }g }t          dz            }t          j                    } |            }|D ]Q}d }d	         r|	                    d
            dS t                      }d	         r nH|                    |           Rt          j                     d	         r|	                    d
           dS                     |j        |           |D ]}                    |j        |           dS )NFc                  ~                                       t          j                                                      d S r!   r-  r(  s   r   another_threadzWTestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.another_thread  s0    #%%%r   rh   c                       e Zd Z fdZdS )TTestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.gc_callbackc                 b                                                                       dd<   d S )NTr   r!  )r9   r$  r%  recycleds    r   r&  z\TestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.gc_callback.__del__  s*    MMOOOMMOOO"&HQKKKr   Nr'  )r$  r%  r\  s   r   gc_callbackrZ    s8        ' ' ' ' ' ' ' ' 'r   r]  c                       e Zd Z fdZdS )PTestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.garbagec                 4    | | _                      | _        d S r!   )cyclecallback)r9   r]  s    r   r  zYTestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.garbage.__init__  s    !%DJ$/KMMDMMMr   N)r   r   r   r  )r]  s   r   garbager_    s.        2 2 2 2 2 2 2r   rc  r3   r   ro   T)rj   r/  r#  rk   rp   r0  r8   r   r   rq   r&   r   r~   r   r6   r   )rX  r   rc  lr   currentr'   _lastr]  r$  r%  r\  Nr9   s            @@@@r   attemptz?TestGreenlet.test_implicit_parent_with_threads.<locals>.attempt  s   N$$EMMOOON$$EMMOOOwH             777AGGIII' ' ' ' ' ' ' ' 'f ' ' '
2 2 2 2 2 2 2& 2 2 2 Aac

A)++G		A  A; !FF2JJJ 55"}}A; E 
A; FF2JJJuT['222 4 4  733334r   r5   )r~   	isenabledget_thresholdr8   )r9   ri  rf  rh  s   `  @r   !test_implicit_parent_with_threadsz.TestGreenlet.test_implicit_parent_with_threads|  s    |~~ 	Fq!r66F,	 ,	 ,	 ,	 ,	 ,	Z q 	 	Awyy 	 	r   c                 @    ddl m ddl m  G d dt                    }t	          j        |          }g  _         fd}t          d          D ]} ||                                             ` 	                    t	          j        |          |           d S )Nr   r   )r   c                       e Zd ZdS )TTestGreenlet.test_issue_245_reference_counting_subclass_no_threads.<locals>.GreenletNr   r   r   r   Greenletrp    rN  r   rq  c                      	              j                                          d S #  $ r& j                                                    Y d S w xY wr!   )r   r   gletsr   )r   r   r9   s   r   greenlet_mainzYTestGreenlet.test_issue_245_reference_counting_subclass_no_threads.<locals>.greenlet_main  si    0
#**,,,,, 0 0 0
!!**,,//////0s   !& 'AAro   )
r   r   r   r&   r   ra   rs  r8   r   r6   )r9   rq  initial_refsrt  rf  r   r   s   `    @@r   5test_issue_245_reference_counting_subclass_no_threadszBTestGreenlet.test_issue_245_reference_counting_subclass_no_threads  s     	('''''))))))	 	 	 	 	{ 	 	 	 x00 
	0 	0 	0 	0 	0 	0 	0 r 	- 	-AH]##**,,,,J22LAAAAAr   z;The manylinux images appear to hang on this test on 3.13rc2c                 D  
 ddl m} ddl m} ddlm  G d dt
                    
g  |            fd
fd}t          j        
          }g }t          |d	z             D ]C} |            } |||f
          }|	                    |           |
                                 D|D ]}	|	                    d           d d =                                  |                                  |                     t          j        
          |           d S )Nr   )rk   )r   rn  c                       e Zd ZdS )STestGreenlet.test_issue_245_reference_counting_subclass_threads.<locals>.MyGreenletNr   r   r   r   
MyGreenletry    rN  r   rz  c                  J                  j                                          d S r!   r  rn  s   r   rt  zVTestGreenlet.test_issue_245_reference_counting_subclass_threads.<locals>.greenlet_main  s$    JLL&&(((((r   c                                }                     |           |                                 ~|                                                      d                         d S )Nro   )r   r   r   r   )greenlet_running_eventminerz  r   rs  rt  ref_cleareds     r   thread_mainzTTestGreenlet.test_issue_245_reference_counting_subclass_threads.<locals>.thread_main  sm    :m,,DLLKKMMM"&&((( R    JLLLLLr   -   r   ro   )rj   rk   r   r   r   r&   r   ra   r8   r   rp   r   r   r  r6   )r9   rk   r   r  ru  thread_ready_eventsrf  eventr   r   rz  r   rs  rt  r  s             @@@@@r   2test_issue_245_reference_counting_subclass_threadsz?TestGreenlet.test_issue_245_reference_counting_subclass_threads  s    	%$$$$$######''''''	 	 	 	 	 	 	 	 egg	) 	) 	) 	) 	)	 	 	 	 	 	 	 	 	8 z22 r!
 
 	 	A EGGEV;eX>>>F&&u---LLNNNN . 	  	 JOOB !!!H 	&&(((44lCCCCCr   c                     d }t          |          }d }t          ||          }|                     t                    5  |                                 d d d            d S # 1 swxY w Y   d S )Nc                      dS )N   r   r   r   r   no_argsz\TestGreenlet.test_falling_off_end_switches_to_unstarted_parent_raises_error.<locals>.no_args  r7  r   c                      dS r   r   r   r   r   leafzYTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_raises_error.<locals>.leaf"  r7  r   )r&   rA   r   r   )r9   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_error  s    	 	 	  +733	 	 	 D"677 y)) 	 	LLNNN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s    A""A&)A&c                     d }t          |          }d }t          ||          }|                                }|                     |d           d S )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                      dS r   r   r   r   r   r  zRTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_works.<locals>.leaf1  r7  r   )r   r  r   )r9   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+  sk    	 	 	  +733	 	 	 D"677*****r   c                     G d dt                     } |            }g t          fd|          }|                     t                    5  |                                 d d d            n# 1 swxY w Y   |                     dg           |                     t                    5  |                                 d d d            n# 1 swxY w Y   |                     dg           d S )Nc                       e Zd Zd ZdS )XTestGreenlet.test_switch_to_dead_greenlet_with_unstarted_perverse_parent.<locals>.Parentc                      |dk    rt           d S r?   )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                  .                          d          S r   r   r   s   r   r   zZTestGreenlet.test_switch_to_dead_greenlet_with_unstarted_perverse_parent.<locals>.<lambda>B      DKKOO r   r   )r&   rA   r   r   r6   )r9   r  r  r  r   s       @r   ;test_switch_to_dead_greenlet_with_unstarted_perverse_parentzHTestGreenlet.test_switch_to_dead_greenlet_with_unstarted_perverse_parent9  s   	$ 	$ 	$ 	$ 	$[ 	$ 	$ 	$  &vxx33335IJJ y)) 	 	LLNNN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	t$$$y)) 	 	LLNNN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	t$$$$$s$   A//A36A3+CCCc                 :   g t          fd          }t          fd          }|                                 |                     dg           ||_        |                                }|                     |           |                     ddg           d S )Nc                  .                          d          S r  r  r   s   r   r   zDTestGreenlet.test_switch_to_dead_greenlet_reparent.<locals>.<lambda>P  s    4;;r?? r   c                  .                          d          S r   r  r   s   r   r   zDTestGreenlet.test_switch_to_dead_greenlet_reparent.<locals>.<lambda>Q  r  r   r   r  )r&   r   r6   r   rQ   )r9   r  r  r   r   s       @r   %test_switch_to_dead_greenlet_reparentz2TestGreenlet.test_switch_to_dead_greenlet_reparentN  s    *+B+B+B+BCC333344t$$$+&!!!Bx(((((r   c                 *   t          j                    fd}fdt          |          }|                                 t          d           }|                                 |                     |j                   |                     |j        j        j        d           |                     |j        j	                   |                     |j        j	        j        j        d           | 
                    |j        j	        j	                   d S )Nc                                     d S r!   r   inners   r   outerzHTestGreenlet.test_can_access_f_back_of_suspended_greenlet.<locals>.outerb  s    EGGGGGr   c                  V                          t          j        d                     d S r   )r   r   r   r   s   r   r  zHTestGreenlet.test_can_access_f_back_of_suspended_greenlet.<locals>.innere  s%    KKa(()))))r   c                      d S r!   r   r   r   r   r   zKTestGreenlet.test_can_access_f_back_of_suspended_greenlet.<locals>.<lambda>n       r   r  r  )r   r   r&   r   rL   r   r6   f_codeco_namer   rQ   )r9   r  r   	unrelatedr  r   s       @@r   ,test_can_access_f_back_of_suspended_greenletz9TestGreenlet.test_can_access_f_back_of_suspended_greenlet\  s    "$$	 	 	 	 		* 	* 	* 	* 	* %  

  --	 	S\***,4g>>>S\0111,3;WEEE#,-455555r   c                    ddl m ddlm fdt	                    }|                    d           |j        }t          d          D ]*}|                     |j	        d         |           |j
        }+|                     |j	        d         d           |                     |j
                   |                     |                    d          d	           d S )
Nr   )partialr   )_test_extension_cppc                     | dk    r%|                       | dz
                      z  S t          j                    j                                        S )Nr   r   )	test_callr   r   r   r   )r   r  r  recurses    r   r  z@TestGreenlet.test_get_stack_with_nested_c_calls.<locals>.recurse}  sT    1uu.88!a%9P9PQQQQ&((/66888r   r5   r   ro   i  )	functoolsr   r  r&   r   r   r8   r6   r  r   rQ   )r9   grr   rm   r  r  r  s       @@@r   "test_get_stack_with_nested_c_callsz/TestGreenlet.test_get_stack_with_nested_c_callsy  s   %%%%%%))))))	9 	9 	9 	9 	9 	9 	9
 !!
		!q 	! 	!AU^C0!444LEE,a000%,'''2-----r   c                 F   t          j                    fd}fdt          |          }|                                }t          d           }|                                 |                     |j        j        d           |                     |j                   d S )Nc                  B      t          j        d                     d S r   )r   r   r  s   r   r  z6TestGreenlet.test_frames_always_exposed.<locals>.outer  s#    E#-""#####r   c                 2                         |            d S r!   )r   )r   r   s    r   r  z6TestGreenlet.test_frames_always_exposed.<locals>.inner  s    KKr   c                      d S r!   r   r   r   r   r   z9TestGreenlet.test_frames_always_exposed.<locals>.<lambda>  r  r   r  )	r   r   r&   r   r6   r  r  rQ   r   )r9   r  r  r   r  r  r   s        @@r   test_frames_always_exposedz'TestGreenlet.test_frames_always_exposed  s     "$$	$ 	$ 	$ 	$ 	$	 	 	 	 	 		  --	-w777%,'''''r   N)F)4r   r   r   r:   r=   rG   rJ   rO   rR   rZ   rb   rt   ry   r|   r   r   unittestskipIfr	   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r  r  r  r  r2  r8  rG  rS  rl  rv  r  r  r  r  r  r  r  r  r   r   r   r+   r+   2   s7       . . .  1 1 11 1 17 7 7
! ! !
' ' '(0 0 0(1 1 1, , ,8+ + +O O O& & &6 X_&&G * * *X+ + + 	 	 	     - - -  &     5 5 5"	+ 	+ 	+C C C
  6  	; 	; 	; 7) 7) 7) _7)r : : _:> > > ) ) _)*- - -^$ $ $4- - -82 2 2 5 5 5nB B B> X_&&E AD AD ADF  + + +% % %*) ) )6 6 6:. . .&( ( ( ( (r   r+   c                   J    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 ZdS )TestGreenletSetParentErrorsc                 R   i t          j                    t          j                    fd}d }t          j        |          }|                                                     d           t          |          }|                                 |                     t                    5 }d         |_	        d d d            n# 1 swxY w Y   
                                 |                    d           |                     t          |j                  d           d S )Nc                      t          d           d<                                                         d           d S )Nc                      d S r!   r   r   r   r   r   zQTestGreenletSetParentErrors.test_threaded_reparent.<locals>.run.<locals>.<lambda>  r   r   r'   ro   r   r   s   r   r@   z?TestGreenletSetParentErrors.test_threaded_reparent.<locals>.run  r   r   c                  Z    t          j                    j                                         d S r!   r   r   r   r   blankzATestGreenletSetParentErrors.test_threaded_reparent.<locals>.blank  r  r   rh   ro   r'   z&parent cannot be on a different thread)rj   r   rk   rp   r   r&   r   rA   r   r   r   rq   r6   rD   rE   )	r9   r@   r  r   r'   r"   r   r   r   s	         @@@r   test_threaded_reparentz2TestGreenletSetParentErrors.test_threaded_reparent  s]   !))_&&
	  	  	  	  	  	  	 
	2 	2 	2 !---2	


z** 	!cCyAH	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!BS]++-UVVVVVs   1CCCc                     g fd}t          j        |          }|                                 |                    d           |                     | j        dz               G fddt                    } |d           }|                     t          j	                  5 }|
                                 d d d            n# 1 swxY w Y   |                     t          |j                  d	           d d = d S )
Nc                  z    t          d           }                     |            |                                  d S )Nc                      d S r!   r   r   r   r   r   zYTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.worker.<locals>.<lambda>      D r   )r&   r   r   )r'   anothers    r   r   zGTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.worker  s5    LL))ANN1HHJJJJJr   rh   ro   r   )initial_main_greenletsc                       e Zd Z fdZdS )KTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.convolutedc                 T    |dk    rd         | _         t          j        | |          S Nr@   r   r   r&   r   r9   r   r  s     r   r   z\TestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.convoluted.__getattribute__  *    5==")!*DK"3D$???r   Nr   r  s   r   r  r    5        @ @ @ @ @ @ @r   r  c                      d S r!   r   r   r   r   r   zITestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.<lambda>  r   r   r  )rj   rk   rp   rq   r  main_greenlets_before_testr&   rA   r   r   r   r6   rD   rE   )r9   r   r   r  r'   r"   r  s         @r   test_unexpected_reparentingz7TestGreenletSetParentErrors.test_unexpected_reparenting  ss   	 	 	 	 	 F+++					r


& 	&&d>]`a>a&bbb	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@
 J||$$x~.. 	#HHJJJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	S]++]	_ 	_ 	_AAAJJJs   %CC
C
c                 r   g t          j                    t          j                    fd} G fddt                    }t          j        |          }|                                                     d           	  |d           }|                     t          j                  5 }|	                                 d d d            n# 1 swxY w Y   | 
                    dt          |j                             | 
                    dt          |j                             | 
                    d	t          |j                                                              |                    d           d d = d S #                                  |                    d           d d = w xY w)
Nc                      t          d           }                     |            |                                                                                       d           d S )Nc                      d S r!   r   r   r   r   r   zhTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.worker.<locals>.<lambda>  r  r   ro   )r&   r   r   r   r   )r'   r  keep_main_aliveswitched_to_greenlets    r   r   zVTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.worker  s]    LL))ANN1HHJJJ $$&&&  $$$$$r   c                       e Zd Z fdZdS )ZTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.convolutedc                 T    |dk    rd         | _         t          j        | |          S r  r  r  s     r   r   zkTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.convoluted.__getattribute__  r  r   Nr   r  s   r   r  r    r  r   r  rh   ro   c                      d S r!   r   r   r   r   r   zXTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.<lambda>      4 r   z#Cannot switch to a different threadExpectedCurrent)rj   r   r&   rk   rp   r   rA   r   r   r   rC   rD   rE   r   rq   )	r9   r   r  r   r'   r"   r  r  r  s	         @@@r   *test_unexpected_reparenting_thread_runningzFTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running  s    (00#/++	% 	% 	% 	% 	% 	% 	%	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ F+++				!!"%%%	
<<((A""8>22 c


              MM?S]ASASTTTMM*c#-&8&8999MM)S%7%7888!!!FF2JJJ


 !!!FF2JJJ
NNNNs1   +F 2CF CF CA;F 0F6c                 6   t          d           }|                     |j        t          j                               |                     t                    5 }|`d d d            n# 1 swxY w Y   |                     t          |j	                  d           d S )Nc                      d S r!   r   r   r   r   r   zGTestGreenletSetParentErrors.test_cannot_delete_parent.<locals>.<lambda>      T r   can't delete attribute)
r&   rM   r   r   r   rA   rB   r6   rD   rE   )r9   r   r"   s      r   test_cannot_delete_parentz5TestGreenletSetParentErrors.test_cannot_delete_parent  s    \\**fmX%8%:%:;;;~.. 	#	 	 	 	 	 	 	 	 	 	 	 	 	 	 	S]++-EFFFFFs   A&&A*-A*c                     |                      t                    5 }t          j                    `d d d            n# 1 swxY w Y   |                     t          |j                  d           d S )Nr  rA   rB   r   r   r   r6   rD   rE   r9   r"   s     r   !test_cannot_delete_parent_of_mainz=TestGreenletSetParentErrors.test_cannot_delete_parent_of_main  s    ~.. 	-##%%,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-S]++-EFFFFFs   ;??c                 \    |                      t          j                    j                   d S r!   )rQ   r   r   r   r<   s    r   !test_main_greenlet_parent_is_nonez=TestGreenletSetParentErrors.test_main_greenlet_parent_is_none  s(    (-//677777r   c                     d } fd}t          |          }                     |            ||           |                                                      |            ||           |                                 d S )Nc                  Z    t          j                    j                                         d S r!   r   r   r   r   bgzCTestGreenletSetParentErrors.test_set_parent_wrong_types.<locals>.bg  s&    !!(//11111r   c                    d ddfD ]x}                     t                    5 }|| _        d d d            n# 1 swxY w Y                       t	          |j                  dt          |          j        z              yd S )Nr   42z4GreenletChecker: Expected any type of greenlet, not )rA   r   r   r6   rD   rE   typer   )gletpr"   r9   s      r   checkzFTestGreenletSetParentErrors.test_set_parent_wrong_types.<locals>.check#  s    1dD( _ _&&y11 $S"#DK$ $ $ $ $ $ $ $ $ $ $ $ $ $ $   &&JTRSWWM]]_ _ _ _	_ _s   8<	<	)r&   r   r   r   )r9   r  r  r'   s   `   r   test_set_parent_wrong_typesz7TestGreenletSetParentErrors.test_set_parent_wrong_types  s    	2 	2 	2	_ 	_ 	_ 	_ 	_ OOa 	



a 	





r   c                     t          d           }|                     t                    5 }||_        d d d            n# 1 swxY w Y   |                     t          |j                  d           d S )Nc                      d S r!   r   r   r   r   r   z@TestGreenletSetParentErrors.test_trivial_cycle.<locals>.<lambda>;  r  r   cyclic parent chain)r&   rA   r   r   r6   rD   rE   )r9   r  r"   s      r   test_trivial_cyclez.TestGreenletSetParentErrors.test_trivial_cycle:  s    <<((z** 	cDK	 	 	 	 	 	 	 	 	 	 	 	 	 	 	S]++-BCCCCCs   ?AAc                    |                      t                    5 }t          j                    t          j                    _        d d d            n# 1 swxY w Y   |                     t          |j                  d           d S )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 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	AS]++-WXXXXXs   *AAAc                 D   t          d           }t          d           }t          d           }||_        ||_        |                     t                    5 }||_        d d d            n# 1 swxY w Y   |                     t          |j                  d           d S )Nc                      d S r!   r   r   r   r   r   z?TestGreenletSetParentErrors.test_deeper_cycle.<locals>.<lambda>G  r   r   c                      d S r!   r   r   r   r   r   z?TestGreenletSetParentErrors.test_deeper_cycle.<locals>.<lambda>H  r   r   c                      d S r!   r   r   r   r   r   z?TestGreenletSetParentErrors.test_deeper_cycle.<locals>.<lambda>I  r   r   r  )r&   r   rA   r   r6   rD   rE   )r9   r(   rw   g3r"   s        r   test_deeper_cyclez-TestGreenletSetParentErrors.test_deeper_cycleF  s    &&&&&&		z** 	cBI	 	 	 	 	 	 	 	 	 	 	 	 	 	 	S]++-BCCCCCs   A--A14A1N)r   r   r   r  r  r  r  r  r  r  r  r  r  r   r   r   r  r    s        W W W2' ' 'R" " "HG G GG G G8 8 8  8D D DY Y Y	D 	D 	D 	D 	Dr   r  c                   8    e Zd Zd Zd Zd Zd Zd Zd Zd Z	dS )	TestReprc                 \    |                      |                    |          ||f           d S r!   )r   endswith)r9   gotsuffixs      r   assertEndsWithzTestRepr.assertEndsWithT  s,    V,,sFm<<<<<r   c                 r    t          t          j                              }|                     |d           d S )N current active started main>)reprr   r   r  r9   rs     r   test_main_while_runningz TestRepr.test_main_while_runningW  s5    $&&''A>?????r   c                     t          j                    fd}t          |          }|                                }|                     |d           d S )Nc                  "    t                     S r!   )r  r  s   r   r@   z-TestRepr.test_main_in_background.<locals>.run]  s    ::r    suspended active started main>)r   r   r&   r   r  )r9   r@   r'   r  r   s       @r   test_main_in_backgroundz TestRepr.test_main_in_background[  sc    "$$	 	 	 	 	 HHJJA@AAAAAr   c                 h    t          t                                }|                     |d           d S )Nz	 pending>)r  r&   r  r  s     r   test_initialzTestRepr.test_initiald  s/    A{+++++r   c                 6   t          j                     G fddt          j                  } |            }|                                 |                    d           |                     |j        d           |                     |j        d           t          d          D ]}t          j        d           t          d          D ]K}|                     |j        j                   t          |j                  }|                     |d           Ld S )	Nc                   &    e Zd ZdxZZdZ fdZdS )/TestRepr.test_main_from_other_thread.<locals>.TNc                     t                    | _        t          j                    | _        t          | j                  | _        d S r!   )r  original_mainr   r   	main_gletr  )r9   r   s    r   r@   z3TestRepr.test_main_from_other_thread.<locals>.T.runn  s9    %)$ZZ"!)!4!6!6#'#7#7   r   )r   r   r   r  r  r   r@   r  s   r   Tr  k  s=        *..MKI8 8 8 8 8 8 8r   r!  ro   r  r  r.   gMbP?z (thread exited) dead>)r   r   rj   rk   rp   rq   r  r  r  r8   r   r   r   r   rX   r  )r9   r!  r   rf  r  r   s        @r   test_main_from_other_threadz$TestRepr.test_main_from_other_threadh  s.   "$$	8 	8 	8 	8 	8 	8 	8	  	8 	8 	8 ACC					r


AO-NOOOAM+JKKK q 	 	AJu q 	= 	=AOOAK,---Q[!!A#;<<<<	= 	=r   c                 f   t          d           }|                                 |                     t          |          d           |                     dt          |                     |                     dt          |                     |                     dt          |                     d S )Nc                      d S r!   r   r   r   r   r   z$TestRepr.test_dead.<locals>.<lambda>  s     r   z dead>	suspendedstartedactive)r&   r   r  r  assertNotInrN   s     r   	test_deadzTestRepr.test_dead  s    %%	


DGGX...d1gg...DGG,,,477+++++r   c                     t          d           }t          d           }t          j                    }|||fD ]:}|                     |t                     |                     |t                     ;d S )Nc                      d S r!   r   r   r   r   r   z>TestRepr.test_formatting_produces_native_str.<locals>.<lambda>  r  r   c                      d S r!   r   r   r   r   r   z>TestRepr.test_formatting_produces_native_str.<locals>.<lambda>  r   r   )r&   r   r   assertIsInstancerD   )r9   g_deadg_not_startedg_curr'   s        r   #test_formatting_produces_native_strz,TestRepr.test_formatting_produces_native_str  s     \\**#LL11#%%- 		 		A!!   !!   		 		r   N)
r   r   r   r  r  r  r  r"  r)  r1  r   r   r   r	  r	  R  s        = = =@ @ @B B B, , ,!= != !=F, , ,    r   r	  c                        e Zd Zd Zd Zd ZdS )TestMainGreenletc                     dt          t          j                              v sJ t          t          j                              }dt          |          vsJ |S )Nr   )r  r   r   r  )r9   r   s     r   _check_current_is_mainz'TestMainGreenlet._check_current_is_main  sV    h133444444$&&''T!WW$$$$r   c                 |    |                                  }t          d|fi           }|                     |           d S )Nsubclass)r5  r  rL   )r9   	main_typer7  s      r   )test_main_greenlet_type_can_be_subclassedz:TestMainGreenlet.test_main_greenlet_type_can_be_subclassed  s@    //11	
YL"55X&&&&&r   c                     |                                   |                     t          j                    t                     d S r!   )r5  r-  r   r   r&   r<   s    r   test_main_greenlet_is_greenletz/TestMainGreenlet.test_main_greenlet_is_greenlet  s9    ##%%%h133[AAAAAr   N)r   r   r   r5  r9  r;  r   r   r   r3  r3    sF          ' ' '
B B B B Br   r3  c                   >    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	S )
TestBrokenGreenletsc                     d }t           j                            |          }d|_        |                     t
          d          5  |                                 d d d            d S # 1 swxY w Y   d S )Nc                       t          d          )NzNever get here)AssertionErrorr   r   r   r   z<TestBrokenGreenlets.test_failed_to_initialstub.<locals>.func  s     !1222r   Tz;Failed to switch stacks into a greenlet for the first time.)r   	_greenletUnswitchableGreenletforce_switch_errorassertRaisesRegexSystemErrorr   )r9   r   r'   s      r   test_failed_to_initialstubz.TestBrokenGreenlets.test_failed_to_initialstub  s    	3 	3 	3 33D99###K$ac c 	 	HHJJJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A''A+.A+c                    g fd}t           j                            |          }|                                 |                     dg           |                                 |                     ddg           d|_        |                     t          d          5  |                                 d d d            n# 1 swxY w Y   d|_        |                                 |                     g d           d S )Nc                  .                         d           t          j                    j                                                               d           t          j                    j                                                               d           d S )Nr   r3   r.   r/   )runss   r   r   zDTestBrokenGreenlets.test_failed_to_switch_into_running.<locals>.func  sp    KKNNN!!(//111KKNNN!!(//111KKNNNNNr   r   r3   Tz0Failed to switch stacks into a running greenlet.F)r   r3   r.   )r   rA  rB  r   r6   rC  rD  rE  )r9   r   r'   rI  s      @r   "test_failed_to_switch_into_runningz6TestBrokenGreenlets.test_failed_to_switch_into_running  sC   	 	 	 	 	 33D99	


s###	


1v&&&###K$VX X 	 	HHJJJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  %	


yyy)))))s   !CC	Cc                     |                      d          }|                     d|j                   |                     |j        |                                            d S )Nzfail_slp_switch.pyzfail_slp_switch is running)assertScriptRaisesrC   output
returncode,get_expected_returncodes_for_aborted_processr9   exs     r   &test_failed_to_slp_switch_into_runningz:TestBrokenGreenlets.test_failed_to_slp_switch_into_running  sU    $$%9::2BI>>>bmT%V%V%X%XYYYYYr   c                 8   |                      d          }|                     d|           |                     d|           |                     d|           |                     d|           |                     d|           |                     d|           d S )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_scriptrC   r9   rM  s     r   #test_reentrant_switch_two_greenletsz7TestBrokenGreenlets.test_reentrant_switch_two_greenlets  s    !?@@k6***gv&&&mV,,,/88816:::-v66666r   c                     |                      dd          }|                     d|j                   |                     d|j                   d S )Nzfail_switch_three_greenlets.py)r   )	exitcodesr   zpositional arguments)rL  rC   rM  rP  s     r   %test_reentrant_switch_three_greenletsz9TestBrokenGreenlets.test_reentrant_switch_three_greenlets  sQ     $$%EQU$VVk29---,bi88888r   c                 \    |                      d          }|                     d|           d S )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')]rU  rW  s     r   &test_reentrant_switch_three_greenlets2z:TestBrokenGreenlets.test_reentrant_switch_three_greenlets2  s@     !BCC= 		
 		
 		
 		
 		
r   c                 \    |                      d          }|                     d|           d S )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)]rU  rW  s     r   4test_reentrant_switch_GreenletAlreadyStartedInPythonzHTestBrokenGreenlets.test_reentrant_switch_GreenletAlreadyStartedInPython  s>    !FGG1
 	
 	
 	
 	
 	
r   c                 \    |                      d          }|                     d|           d S )Nzfail_clearing_run_switches.pyzRESULTS [('G.__getattribute__', 'run'), ('RunCallable', '__del__'), ('main: g.switch()', 'from RunCallable'), ('run_func', 'enter')]rU  rW  s     r   *test_reentrant_switch_run_callable_has_delz>TestBrokenGreenlets.test_reentrant_switch_run_callable_has_del!  s>    !@AA 	
 	
 	
 	
 	
r   N)r   r   r   rF  rJ  rR  rX  r[  r]  r_  ra  r   r   r   r=  r=    s        
 
 
* * *8Z Z Z7 7 79 9 9
 
 
$

 

 


 
 
 
 
r   r=  __main__)r~   r   r   rj   r  abcr   r   r   r&   r  r   r   r	   	leakcheckr
   r   r   r   r)   r+   r  r	  r3  r=  r   r   r   r   r   <module>re     s0   				 



                    , , , , , ,       " " " " " "       & & & & & &	 	 	 	 		 	 	 	    p( p( p( p( p(8 p( p( p(fjD jD jD jD jD( jD jD jDZR R R R Rx R R RjB B B B Bx B B B.k
 k
 k
 k
 k
( k
 k
 k
Z zHMOOOOO r   