a
    dh{                     @   s"  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 G dd deZdd Zdd ZG dd deZG dd deZG dd deZG dd deZG dd deZedkre  dS )    N)ABCMeta)abstractmethod)greenlet   )TestCase)RUNNING_ON_MANYLINUX)PY313)PY314)RUNNING_ON_FREETHREAD_BUILD)fails_leakcheckc                   @   s   e Zd ZdS )	SomeErrorN__name__
__module____qualname__ r   r   X/var/www/html/requester/venv/lib/python3.9/site-packages/greenlet/tests/test_greenlet.pyr      s   r   c                 C   s<   zt  j  W n    | t d   Y n0 td S Nr   )r   
getcurrentparentswitchappendsysexc_infor   seenr   r   r   fmain"   s    r   c                 C   s"   dd }t || d}|| d S )Nc                 S   s   | d S Nr   )excr   r   r   crasher.   s    zsend_exception.<locals>.crasherr   RawGreenletr   )gr   r   g1r   r   r   send_exception+   s    r%   c                   @   s  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zeeozeddd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Z e!d]d7d8Z"e!d9d: Z#d;d< Z$e!d=d> Z%d?d@ Z&dAdB Z'dCdD Z(dEdF Z)dGdH Z*dIdJ Z+eeoRedKdLdM Z,dNdO Z-dPdQ Z.dRdS Z/dTdU Z0dVdW Z1dXdY Z2dZd[ Z3d\S )^TestGreenletc                    s^   g   fdd}t |} d |   d |   d |  ttd d S )Nc                      s&     d t j    d d S )Nr      r   r   r   r   r   r   lstr   r   f9   s    
z'TestGreenlet._do_simple_test.<locals>.fr            )r"   r   r   assertEquallistrange)selfr+   r#   r   r)   r   _do_simple_test6   s    


zTestGreenlet._do_simple_testc                 C   s   |    d S r   )r3   r2   r   r   r   test_simpleE   s    zTestGreenlet.test_simplec                 C   sN   t  }| t}|  W d    n1 s.0    Y  | dt|j d S Nrun)r"   assertRaisesAttributeErrorr   assertInstr	exceptionr2   r#   r   r   r   r   (test_switch_no_run_raises_AttributeErrorH   s    &z5TestGreenlet.test_switch_no_run_raises_AttributeErrorc                 C   sP   t  }| t}|t W d    n1 s00    Y  | dt|j d S r6   )r"   r8   r9   throwr   r:   r;   r<   r=   r   r   r   'test_throw_no_run_raises_AttributeErrorO   s    (z4TestGreenlet.test_throw_no_run_raises_AttributeErrorc                 C   s*   t d d}| | | |jt  d S )Nr    )r"   assertIsNotNoneassertIsr   r   r   r2   r#   r   r   r   test_parent_equals_NoneV   s    

z$TestGreenlet.test_parent_equals_Nonec                 C   s$   t d d}| | | |j d S )N)r7   )r"   rA   assertIsNoner7   rC   r   r   r   test_run_equals_None[   s    

z!TestGreenlet.test_run_equals_Nonec                    s   g   fdd}t |}t |}|  | t d |  | t d |  | t d | |jd |  | t d | |jd d S )Nc                      s*     d t j   ddg d S )Nr   )r   r   r   r   r   extendr   r)   r   r   r+   c   s    
z)TestGreenlet.test_two_children.<locals>.fr   r,   r-   T   )r"   r   r/   lendead)r2   r+   r#   hr   r)   r   test_two_children`   s    zTestGreenlet.test_two_childrenc                    s   g fdd  fdd}|  t|ts2dnd t|}|  t|tsTdnd |  |  g d |  t|tsdnd d S )Nc                      s     d t j  d S )Nbr(   r   r)   r   r   r+   w   s    
z3TestGreenlet.test_two_recursive_children.<locals>.fc                     s(    d t } |    d d S )Nac)r   r"   r   r#   r+   r*   r   r   r#   {   s    
z3TestGreenlet.test_two_recursive_children.<locals>.gr,   r   )rN   rM   rO   )r/   r   getrefcountr	   r"   r   rC   r   rQ   r   test_two_recursive_childrent   s    z(TestGreenlet.test_two_recursive_childrenc                    sf   g fdd  fddt dD }|D ]}|  q,|D ]}|d q>tt| d S )Nc                      s       d d S NT)r3   r   r   )r2   successr   r   r+      s    z$TestGreenlet.test_threads.<locals>.fc                    s   g | ]}t j d qS )target)	threadingThread).0ir+   r   r   
<listcomp>       z-TestGreenlet.test_threads.<locals>.<listcomp>
   )r1   startjoinr/   rI   )r2   Zthsthr   )r+   r2   rU   r   test_threads   s    
zTestGreenlet.test_threadsc                 C   s   g }t t}t t}|| || ||_| |g  | t|j | |tg | }| |d | |tg |d}| |d | |tg d S )Nr      )r"   r   r   r   r/   r8   r   )r2   r   r$   g2valuer   r   r   test_exception   s    


zTestGreenlet.test_exceptionc                 C   s8   g }t t}|| | tt|t | |tg d S r   )r"   r   r   r8   KeyErrorr%   r/   )r2   r   r$   r   r   r   test_send_exception   s
    
