
    R3j                        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 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jB                          yy)!    N)ABCMeta)abstractmethod)greenlet   )TestCase)RUNNING_ON_MANYLINUX)PY313)PY314)WIN)RUNNING_ON_FREETHREAD_BUILD)fails_leakcheck) fails_leakcheck_on_py314_or_less)ignores_leakcheck)ignores_leakcheck_ifc                       e Zd Zy)	SomeErrorN__name__
__module____qualname__     I/DATA/.local/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>.crasher2   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#d4d       Z$e# e%e&xr e'jP                  dd d k(  d!      d"               Z)d# Z*e+d$        Z,d% Z-d& Z.d' Z/d( Z0d) Z1d* Z2 ej&                  exr ed+      d,        Z3d- Z4d. Z5d/ Z6d0 Z7d1 Z8d2 Z9d3 Z:y)5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)selfr8   r-   r7   s      @r   _do_simple_testzTestGreenlet._do_simple_test:   sc    	 N

1	


1	


1d58n-r   c                 $    | j                          y r'   )r@   r?   s    r   test_simplezTestGreenlet.test_simpleI   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_AttributeErrorL   sI    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rE   )r,   rG   rH   throwr   rI   rJ   rK   rL   s      r   'test_throw_no_run_raises_AttributeErrorz4TestGreenlet.test_throw_no_run_raises_AttributeErrorS   sL    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_NoneZ   s5    t$Qahh 3 3 56r   c                 t    t        d       }| j                  |       | j                  |j                         y )N)rF   )r,   rR   assertIsNonerF   rT   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   extendr6   s   r   r8   z)TestGreenlet.test_two_children.<locals>.fg   s9    JJqM!((//1JJ1vr   r   r9   r:   T   )r,   r   r<   lendead)r?   r8   r-   hr7   s       @r   test_two_childrenzTestGreenlet.test_two_childrend   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br5   r6   s   r   r8   z3TestGreenlet.test_two_recursive_children.<locals>.f{   s)    JJsO!((//1r   c                      j                  d       t              } | j                          j                  d       y )Nac)r   r,   r   )r-   r8   r7   s    r   r-   z3TestGreenlet.test_two_recursive_children.<locals>.g   s+    JJsOAAHHJJJsOr   r9   r   )re   rc   rf   )r<   r    getrefcountr
   r,   r   )r?   r-   r8   r7   s     @@r   test_two_recursive_childrenz(TestGreenlet.test_two_recursive_childrenx   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   r8   z$TestGreenlet.test_threads.<locals>.f   s      "NN4 r   
   target)r>   	threadingThreadstartjoinr<   r]   )r?   r8   ithsthrl   s   `    @r   test_threadszTestGreenlet.test_threads   sw    	! 499=9ayq)9=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_untilr|   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'   )r}   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   sf    $$& '')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ro   argsg-C6?g      ?)
rp   Eventr>   rq   rr   r]   timesleepsetrs   )
thread_countr~   greenlets_all_createdall_threads_dead_creators	switcherstr   r   s
           r   run_itz8TestGreenlet.test_switching_many_threads.<locals>.run_it   sC   I$-OO$5!(0 &	 '    (8	@U@W X '	   &
 '    (B'02G'7'9: '   *	 + 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   s1    	%
	 	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   r<   rG   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   rG   KeyErrorr/   r<   )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   r<   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   rF   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    unraisablehookr<   r]   assertIsInstance	exc_valuer   )r?   rF   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 )Nr4   )r,   r$   r   r   r   r   r   r}   )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   r8   z1TestGreenlet.test_dealloc_other_thread.<locals>.f4  sn    U#BIIdONN2JJL;??A  #M""$ %%a(Mr   rn   rm   r   r4   )rp   r   rq   rr   r}   r<   r]   r   r   r   r   r   r   rs   )	r?   r8   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    	_getframer<   f_backr   r   r   r   )r8   r?   s    r   f1z#TestGreenlet.test_frame.<locals>.f1c  sE    a AQXXt,!((//2$r   r   )r,   r   
assertTruegr_frameassertFalser<   )r?   r   r-   framefrom_gs   `    r   
test_framezTestGreenlet.test_frameb  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>t  s    DJJqMr   r+   )r   r-   s   ` r   runnerz,TestGreenlet.test_thread_bug.<locals>.runners  s    12AHHJr   )g?r   )g333333?rm   )rp   rq   rr   rs   )r?   r   t1t2s       r   test_thread_bugzTestGreenlet.test_thread_bugr  sU    	 V&9V&9





r   c                 l      fd}t        |      j                  dd      } j                  |d       y )Nc                 N    j                  | d       j                  |d       y)Nr:   r9   *   )r<   )re   rc   r?   s     r   rF   z,TestGreenlet.test_switch_kwargs.<locals>.run~  s&    Q"Q"r   r:   r9   )re   rc   r   r,   r   r<   )r?   rF   r   s   `  r   test_switch_kwargszTestGreenlet.test_switch_kwargs}  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   r9   r4   r{   r   s    r   rF   z6TestGreenlet.test_switch_kwargs_to_parent.<locals>.run  sR    !((//!/4!((//Q/7a1f9r   r   r4   )r9   )r4   	   )r,   r<   r   )r?   rF   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>      Dr   r-   rm   r,   r   r}   created_eventdata
done_events   r   rF   z7TestGreenlet.test_switch_to_another_thread.<locals>.run  )    #L1DIOOBr   rn   rm   r-   )rp   r   rq   rr   r}   rG   r   r   r   r   rs   clear)r?   rF   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   r<   )r!   r_   r?   s    r   r8   z&TestGreenlet.test_exc_state.<locals>.f  sK    ; '';<<>A%%'  3<<>:s
    AA#c                  N     j                  t        j                         d       y )N)NNN)r<   r    r!   rB   s   r   r_   z&TestGreenlet.test_exc_state.<locals>.h  s    S\\^-?@r   r+   )r?   r8   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   r8   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,   r<   r   r   r   rG   	TypeError)r?   r8   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 rE   )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   )copyrG   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>  s    $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   rS   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,   rG   rH   r   )r?   r.   r   s      r   test_parent_return_failurez'TestGreenlet.test_parent_return_failure  s8    ]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>      tr   )r,   rG   r   rO   )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'   r{   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   rn   rm   r   Bcannot switch to a different thread (which happens to have exited)zCannot switchr   T)rp   rq   rr   rs   wait_for_pending_cleanupsrG   r   r   rO   r   rJ   rK   r   r<   r   r   RuntimeErrorr   f_localsrW   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	""((* 1  vay))22G<q	""++113fQi0011I %)!C /." 10s   9G G G
GNr9   )r4   rm   z;does not leaks on Windows 3.10, but does on other platformsc                 $    | j                          y r'   r'  rB   s    r   &test_throw_to_dead_thread_doesnt_crashz3TestGreenlet.test_throw_to_dead_thread_doesnt_crash:  s     	779r   c                 &    | j                  d       y rk   r)  rB   s    r   +test_throw_to_dead_thread_doesnt_crash_waitz8TestGreenlet.test_throw_to_dead_thread_doesnt_crash_waitB  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__countrB   s    r   r1  z@TestGreenlet.test_recursive_startup.<locals>.convoluted.__init__H  s    $$T*
r   c                     |dk(  r'| j                   dk(  rd| _         | j                  d       t        j                  | |      S )NrF   r   r   +   )r2  r   r,   r  r  s     r   r  zHTestGreenlet.test_recursive_startup.<locals>.convoluted.__getattribute__K  s8    5=TZZ1_!"DJKKO"33D$??r   c                 <    	 | j                   j                  |       r'   r   r   )r?   r   s     r   rF   z;TestGreenlet.test_recursive_startup.<locals>.convoluted.runP  s    KK&&u- r   N)r   r   r   r1  r  rF   r   r   r   
convolutedr/  G  s    @
.r   r7  r   r4  T)r,   r<   r   r%  )r?   r7  r-   s      r   test_recursive_startupz#TestGreenlet.test_recursive_startupE  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__c  s     r   Nr   r   r   rB  r@  rA  s   r   	finalizedr;  b  s     r   rE  c                  \    t        j                         j                  j                          y r'   r{   r   r   r   deallocatorz=TestGreenlet.test_threaded_updatecurrent.<locals>.deallocatorj  r  r   c                      j                          t        j                           d= j                          j                          t        j                          j                          y r   r?  r   r   r>  )r-   r@  rA  s   r   fthreadz9TestGreenlet.test_threaded_updatecurrent.<locals>.fthreadl  sF    MMO!!MMOMMO!MMOr   r   rn   rm   )rp   Lockr?  objectr   r   r,   bombr   rq   rr   r>  r<   rs   )	r?   rE  rG  rJ  r  r   r-   r@  rA  s	         @@@r   test_threaded_updatecurrentz(TestGreenlet.test_threaded_updatecurrent[  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   rQ  zATestGreenlet.test_dealloc_switch_args_not_lost.<locals>.initiator      r   r   r   )r,   r   r   r<   )r?   r   rQ  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   re   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__rB   s    r   r]  z9TestGreenlet.test_tuple_subclass.<locals>.mytuple.__len__  s&    ##%,,.}}T**r   N)r   r   r   r]  r   r   r   mytuplerZ    s    +r   r^  r   )re   c                  f      t        j                         j                  j                         y r'   r{   )rX  r   kwargss   r   switchapplyz5TestGreenlet.test_tuple_subclass.<locals>.switchapply  s$    8&&(//66fEr   )r\  dictr,   r<   r   )r?   r^  ra  r-   rX  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AbstractSubclassrF   c                      y r'   r   rB   s    r   r   z7TestGreenlet.test_abstract_subclasses.<locals>.<lambda>  s    r   c                       e Zd Zy):TestGreenlet.test_abstract_subclasses.<locals>.BadSubclassNr   r   r   r   BadSubclassrh        r   ri  c                       e Zd Zd Zy);TestGreenlet.test_abstract_subclasses.<locals>.GoodSubclassc                      y r'   r   rB   s    r   rF   z?TestGreenlet.test_abstract_subclasses.<locals>.GoodSubclass.run  s    r   N)r   r   r   rF   r   r   r   GoodSubclassrl    s    r   rn  )r   r,   r   rG   r   )r?   re  ri  rn  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'   rI  rD  s   r   another_threadzWTestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.another_thread  s!    ##%r   rn   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   r=  )r?   r@  rA  recycleds    r   rB  z\TestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.gc_callback.__del__  s    MMOMMO"&HQKr   NrC  )r@  rA  rx  s   r   gc_callbackrv    s    'r   ry  c                       e Zd Z fdZy)PTestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.garbagec                 ,    | | _                 | _        y r'   )cyclecallback)r?   ry  s    r   r1  zYTestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.garbage.__init__  s    !%DJ$/MDMr   N)r   r   r   r1  )ry  s   r   garbager{    s    2r   r  r9   r   rm   T)rp   rK  r?  rq   rr   rL  r>   r   r   rs   r,   r   r   r   r<   r   )rt  r   r  lr   currentr-   r   lastry  r@  rA  rx  Nr?   s            @@@@r   attemptz?TestGreenlet.test_implicit_parent_with_threads.<locals>.attempt  s/   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  73 r   r;   )r   	isenabledget_thresholdr>   )r?   r  r   r  s   `  @r   !test_implicit_parent_with_threadsz.TestGreenlet.test_implicit_parent_with_threads  sJ    ||~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    rj  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rm   )r   r   r   r,   r    rg   r  r>   r   r   assertGreaterEqualr<   )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   )rq   )r   r  c                       e Zd Zy)STestGreenlet.test_issue_245_reference_counting_subclass_threads.<locals>.MyGreenletNr   r   r   r   
MyGreenletr  ?  rj  r   r  c                  D             j                   j                          y r'   r6  r  s   r   r  zVTestGreenlet.test_issue_245_reference_counting_subclass_threads.<locals>.greenlet_mainE  s    L&&(r   c                            }j                  |       |j                          ~| j                          j                  d                y )Nrm   )r   r   r   r}   )greenlet_running_eventminer  r   r  r  ref_cleareds     r   thread_mainzTTestGreenlet.test_issue_245_reference_counting_subclass_threads.<locals>.thread_mainH  sH    m,DLLKKM"&&( R  Lr   -   rq  r   rm   )rp   rq   r   r   r   r,   r    rg   r   r>   r   rr   r}   r   r"  r<   )r?   rq   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_threads1  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_args  rS  r   c                       yr   r   r   r   r   leafzYTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_raises_error.<locals>.leaf  rS  r   )r,   rG   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_error~  sM    	  +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  rS  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 rE   )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Or   r   )r,   rG   r   r   r<   )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   r<   r   rW   )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   rR   r   r<   f_codeco_namer   rW   )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   r;   r  rm   i  )	functoolsr   r  r,   r   r   r>   r<   r$  r   rW   )r?   grr   rt   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   r<   r  r  rW   r   )r?   r  r  r   r  r  r  s        @@r   test_frames_always_exposedz'TestGreenlet.test_frames_always_exposed  sx     ""$	$	 		  -	--w7%,,'r   )F);r   r   r   r@   rC   rM   rP   rU   rX   r`   rh   rw   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   r    version_infor*  r,  r   r8  rN  rT  rc  ro  r  r  r  r  r  r  r  r  r  r  r   r   r   r1   r1   8   s   .117
!
'(D,1 = =~,8+OI0 X__&&G55n+ 	 -& 5"	+C
6	; &7) &7)r &/  !$/E:	 &
:> ) )*-^$4-82 5n(FT X__&&EDDDDL+%*)6:.&(r   r1   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-   rm   r   r   s   r   rF   z?TestGreenletSetParentErrors.test_threaded_reparent.<locals>.run  r   r   c                  \    t        j                         j                  j                          y r'   r{   r   r   r   blankzATestGreenletSetParentErrors.test_threaded_reparent.<locals>.blank  r  r   rn   rm   r-   z&parent cannot be on a different thread)rp   r   rq   rr   r}   r,   r   rG   r   r   r   rs   r<   rJ   rK   )	r?   rF   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>$      Dr   )r,   r   r   )r-   anothers    r   r  zGTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.worker#  s"    L)ANN1HHJr   rn   rm   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 NrF   r   r   r,   r  r?   r  r  s     r   r  z\TestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.convoluted.__getattribute__?  (    5=")!*DK"33D$??r   Nr  r  s   r   r7  r  >  	    @r   r7  c                       y r'   r   r   r   r   r   zITestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.<lambda>C  r  r   r!  )rp   rq   rr   rs   r"  main_greenlets_before_testr,   rG   r   r   r   r<   rJ   rK   )r?   r  r   r7  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>Q  r  r   rm   )r,   r   r   r   r}   )r-   r  keep_main_aliveswitched_to_greenlets    r   r  zVTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.workerP  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__W  r  r   Nr  r  s   r   r7  r  V  r  r   r7  rn   rm   c                       y r'   r   r   r   r   r   zXTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.<lambda>a      4r   z#Cannot switch to a different threadExpectedCurrent)rp   r   r,   rq   rr   r}   rG   r   r   r   rI   rJ   rK   r   rs   )	r?   r  r7  r   r-   r(   r  r  r  s	         @@@r   *test_unexpected_reparenting_thread_runningzFTestGreenletSetParentErrors.test_unexpected_reparenting_thread_runningJ  s    (0#//+	%	@ 	@ F+		!!"%	<(A""8>>2c
 3MM?S]]ASTMM*c#--&89MM)S%78!FF2J
 32 !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>o      Tr   can't delete attribute)