z TestGreenlet.test_send_exceptionc                 C   sp   g }t t}t t}|| || | |g  ~t  | |tjg ~t  | |tjtjg d S r   )r"   r   r   r/   gccollectr   GreenletExit)r2   r   r$   re   r   r   r   test_dealloc   s    

zTestGreenlet.test_deallocc                 C   s|   dd }t |}|  tj}ddlm} |  }t_z~W |t_n|t_0 | }| d| | d| | d| d S )Nc                   S   s2   zt  j  W n t jy,   td Y n0 d S r   )r   r   r   r   rl   r   r   r   r   r   r7      s    zHTestGreenlet.test_dealloc_catches_GreenletExit_throws_other.<locals>.runr   )StringIO	ExceptionZignoredr   )r"   r   r   stderriorn   getvaluer:   )r2   r7   r#   Z	oldstderrrn   rp   vr   r   r   .test_dealloc_catches_GreenletExit_throws_other   s    z;TestGreenlet.test_dealloc_catches_GreenletExit_throws_otherz=Sometimes flaky (getting one GreenletExit in the second list)c              	      s  g g t   t  t  t   fdd}t j|d}|   d | g  | td d d = tst	  | g  
  d z:| tjg W 
  |d d d = d d = n(
  |d d d = d d = 0 d S )Nc                     sZ   t t} |  |  ~ t     d t     d t   d S )Nr'   )r"   r   r   r   rj   rk   setwait)r$   Z/bg_glet_created_running_and_no_longer_ref_in_bgZbg_should_be_clearZfg_ref_releasedZok_to_exit_bg_threadr   Zsomerefr   r   r+      s    



z1TestGreenlet.test_dealloc_other_thread.<locals>.frV   r_   r   r'   )rX   EventrY   r`   rv   r/   rI   r
   rj   rk   ru   r   rl   ra   )r2   r+   tr   rw   r   test_dealloc_other_thread   s8    







z&TestGreenlet.test_dealloc_other_threadc                    sf    fdd}t |}| } ||ju   | | } |  |d  |jd  d S )Nc                     s,   t d}  | jd  t j|  dS )Nr   meaning of life)r   	_getframer/   f_backr   r   r   r   r\   r4   r   r   f1"  s    
z#TestGreenlet.test_frame.<locals>.f1r{   )r"   r   
assertTruegr_frameassertFalser/   )r2   r~   r#   frameZfrom_gr   r4   r   
test_frame!  s    

zTestGreenlet.test_framec                 C   sL   dd }t j|dd}t j|dd}|  |  |d |d d S )Nc                    s   t  fdd}|  d S )Nc                      s
   t  S r   )timesleepr   xr   r   <lambda>3  r^   z>TestGreenlet.test_thread_bug.<locals>.runner.<locals>.<lambda>r!   )r   r#   r   r   r   runner2  s    z,TestGreenlet.test_thread_bug.<locals>.runner)g?rW   args)g333333?r_   )rX   rY   r`   ra   )r2   r   t1t2r   r   r   test_thread_bug1  s    
zTestGreenlet.test_thread_bugc                    s.    fdd}t |jddd} |d d S )Nc                    s     | d   |d dS )Nr-   r,   *   )r/   rN   rM   r4   r   r   r7   =  s    z,TestGreenlet.test_switch_kwargs.<locals>.runr-   r,   r   r   r"   r   r/   )r2   r7   r   r   r4   r   test_switch_kwargs<  s    zTestGreenlet.test_switch_kwargsc                 C   sR   dd }t |}| ddi|d | dddif|  | d|  d S )Nc                 S   s2   t  jj| d t  jjddd | | d fS )Nr   r,   r'   r   r   r   r   r   r   r   r   r7   E  s    z6TestGreenlet.test_switch_kwargs_to_parent.<locals>.runr   r'   )r,   )r'   	   )r"   r/   r   )r2   r7   r#   r   r   r   test_switch_kwargs_to_parentD  s
    z)TestGreenlet.test_switch_kwargs_to_parentc                    s   i t   t   fdd}t j|d}|   d | tj d   W d    n1 sp0    Y  	  |
d   d S )Nc                      s&   t dd d<    d d S )Nc                   S   s   d S r   r   r   r   r   r   r   T  r^   zITestGreenlet.test_switch_to_another_thread.<locals>.run.<locals>.<lambda>r#   r_   r"   ru   rv   r   Zcreated_eventdata
done_eventr   r   r7   S  s    z7TestGreenlet.test_switch_to_another_thread.<locals>.runrV   r_   r#   )rX   rx   rY   r`   rv   r8   r   errorr   ru   ra   clear)r2   r7   threadr   r   r   test_switch_to_another_threadN  s    
*
z*TestGreenlet.test_switch_to_another_threadc                    s*    fdd}fdd t |  d S )Nc                     sB   zt dW n0   t } t   | t  Y n0 d S )NZfun)
ValueErrorr   r   r"   r   r/   )r   rK   r2   r   r   r+   b  s    z&TestGreenlet.test_exc_state.<locals>.fc                      s     t d d S )N)NNN)r/   r   r   r   r4   r   r   rK   j  s    z&TestGreenlet.test_exc_state.<locals>.hr!   )r2   r+   r   r   r   test_exc_statea  s    zTestGreenlet.test_exc_statec                 C   s   dd }dd }dd }t |}| |ji  |  | |jd | |jddi |j|_| |jddi | t|| | t||d d S )	Nc                   S   s   dt  _d S Nr   )r   r   testr   r   r   r   r+   p  s    z*TestGreenlet.test_instance_dict.<locals>.fc                 S   s   | ` d S r   __dict__rP   r   r   r   deldictr  s    z0TestGreenlet.test_instance_dict.<locals>.deldictc                 S   s
   || _ d S r   r   )r#   rf   r   r   r   setdictt  s    z0TestGreenlet.test_instance_dict.<locals>.setdictr   r   )r"   r/   r   r   r   r8   	TypeError)r2   r+   r   r   r#   r   r   r   test_instance_dicto  s    zTestGreenlet.test_instance_dictc                    s2   g   fdd}t |}|  |  dg d S )Nc                      s     tt d d S r6   )r   hasattrr   r   r   Zhas_runr   r   func  s    z;TestGreenlet.test_running_greenlet_has_no_run.<locals>.funcFr   r2   r   r#   r   r   r    test_running_greenlet_has_no_run  s
    z-TestGreenlet.test_running_greenlet_has_no_runc                 C   s0   dd l }| t|j t  | t|jt  d S r   )copyr8   r   r"   deepcopy)r2   r   r   r   r   test_deepcopy  s    zTestGreenlet.test_deepcopyc                    s   t dd  t g  fdd}t | d}|  ~|  | d  | d j  d d = d  d d S )Nc                   S   s   d S r   r   r   r   r   r   r     r^   z;TestGreenlet.test_parent_restored_on_kill.<locals>.<lambda>c                      sL   z   W n: tjyF   t j t      Y n0 d S r   )r   r   rl   r   r   r   r   hubmainresultr   r   worker  s    z9TestGreenlet.test_parent_restored_on_kill.<locals>.workerr    r   r   )r"   r   r   r   r   rB   r   )r2   r   r#   r   r   r   test_parent_restored_on_kill  s    	

z)TestGreenlet.test_parent_restored_on_killc                 C   sL   t  }t dd |d}| t |  W d    n1 s>0    Y  d S )Nc                   S   s   d S r   r   r   r   r   r   r     r^   z9TestGreenlet.test_parent_return_failure.<locals>.<lambda>r    )r"   r8   r9   r   )r2   r$   re   r   r   r   test_parent_return_failure  s    z'TestGreenlet.test_parent_return_failurec                 C   s2   G dd dt }|dd }| t|jt  d S )Nc                   @   s   e Zd Zdd ZdS )z>TestGreenlet.test_throw_exception_not_lost.<locals>.mygreenletc                 S   s"   zt W n   Y n0 t| |S r   )ro   r"   __getattribute__r2   namer   r   r   r     s
    zOTestGreenlet.test_throw_exception_not_lost.<locals>.mygreenlet.__getattribute__Nr   r   r   r   r   r   r   r   
mygreenlet  s   r   c                   S   s   d S r   r   r   r   r   r   r     r^   z<TestGreenlet.test_throw_exception_not_lost.<locals>.<lambda>)r"   r8   r   r?   )r2   r   r#   r   r   r   test_throw_exception_not_lost  s    z*TestGreenlet.test_throw_exception_not_lostFc                    sZ  g  dd  fdd}t j|d}|  |d ~rH|   | tj} d t	 W d    n1 sx0    Y  st
|j}| |dkpd	|v  n| t
|jd t d jd
r| t  d j  W d    n1 s0    Y  s0t d jjd
r@ d jj  n|  d j ~d  d d = d| _d S )Nc                   S   s   t  j  d S r   r   r   r   r   r   r     s    zGTestGreenlet._do_test_throw_to_dead_thread_doesnt_crash.<locals>.workerc                     s2   t } |    |  r.|   t  d S r   )r"   r   r   r   r   rP   r   wait_for_cleanupr   r   r   creator  s    
zHTestGreenlet._do_test_throw_to_dead_thread_doesnt_crash.<locals>.creatorrV   r_   r   Bcannot switch to a different thread (which happens to have exited)zCannot switchr   T)rX   rY   r`   ra   wait_for_pending_cleanupsr8   r   r   r?   r   r;   r<   r   r/   r   r   RuntimeErrorr   f_localsrE   expect_greenlet_leak)r2   r   r   ry   r   sr   r   r   *_do_test_throw_to_dead_thread_doesnt_crash  s@    
,
,
z7TestGreenlet._do_test_throw_to_dead_thread_doesnt_crashc                 C   s   |    d S r   r   r4   r   r   r   &test_throw_to_dead_thread_doesnt_crash  s    z3TestGreenlet.test_throw_to_dead_thread_doesnt_crashc                 C   s   |  d d S rT   r   r4   r   r   r   +test_throw_to_dead_thread_doesnt_crash_wait  s    z8TestGreenlet.test_throw_to_dead_thread_doesnt_crash_waitc                 C   s2   G dd dt }| }| |dd d| _d S )Nc                   @   s$   e Zd Zdd Zdd Zdd ZdS )z7TestGreenlet.test_recursive_startup.<locals>.convolutedc                 S   s   t |  d| _d S r   )r"   __init__countr4   r   r   r   r     s    
z@TestGreenlet.test_recursive_startup.<locals>.convoluted.__init__c                 S   s.   |dkr"| j dkr"d| _ | d t| |S )Nr7   r   r   +   )r   r   r"   r   r   r   r   r   r     s    
zHTestGreenlet.test_recursive_startup.<locals>.convoluted.__getattribute__c                 S   s   | j | q d S r   r   r   r2   rf   r   r   r   r7     s    z;TestGreenlet.test_recursive_startup.<locals>.convoluted.runN)r   r   r   r   r   r7   r   r   r   r   
convoluted  s   r   r   r   T)r"   r/   r   r   )r2   r   r#   r   r   r   test_recursive_startup   s    z#TestGreenlet.test_recursive_startupc                    s   t    t    G fdddt}dd } fdd}t }t|g |  d _ d   t j	|d}|
      | t | |d	 d S )
Nc                       s   e Zd Z fddZdS )z;TestGreenlet.test_threaded_updatecurrent.<locals>.finalizedc                    s         d S r   releaseacquirer4   lock1lock2r   r   __del__  s    zCTestGreenlet.test_threaded_updatecurrent.<locals>.finalized.__del__Nr   r   r   r   r   r   r   r   	finalized  s   r   c                   S   s   t  j  d S r   r   r   r   r   r   deallocator%  s    z=TestGreenlet.test_threaded_updatecurrent.<locals>.deallocatorc                      s:      t   d=      t    d S r   r   r   r   r   r   r#   r   r   r   r   fthread'  s    z9TestGreenlet.test_threaded_updatecurrent.<locals>.fthreadr   rV   r_   )rX   Lockr   objectr   r   r"   Zbombr   rY   r`   r   r/   ra   )r2   r   r   r   r   ry   r   r   r   test_threaded_updatecurrent  s"    