r,   rS   r   r   r   rG   rH   r<   rJ   rK   )r?   r  r(   s      r   test_cannot_delete_parentz5TestGreenletSetParentErrors.test_cannot_delete_parentn  sd    \*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  rG   rH   r   r   r   r<   rJ   rK   r?   r(   s     r   !test_cannot_delete_parent_of_mainz=TestGreenletSetParentErrors.test_cannot_delete_parent_of_mainv  sI    ~.###%, /S]]+-EF /.s   AA"c                 ^    | j                  t        j                         j                         y r'   )rW   r   r   r   rB   s    r   !test_main_greenlet_parent_is_nonez=TestGreenletSetParentErrors.test_main_greenlet_parent_is_none|  s    (--/667r   c                      d } fd}t        |      } j                  |        ||       |j                           j                  |        ||       |j                          y )Nc                  \    t        j                         j                  j                          y r'   r{   r   r   r   bgzCTestGreenletSetParentErrors.test_set_parent_wrong_types.<locals>.bg  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 )rG   r   r   r<   rJ   rK   typer   )gletpr(   r?   s      r   checkzFTestGreenletSetParentErrors.test_set_parent_wrong_types.<locals>.check  si    1dD(&&y1S"#DK 2   &JTRSWM]M]]_	 )11s   A..A7	)r,   r   r   r   )r?   r  r  r-   s   `   r   test_set_parent_wrong_typesz7TestGreenletSetParentErrors.test_set_parent_wrong_types  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,   rG   r   r   r<   rJ   rK   )r?   r  r(   s      r   test_trivial_cyclez.TestGreenletSetParentErrors.test_trivial_cycle  sK    <(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  sT    ~.#+3+>+>+@H!( /S]]+-WX /.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   rG   r   r<   rJ   rK   )r?   r.   r   g3r(   s        r   test_deeper_cyclez-TestGreenletSetParentErrors.test_deeper_cycle  sm    &&&		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   rF   z-TestRepr.test_main_in_background.<locals>.run  s    :r    suspended active started main>)r   r   r,   r   r'  )r?   rF   r-   r0  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   rF   z3TestRepr.test_main_from_other_thread.<locals>.T.run  s0    %)$Z"!)!4!4!6#'#7 r   )r   r   r   r<  r  r=  rF   r  s   r   Tr:    s    *..MKI8r   r>  rm   r4  r-  r4   gMbP?z (thread exited) dead>)r   r   rp   rq   rr   rs   r'  r<  r  r>   r   r   r   r=  r^   r.  )r?   r>  r   r   r0  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>  s    r   z dead>	suspendedstartedactive)r,   r   r'  r.  assertNotInrT   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   rJ   )r?   g_deadg_not_startedg_curr-   s        r   #test_formatting_produces_native_strz,TestRepr.test_formatting_produces_native_str  sa     \*#L1##%-A!! !! .r   r'   )r   r   r   r   r   r'  r1  r5  r7  r?  rF  rM  r   r   r   r&  r&    s4    8-.	B@B,!=F,r   r&  c                   $    e Zd 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)rQ  r  rR   )r?   	main_typerS  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'   )rQ  r   r   r   r,   rB   s    r   test_main_greenlet_is_greenletz/TestMainGreenlet.test_main_greenlet_is_greenlet  s'    ##%h113[Ar   c                       fd}t        j                   |      j                           j                  t              5  t	        d       d d d        y # 1 sw Y   y xY w)Nc                  r    j                  t              5  t         d       d d d        y # 1 sw Y   y xY wrE   )rG   rH   getattr)r-   r?   s   r   do_itz1TestMainGreenlet.test_greenlet_run.<locals>.do_it   s'    "">25! 322s   -6rF   )r   r   rG   rH   rZ  )r?   r[  r-   s   ` @r   test_greenlet_runz"TestMainGreenlet.test_greenlet_run  sG    	" e$	
~.Au /..s   AA"N)r   r   r   rQ  rU  rW  r\  r   r   r   rO  rO  
  s    '
Br   rO  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.  sW    	3 33D9###K$acHHJc c c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   r9   r4   r5   )runss   r   r   zDTestBrokenGreenlets.test_failed_to_switch_into_running.<locals>.func<  sZ    KKN!((//1KKN!((//1KKNr   r   r9   Tz0Failed to switch stacks into a running greenlet.F)r   r9   r4   )r   rb  rc  r   r<   rd  re  rf  )r?   r   r-   rj  s      @r   "test_failed_to_switch_into_runningz6TestBrokenGreenlets.test_failed_to_switch_into_running:  s    	 33D9	
s#	
1v&###K$VXHHJX  %	
y)X Xs   CCc                     | j                  d      }| j                  d|j                         | j                  |j                  | j	                                y )Nzfail_slp_switch.pyzfail_slp_switch is running)assertScriptRaisesrI   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_runningV  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_scriptrI   r?   rn  s     r   #test_reentrant_switch_two_greenletsz7TestBrokenGreenlets.test_reentrant_switch_two_greenlets\  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)rm  rI   rn  rq  s     r   %test_reentrant_switch_three_greenletsz9TestBrokenGreenlets.test_reentrant_switch_three_greenletsf  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')]rv  rx  s     r   &test_reentrant_switch_three_greenlets2z:TestBrokenGreenlets.test_reentrant_switch_three_greenlets2o  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)]rv  rx  s     r   4test_reentrant_switch_GreenletAlreadyStartedInPythonzHTestBrokenGreenlets.test_reentrant_switch_GreenletAlreadyStartedInPython  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')]rv  rx  s     r   *test_reentrant_switch_run_callable_has_delz>TestBrokenGreenlets.test_reentrant_switch_run_callable_has_del  s'    !@A 	
r   N)r   r   r   rg  rk  rs  ry  r|  r~  r  r  r   r   r   r^  r^  *  s+    
*8Z79
$


r   r^  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    r  r   rB   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    r  r   r   r   r  r    s0    X&78GI3I3r   r  __main__)"r   r    r   rp   r  abcr   r   r   r,   r  r   r   r	   r
   r   r   	leakcheckr   r   r   r   r  r   r$   r/   r1   r  r&  rO  r^  r  r   r  r   r   r   <module>r     s    	 
       ,  "    ) & 7 ( +		 	L(8 L(^jD( jDZSx Slx @k
( k
Z3 3 zHMMO r   