z(TestGreenlet.test_threaded_updatecurrentc                    sb   g  fdddd  t gd   t  d    }|  | |d d S )Nc               
      sX   t  j } d= t  j _z"t  j|  W t   nt   0 d S r   )r   r   r   r   r   )rf   	initiatorr   r   r   r   r   G  s    z>TestGreenlet.test_dealloc_switch_args_not_lost.<locals>.workerc                   S   s   dS r   r   r   r   r   r   r   T  s    zATestGreenlet.test_dealloc_switch_args_not_lost.<locals>.initiatorr   r   )r"   r   r   r/   r   r   r   r   !test_dealloc_switch_args_not_lostE  s    

z.TestGreenlet.test_dealloc_switch_args_not_lostc                    sT   dd  G dd dt }| tdd fdd}t|}| |  d S )	Nc                 S   s   | |i | d S r   r   )r   rN   kr   r   r   _applym  s    z0TestGreenlet.test_tuple_subclass.<locals>._applyc                   @   s   e Zd Zdd ZdS )z1TestGreenlet.test_tuple_subclass.<locals>.mytuplec                 S   s   t    t| S r   )r   r   r   tuple__len__r4   r   r   r   r   q  s    z9TestGreenlet.test_tuple_subclass.<locals>.mytuple.__len__N)r   r   r   r   r   r   r   r   mytuplep  s   r   r   )rN   c                      s    t  jj d S r   r   r   r   r   kwargsr   r   switchapplyv  s    z5TestGreenlet.test_tuple_subclass.<locals>.switchapply)r   dictr"   r/   r   )r2   r   r   r#   r   r   r   test_tuple_subclass_  s    
z TestGreenlet.test_tuple_subclassc                 C   sP   t dtfdtdd i}G dd d|}G dd d|}|  | t| d S )	NAbstractSubclassr7   c                 S   s   d S r   r   r4   r   r   r   r     r^   z7TestGreenlet.test_abstract_subclasses.<locals>.<lambda>c                   @   s   e Zd ZdS )z:TestGreenlet.test_abstract_subclasses.<locals>.BadSubclassNr   r   r   r   r   BadSubclass  s   r   c                   @   s   e Zd Zdd ZdS )z;TestGreenlet.test_abstract_subclasses.<locals>.GoodSubclassc                 S   s   d S r   r   r4   r   r   r   r7     s    z?TestGreenlet.test_abstract_subclasses.<locals>.GoodSubclass.runN)r   r   r   r7   r   r   r   r   GoodSubclass  s   r   )r   r"   r   r8   r   )r2   r   r   r   r   r   r   test_abstract_subclasses{  s    z%TestGreenlet.test_abstract_subclassesc                    sN   t  sd S t  d   dk r$d S  fdd}tdD ]}| r: qJq:d S )Nr   2   c            	         s  t    t    dgfdd} t j| d}|  G fdddt G  fdddt}g }td	 }t }| }|D ]<}d }d
 r|	d  dS t
 }d
 r q|| qt  d
 r|	d dS |j| |D ]}|j| qdS )NFc                      s       t    d S r   r   r   r   r   r   another_thread  s    zWTestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.another_threadrV   c                       s   e Zd Z fddZdS )zTTestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.gc_callbackc                    s         dd< d S )NTr   r   r4   r   r   recycledr   r   r     s    z\TestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.gc_callback.__del__Nr   r   r   r   r   gc_callback  s   r   c                       s   e Zd Z fddZdS )zPTestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.garbagec                    s   | | _   | _d S r   )cyclecallbackr4   r   r   r   r     s    zYTestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.garbage.__init__N)r   r   r   r   r   r   r   r   garbage  s   r   r,   r   r_   T)rX   r   r   rY   r`   r   r1   r   r   ra   r"   r   rj   rk   r/   r   )	r   ry   r   lr   currentr#   _lastNr2   )r   r   r   r   r   attempt  s>    

z?TestGreenlet.test_implicit_parent_with_threads.<locals>.attemptr.   )rj   	isenabledZget_thresholdr1   )r2   r  r   r   r   r   !test_implicit_parent_with_threads  s    -z.TestGreenlet.test_implicit_parent_with_threadsc                    s   ddl m ddl m  G dd dt}t|}g _ fdd}tdD ]}||  qP`t	r~
t|| nt|| d S )	Nr   r   )rl   c                   @   s   e Zd ZdS )zTTestGreenlet.test_issue_245_reference_counting_subclass_no_threads.<locals>.GreenletNr   r   r   r   r   Greenlet  s   r  c                      s6   z j   W n   y0   j  Y n0 d S r   )r   r   gletsr   r   rl   r   r2   r   r   greenlet_main  s    zYTestGreenlet.test_issue_245_reference_counting_subclass_no_threads.<locals>.greenlet_mainr_   )r   r   rl   r"   r   rR   r  r1   r   r
   ZassertGreaterEqualr/   )r2   r  initial_refsr	  r   r   r  r   5test_issue_245_reference_counting_subclass_no_threads  s    
	zBTestGreenlet.test_issue_245_reference_counting_subclass_no_threadsz;The manylinux images appear to hang on this test on 3.13rc2c                    s   ddl m} ddl m} ddlm G dd dt g | fdd fd	d
}t }g }|d }tr|d}t	|D ]*}| }|||fd}	|
| |	  q|D ]}
|
d qd d =   |   | t | d S )Nr   )rY   )rx   r  c                   @   s   e Zd ZdS )zSTestGreenlet.test_issue_245_reference_counting_subclass_threads.<locals>.MyGreenletNr   r   r   r   r   
MyGreenlet  s   r  c                      s     j   d S r   r   r   r  r   r   r	     s    zVTestGreenlet.test_issue_245_reference_counting_subclass_threads.<locals>.greenlet_mainc                    s8    } | |  ~|   d   d S )Nr_   )r   r   ru   rv   )Zgreenlet_running_eventZminer  r   r  r	  Zref_clearedr   r   thread_main  s    

zTTestGreenlet.test_issue_245_reference_counting_subclass_threads.<locals>.thread_main-   r   r   r_   )rX   rY   rx   r   r   r"   r   rR   r
   r1   r   r`   rv   ru   r   r/   )r2   rY   rx   r  r
  Zthread_ready_eventsZthread_countr   eventr   r   r   r  r   2test_issue_245_reference_counting_subclass_threads  s0    	



z?TestGreenlet.test_issue_245_reference_counting_subclass_threadsc                 C   sX   dd }t |}dd }t ||}| t |  W d    n1 sJ0    Y  d S )Nc                   S   s   dS )N   r   r   r   r   r   no_args:  s    z\TestGreenlet.test_falling_off_end_switches_to_unstarted_parent_raises_error.<locals>.no_argsc                   S   s   dS r   r   r   r   r   r   leaf?  s    zYTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_raises_error.<locals>.leaf)r"   r8   r   r   )r2   r  parent_never_startedr  childr   r   r   >test_falling_off_end_switches_to_unstarted_parent_raises_error9  s    
zKTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_raises_errorc                 C   s:   dd }t |}dd }t ||}| }| |d d S )Nc                 S   s   | dfS N   r   r   r   r   r   one_argI  s    zUTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_works.<locals>.one_argc                   S   s   dS r   r   r   r   r   r   r  N  s    zRTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_works.<locals>.leaf)r   r  r   )r2   r  r  r  r  r   r   r   r   7test_falling_off_end_switches_to_unstarted_parent_worksH  s    
zDTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_worksc                    s   G dd dt }| }g  t  fdd|}| t |  W d    n1 sT0    Y  |  dg | t |  W d    n1 s0    Y  |  dg d S )Nc                   @   s   e Zd Zdd ZdS )zXTestGreenlet.test_switch_to_dead_greenlet_with_unstarted_perverse_parent.<locals>.Parentc                 S   s   |dkrt d S r6   )r   r   r   r   r   r   X  s    ziTestGreenlet.test_switch_to_dead_greenlet_with_unstarted_perverse_parent.<locals>.Parent.__getattribute__Nr   r   r   r   r   ParentW  s   r  c                      s
     dS r   r   r   r   r   r   r   _  r^   zZTestGreenlet.test_switch_to_dead_greenlet_with_unstarted_perverse_parent.<locals>.<lambda>r   )r"   r8   r   r   r/   )r2   r  r  r  r   r   r   ;test_switch_to_dead_greenlet_with_unstarted_perverse_parentV  s    &&zHTestGreenlet.test_switch_to_dead_greenlet_with_unstarted_perverse_parentc                    sf   g  t  fdd}t  fdd}|  |  dg ||_| }| | |  ddg d S )Nc                      s
     dS r  r  r   r   r   r   r   m  r^   zDTestGreenlet.test_switch_to_dead_greenlet_reparent.<locals>.<lambda>c                      s
     dS r   r  r   r   r   r   r   n  r^   r   r  )r"   r   r/   r   rE   )r2   r  r  r   r   r   r   %test_switch_to_dead_greenlet_reparentk  s    
z2TestGreenlet.test_switch_to_dead_greenlet_reparentc                    s   t   fdd}fdd t|}|  tdd }|  | |j | |jjjd | |jj	 | |jj	jjd | 
|jj	j	 d S )	Nc                      s
      d S r   r   r   innerr   r   outer  s    zHTestGreenlet.test_can_access_f_back_of_suspended_greenlet.<locals>.outerc                      s     td d S r   )r   r   r|   r   r   r   r   r!    s    zHTestGreenlet.test_can_access_f_back_of_suspended_greenlet.<locals>.innerc                   S   s   d S r   r   r   r   r   r   r     r^   zKTestGreenlet.test_can_access_f_back_of_suspended_greenlet.<locals>.<lambda>r!  r"  )r   r   r"   r   rA   r   r/   f_codeco_namer}   rE   )r2   r"  r   	unrelatedr   r!  r   r   ,test_can_access_f_back_of_suspended_greenlety  s    z9TestGreenlet.test_can_access_f_back_of_suspended_greenletc                    s   ddl m ddlm   fddt}|d |j}tdD ]}| |j	d | |j
}qH| |j	d d | |j
 | |d	d
 d S )Nr   )partialr   )_test_extension_cppc                    s.   | dkr |   | d  S t j S )Nr   r   )Z	test_callr   r   r   r   )rs   r*  r)  recurser   r   r,    s    z@TestGreenlet.test_get_stack_with_nested_c_calls.<locals>.recurser.   rs   r_   i  )	functoolsr)   r*  r"   r   r   r1   r/   r   r}   rE   )r2   grr   r[   r   r+  r   "test_get_stack_with_nested_c_calls  s    
z/TestGreenlet.test_get_stack_with_nested_c_callsc                    sd   t   fdd}fdd t|}| }tdd }|  | |jjd | |j d S )Nc                      s    t d d S r   )r   r|   r   r   r   r   r"    s    z6TestGreenlet.test_frames_always_exposed.<locals>.outerc                    s     |  d S r   )r   )r   r#  r   r   r!    s    z6TestGreenlet.test_frames_always_exposed.<locals>.innerc                   S   s   d S r   r   r   r   r   r   r     r^   z9TestGreenlet.test_frames_always_exposed.<locals>.<lambda>r"  )	r   r   r"   r   r/   r$  r%  rE   r}   )r2   r"  r/  r   r&  r   r'  r   test_frames_always_exposed  s    z'TestGreenlet.test_frames_always_exposedN)F)4r   r   r   r3   r5   r>   r@   rD   rF   rL   rS   rc   rg   ri   rm   rt   unittestZskipIfr   r   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r(  r0  r1  r   r   r   r   r&   4   sn   
7
	9

/7*
Fr&   c                   @   s\   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd ZdS )TestGreenletSetParentErrorsc                    s   i t   t   fdd}dd }t j|d}|   d t|}|  | t}d |_	W d    n1 s0    Y  
  |d | t|jd d S )	Nc                      s&   t dd d<    d d S )Nc                   S   s   d S r   r   r   r   r   r   r     r^   zQTestGreenletSetParentErrors.test_threaded_reparent.<locals>.run.<locals>.<lambda>r#   r_   r   r   r   r   r   r7     s    z?TestGreenletSetParentErrors.test_threaded_reparent.<locals>.runc                   S   s   t  j  d S r   r   r   r   r   r   blank  s    zATestGreenletSetParentErrors.test_threaded_reparent.<locals>.blankrV   r_   r#   z&parent cannot be on a different thread)rX   rx   rY   r`   rv   r"   r   r8   r   r   ru   ra   r/   r;   r<   )r2   r7   r4  r   r#   r   r   r   r   test_threaded_reparent  s    
(
z2TestGreenletSetParentErrors.test_threaded_reparentc                    s   g   fdd}t j|d}|  |d | j| jd d G  fdddt}|d	d
 }| tj	}|
  W d    n1 s0    Y  | t|jd  d d = d S )Nc                     s"   t dd }  |  |   d S )Nc                   S   s   d S r   r   r   r   r   r   r     r^   zYTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.worker.<locals>.<lambda>)r"   r   r   rP   anotherr   r   r     s    
zGTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.workerrV   r_   r   )Zinitial_main_greenletsc                       s   e Zd Z fddZdS )zKTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.convolutedc                    s   |dkr d | _ t| |S Nr7   r   r   r"   r   r   r6  r   r   r     s    
z\TestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.convoluted.__getattribute__Nr   r   r6  r   r   r     s   r   c                   S   s   d S r   r   r   r   r   r   r     r^   zITestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.<lambda>r   )rX   rY   r`   ra   r   Zmain_greenlets_before_testr"   r8   r   r   r   r/   r;   r<   )r2   r   ry   r   r#   r   r   r6  r   test_unexpected_reparenting  s    
&z7TestGreenletSetParentErrors.test_unexpected_reparentingc              	      s  g  t  t   fdd}G  fdddt}t j|d}|  d z|dd }| tj}|	  W d    n1 s0    Y  | 
d	t|j | 
d
t|j | 
dt|j W   |d  d d = n  |d  d d = 0 d S )Nc                     s4   t dd }  |  |     d d S )Nc                   S   s   d S r   r   r   r   r   r   r     r^   zhTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.worker.<locals>.<lambda>r_   )r"   r   r   ru   rv   rP   r7  Zkeep_main_aliveZswitched_to_greenletr   r   r     s
    
zVTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.workerc                       s   e Zd Z fddZdS )zZTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.convolutedc                    s   |dkr d | _ t| |S r8  r9  r   r6  r   r   r     s    
zkTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.convoluted.__getattribute__Nr   r   r6  r   r   r     s   r   rV   r_   c                   S   s   d S r   r   r   r   r   r   r     r^   zXTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.<lambda>z#Cannot switch to a different threadZExpectedZCurrent)rX   rx   r"   rY   r`   rv   r8   r   r   r   r:   r;   r<   ru   ra   )r2   r   r   ry   r#   r   r   r;  r   *test_unexpected_reparenting_thread_running  s*    
&

zFTestGreenletSetParentErrors.test_unexpected_reparenting_thread_runningc                 C   sb   t dd }| |jt  | t}|`W d    n1 sB0    Y  | t|j	d d S )Nc                   S   s   d S r   r   r   r   r   r   r   *  r^   zGTestGreenletSetParentErrors.test_cannot_delete_parent.<locals>.<lambda>can't delete attribute)
r"   rB   r   r   r   r8   r9   r/   r;   r<   )r2   r   r   r   r   r   test_cannot_delete_parent)  s
    "z5TestGreenletSetParentErrors.test_cannot_delete_parentc                 C   sH   |  t}t `W d    n1 s(0    Y  | t|jd d S )Nr=  r8   r9   r   r   r   r/   r;   r<   r2   r   r   r   r   !test_cannot_delete_parent_of_main1  s    &z=TestGreenletSetParentErrors.test_cannot_delete_parent_of_mainc                 C   s   |  t j d S r   )rE   r   r   r   r4   r   r   r   !test_main_greenlet_parent_is_none7  s    z=TestGreenletSetParentErrors.test_main_greenlet_parent_is_nonec                    sT   dd } fdd}t |} | || |   | || |  d S )Nc                   S   s   t  j  d S r   r   r   r   r   r   bg<  s    zCTestGreenletSetParentErrors.test_set_parent_wrong_types.<locals>.bgc              	      sb   d d dfD ]P}  t}|| _W d    n1 s60    Y   t|jdt|j  qd S )Nr   Z42z4GreenletChecker: Expected any type of greenlet, not )r8   r   r   r/   r;   r<   typer   )gletpr   r4   r   r   check@  s    $zFTestGreenletSetParentErrors.test_set_parent_wrong_types.<locals>.check)r"   r   r   r   )r2   rC  rG  r#   r   r4   r   test_set_parent_wrong_types;  s    


z7TestGreenletSetParentErrors.test_set_parent_wrong_typesc                 C   sR   t dd }| t}||_W d    n1 s20    Y  | t|jd d S )Nc                   S   s   d S r   r   r   r   r   r   r   X  r^   z@TestGreenletSetParentErrors.test_trivial_cycle.<locals>.<lambda>cyclic parent chain)r"   r8   r   r   r/   r;   r<   )r2   rE  r   r   r   r   test_trivial_cycleW  s    $z.TestGreenletSetParentErrors.test_trivial_cyclec                 C   sN   |  t}t t _W d    n1 s.0    Y  | t|jd d S )Nz(cannot set the parent of a main greenletr?  r@  r   r   r   test_trivial_cycle_main]  s    ,z3TestGreenletSetParentErrors.test_trivial_cycle_mainc                 C   sv   t dd }t dd }t dd }||_||_| t}||_W d    n1 sV0    Y  | t|jd d S )Nc                   S   s   d S r   r   r   r   r   r   r   d  r^   z?TestGreenletSetParentErrors.test_deeper_cycle.<locals>.<lambda>c                   S   s   d S r   r   r   r   r   r   r   e  r^   c                   S   s   d S r   r   r   r   r   r   r   f  r^   rI  )r"   r   r8   r   r/   r;   r<   )r2   r$   re   Zg3r   r   r   r   test_deeper_cyclec  s    $z-TestGreenletSetParentErrors.test_deeper_cycleN)r   r   r   r5  r:  r<  r>  rA  rB  rH  rJ  rK  rL  r   r   r   r   r3    s   )$r3  c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )TestReprc                 C   s   |  ||||f d S r   )r   endswith)r2   gotsuffixr   r   r   assertEndsWithq  s    zTestRepr.assertEndsWithc                 C   s   t t }| |d d S )N current active started main>)reprr   r   rQ  r2   rr   r   r   test_main_while_runningt  s    z TestRepr.test_main_while_runningc                    s4   t    fdd}t|}| }| |d d S )Nc                      s   t  S r   )rS  r   r#  r   r   r7   z  s    z-TestRepr.test_main_in_background.<locals>.run suspended active started main>)r   r   r"   r   rQ  )r2   r7   r#   rU  r   r#  r   test_main_in_backgroundx  s
    z TestRepr.test_main_in_backgroundc                 C   s   t t }| |d d S )Nz	 pending>)rS  r"   rQ  rT  r   r   r   test_initial  s    
zTestRepr.test_initialc                    s   t   G  fdddtj}| }|  |d | |jd | |jd t	dD ]}t
d qZt	dD ](}| |jj t|j}| |d qrd S )	Nc                       s$   e Zd Zd ZZdZ fddZdS )z/TestRepr.test_main_from_other_thread.<locals>.TNc                    s$   t  | _t | _t | j| _d S r   )rS  original_mainr   r   	main_gletr  r4   r#  r   r   r7     s    

z3TestRepr.test_main_from_other_thread.<locals>.T.run)r   r   r   rZ  r  r[  r7   r   r#  r   r   T  s   r\  r_   rW  rR  r'   gMbP?z (thread exited) dead>)r   r   rX   rY   r`   ra   rQ  rZ  r  r1   r   r   r   r[  rJ   rS  )r2   r\  ry   r   rU  r   r#  r   test_main_from_other_thread  s    

z$TestRepr.test_main_from_other_threadc                 C   sX   t dd }|  | t|d | dt| | dt| | dt| d S )Nc                   S   s   d S r   r   r   r   r   r   r     r^   z$TestRepr.test_dead.<locals>.<lambda>z dead>Z	suspendedstartedactive)r"   r   rQ  rS  ZassertNotInrC   r   r   r   	test_dead  s    zTestRepr.test_deadc                 C   sX   t dd }t dd }t }|||fD ](}| d|f t | d|f t q*d S )Nc                   S   s   d S r   r   r   r   r   r   r     r^   z>TestRepr.test_formatting_produces_native_str.<locals>.<lambda>c                   S   s   d S r   r   r   r   r   r   r     r^   z%sz%r)r"   r   r   assertIsInstancer;   )r2   Zg_deadZg_not_startedZg_curr#   r   r   r   #test_formatting_produces_native_str  s    z,TestRepr.test_formatting_produces_native_strN)
r   r   r   rQ  rV  rX  rY  r]  r`  rb  r   r   r   r   rM  o  s   	#rM  c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestMainGreenletc                 C   s4   dt t v sJ tt }dt |vs0J |S )Nr   )rS  r   r   rD  )r2   ry   r   r   r   _check_current_is_main  s    z'TestMainGreenlet._check_current_is_mainc                 C   s$   |   }td|fi }| | d S )Nsubclass)rd  rD  rA   )r2   Z	main_typere  r   r   r   )test_main_greenlet_type_can_be_subclassed  s    z:TestMainGreenlet.test_main_greenlet_type_can_be_subclassedc                 C   s   |    | t t d S r   )rd  ra  r   r   r"   r4   r   r   r   test_main_greenlet_is_greenlet  s    z/TestMainGreenlet.test_main_greenlet_is_greenletN)r   r   r   rd  rf  rg  r   r   r   r   rc    s   rc  c                   @   sL   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )TestBrokenGreenletsc                 C   sR   dd }t j|}d|_| td |  W d    n1 sD0    Y  d S )Nc                   S   s   t dd S )NzNever get here)AssertionErrorr   r   r   r   r     s    z<TestBrokenGreenlets.test_failed_to_initialstub.<locals>.funcTz;Failed to switch stacks into a greenlet for the first time.)r   	_greenletUnswitchableGreenletforce_switch_errorassertRaisesRegexSystemErrorr   r   r   r   r   test_failed_to_initialstub  s    z.TestBrokenGreenlets.test_failed_to_initialstubc                    s   g   fdd}t j|}|  |  dg |  |  ddg d|_| td |  W d    n1 sz0    Y  d|_|  |  g d d S )	Nc                      s>     d t j    d t j    d d S )Nr   r,   r'   r(   r   runsr   r   r     s
    

zDTestBrokenGreenlets.test_failed_to_switch_into_running.<locals>.funcr   r,   Tz0Failed to switch stacks into a running greenlet.F)r   r,   r'   )r   rj  rk  r   r/   rl  rm  rn  r   r   rp  r   "test_failed_to_switch_into_running  s    &z6TestBrokenGreenlets.test_failed_to_switch_into_runningc                 C   s.   |  d}| d|j | |j|   d S )Nzfail_slp_switch.pyzfail_slp_switch is running)assertScriptRaisesr:   output
returncodeZ,get_expected_returncodes_for_aborted_processr2   exr   r   r   &test_failed_to_slp_switch_into_running  s    
z:TestBrokenGreenlets.test_failed_to_slp_switch_into_runningc                 C   sV   |  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_scriptr:   r2   rt  r   r   r   #test_reentrant_switch_two_greenlets  s    
z7TestBrokenGreenlets.test_reentrant_switch_two_greenletsc                 C   s.   | j ddd}| d|j | d|j d S )Nzfail_switch_three_greenlets.py)r   )Z	exitcodesr   zpositional arguments)rs  r:   rt  rv  r   r   r   %test_reentrant_switch_three_greenlets  s    z9TestBrokenGreenlets.test_reentrant_switch_three_greenletsc                 C   s   |  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')]rz  r|  r   r   r   &test_reentrant_switch_three_greenlets2   s
    
z:TestBrokenGreenlets.test_reentrant_switch_three_greenlets2c                 C   s   |  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)]rz  r|  r   r   r   4test_reentrant_switch_GreenletAlreadyStartedInPython2  s
    
zHTestBrokenGreenlets.test_reentrant_switch_GreenletAlreadyStartedInPythonc                 C   s   |  d}| d| d S )Nzfail_clearing_run_switches.pyzRESULTS [('G.__getattribute__', 'run'), ('RunCallable', '__del__'), ('main: g.switch()', 'from RunCallable'), ('run_func', 'enter')]rz  r|  r   r   r   *test_reentrant_switch_run_callable_has_del>  s
    
z>TestBrokenGreenlets.test_reentrant_switch_run_callable_has_delN)r   r   r   ro  rr  rx  r}  r~  r  r  r  r   r   r   r   rh    s   
	rh  __main__)rj   r   r   rX   r2  abcr   r   r   r"   r.  r   r   r   r	   r
   Z	leakcheckr   ro   r   r   r%   r&   r3  rM  rc  rh  r   r   r   r   r   r   <module>   s@   		        .Um
