o
    E6ds                     @   s   d dl mZ d dl mZ d dl mZ d dlZd dlZd dlZd dlZd dlm	Z	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edkrzd dlZe  dS dS )    )absolute_import)division)print_functionN)ABCMetaabstractmethod)greenlet   )TestCase)fails_leakcheckc                   @      e Zd ZdS )	SomeErrorN__name__
__module____qualname__ r   r   sC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\greenlet/tests/test_greenlet.pyr          r   c                 C   s0   z
t  j  W t   | t d   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   | Nr   )excr   r   r   crasher%      zsend_exception.<locals>.crasherr   r   r   )gr   r    g1r   r   r   send_exception"   s   r&   c                   @   sb  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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/d0 Zd1d2 Zd3d4 ZedUd6d7Zed8d9 Zd:d; Z ed<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dJdK Z(dLdM Z)dNdO Z*dPdQ Z+dRdS Z,dTS )V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   f0   s   
z'TestGreenlet._do_simple_test.<locals>.fr            )r   r   r   assertEquallistrange)selfr,   r$   r   r*   r   _do_simple_test-   s   


zTestGreenlet._do_simple_testc                 C      |    d S r   )r4   r3   r   r   r   test_simple<   s   zTestGreenlet.test_simplec                 C   sN   t  }| t}|  W d    n1 sw   Y  | dt|j d S Nrun)r   assertRaisesAttributeErrorr   assertInstr	exceptionr3   r$   r   r   r   r   (test_switch_no_run_raises_AttributeError?   s
   
z5TestGreenlet.test_switch_no_run_raises_AttributeErrorc                 C   sP   t  }| t}|t W d    n1 sw   Y  | dt|j d S r8   )r   r:   r;   throwr   r<   r=   r>   r?   r   r   r   'test_throw_no_run_raises_AttributeErrorF   s
   z4TestGreenlet.test_throw_no_run_raises_AttributeErrorc                 C   s*   t d d}| | | |jt   d S )Nr"   )r   assertIsNotNoneassertIsr   r   r3   r$   r   r   r   test_parent_equals_NoneM   s   

z$TestGreenlet.test_parent_equals_Nonec                 C   s$   t d d}| | | |j d S )N)r9   )r   rC   assertIsNoner9   rE   r   r   r   test_run_equals_NoneR      

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,   Z   s   
z)TestGreenlet.test_two_children.<locals>.fr   r-   r.   T   )r   r   r0   lendead)r3   r,   r$   hr   r*   r   test_two_childrenW   s   zTestGreenlet.test_two_childrenc                    sf   g fdd  fdd}t |}| t|d |  | g d | t|d d S )Nc                      s     d t j  d S )Nbr)   r   r*   r   r   r,   n   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$   r   s   
z3TestGreenlet.test_two_recursive_children.<locals>.gr-   )rQ   rP   rR   )r   r0   r   getrefcountr   rE   r   rT   r   test_two_recursive_childrenk   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)r4   r   r   )r3   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>   s    z-TestGreenlet.test_threads.<locals>.<listcomp>
   )r2   startjoinr0   rL   )r3   Zthsthr   )r,   r3   rX   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   r0   r:   r   )r3   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   r:   KeyErrorr&   r0   )r3   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   r0   gccollectGreenletExit)r3   r   r%   rg   r   r   r   test_dealloc   s   

zTestGreenlet.test_deallocc                 C   s   dd }t |}|  tj}zddlm} W n ty&   ddlm} Y nw |  }t_z~W |t_n|t_w | }| 	d| | 	d| | 	d| d S )Nc                   S   s*   z
t  j  W d S  t jy   tw r   )r   r   r   r   rn   r   r   r   r   r   r9      s
   zHTestGreenlet.test_dealloc_catches_GreenletExit_throws_other.<locals>.runr   )StringIO	ExceptionZignoredr   )
r   r   r   stderr	cStringIOrp   ImportErroriogetvaluer<   )r3   r9   r$   Z	oldstderrrp   rr   vr   r   r   .test_dealloc_catches_GreenletExit_throws_other   s"   z;TestGreenlet.test_dealloc_catches_GreenletExit_throws_otherc              	      s  g g t   t  t  t   fdd}t j|d}|   d | g  | td d d = t  | g  	  d z| t
jg W 	  |d d d = d d = d S 	  |d d d = d d = w )Nc                     sZ   t t} |  |  ~ t     d t     d t   d S )Nr(   )r   r   r   r   rl   rm   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>.frY   ra   r   r(   )r[   Eventr\   rb   rz   r0   rL   rl   rm   ry   r   rn   rc   )r3   r,   tr   r{   r   test_dealloc_other_thread   s6   






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   	_getframer0   f_backr   r   r   r   r_   r6   r   r   f1  s   
z#TestGreenlet.test_frame.<locals>.f1r   )r   r   
assertTruegr_frameassertFalser0   )r3   r   r$   frameZfrom_gr   r6   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>     
 z>TestGreenlet.test_thread_bug.<locals>.runner.<locals>.<lambda>r#   )r   r$   r   r   r   runner  s   z,TestGreenlet.test_thread_bug.<locals>.runner)g?rZ   args)g333333?ra   )r[   r\   rb   rc   )r3   r   t1t2r   r   r   test_thread_bug  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-   *   )r0   rQ   rP   r6   r   r   r9   #  s   z,TestGreenlet.test_switch_kwargs.<locals>.runr.   r-   r   r   r   r   r0   )r3   r9   r   r   r6   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   r9   +  s   z6TestGreenlet.test_switch_kwargs_to_parent.<locals>.runr   r(   )r-   )r(   	   )r   r0   r   )r3   r9   r$   r   r   r   test_switch_kwargs_to_parent*  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 s8w   Y  	  |
d   d S )Nc                      &   t dd d<    d d S )Nc                   S      d S r   r   r   r   r   r   r   :      zITestGreenlet.test_switch_to_another_thread.<locals>.run.<locals>.<lambda>r$   ra   r   ry   rz   r   Zcreated_eventdata
done_eventr   r   r9   9     z7TestGreenlet.test_switch_to_another_thread.<locals>.runrY   ra   r$   )r[   r|   r\   rb   rz   r:   r   errorr   ry   rc   clear)r3   r9   threadr   r   r   test_switch_to_another_thread4  s   

z*TestGreenlet.test_switch_to_another_threadc                    s*    fdd}fdd t |  d S )Nc                     s:   zt d   t } t   | t  Y d S )NZfun)
ValueErrorr   r   r   r   r0   )r   rN   r3   r   r   r,   H  s   z&TestGreenlet.test_exc_state.<locals>.fc                      s     t d d S )N)NNN)r0   r   r   r   r6   r   r   rN   P  s   z&TestGreenlet.test_exc_state.<locals>.hr#   )r3   r,   r   r   r   test_exc_stateG  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,   V     z*TestGreenlet.test_instance_dict.<locals>.fc                 S   s   | ` d S r   __dict__rS   r   r   r   deldictX     z0TestGreenlet.test_instance_dict.<locals>.deldictc                 S   s
   || _ d S r   r   )r$   rh   r   r   r   setdictZ  s   
z0TestGreenlet.test_instance_dict.<locals>.setdictr   r   )r   r0   r   r   r   r:   	TypeError)r3   r,   r   r   r$   r   r   r   test_instance_dictU  s   zTestGreenlet.test_instance_dictc                    s2   g   fdd}t |}|  |  dg d S )Nc                      s     tt d d S r8   )r   hasattrr   r   r   Zhas_runr   r   funch  s   z;TestGreenlet.test_running_greenlet_has_no_run.<locals>.funcFr   )r3   r   r$   r   r   r    test_running_greenlet_has_no_runf  s
   z-TestGreenlet.test_running_greenlet_has_no_runc                 C   s0   dd l }| t|j t  | t|jt  d S r   )copyr:   r   r   deepcopy)r3   r   r   r   r   test_deepcopyq  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   r   r   r   r   r   r   r   r   w  r   z;TestGreenlet.test_parent_restored_on_kill.<locals>.<lambda>c                      sL   z   W d S  tjy%   t j t      Y d S w r   )r   r   rn   r   r   r   r   Zhubmainresultr   r   workerz  s   z9TestGreenlet.test_parent_restored_on_kill.<locals>.workerr"   r   r   )r   r   r   r   rD   r   )r3   r   r$   r   r   r   test_parent_restored_on_killv  s   	

z)TestGreenlet.test_parent_restored_on_killc                 C   sN   t  }t dd |d}| t |  W d    d S 1 s w   Y  d S )Nc                   S   r   r   r   r   r   r   r   r     r   z9TestGreenlet.test_parent_return_failure.<locals>.<lambda>r"   )r   r:   r;   r   )r3   r%   rg   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                   @      e Zd Zdd ZdS )z>TestGreenlet.test_throw_exception_not_lost.<locals>.mygreenletc                 S   s   zt     Y t| |S r   )rq   r   __getattribute__r3   namer   r   r   r     s
   zOTestGreenlet.test_throw_exception_not_lost.<locals>.mygreenlet.__getattribute__Nr   r   r   r   r   r   r   r   
mygreenlet      r   c                   S   r   r   r   r   r   r   r   r     r   z<TestGreenlet.test_throw_exception_not_lost.<locals>.<lambda>)r   r:   r   rA   )r3   r   r$   r   r   r   test_throw_exception_not_lost  s   z*TestGreenlet.test_throw_exception_not_lostFc                    s8  g  dd  fdd}t j|d}|  |d ~r$|   | tj} d t	 W d    n1 s<w   Y  sO| 
t|jdd	g n	| t|jd t d jd
r|| t  d j  W d    n1 sww   Y  s d jj  n|  d j ~d  d d = d| _d S )Nc                   S      t  j  d S r   r   r   r   r   r   r        zGTestGreenlet._do_test_throw_to_dead_thread_doesnt_crash.<locals>.workerc                     s6   t } |    |  r|   t   d S d S r   )r   r   r   r   rS   r   wait_for_cleanupr   r   r   creator  s   
zHTestGreenlet._do_test_throw_to_dead_thread_doesnt_crash.<locals>.creatorrY   ra   r   Bcannot switch to a different thread (which happens to have exited)#cannot switch to a different threadr   T)r[   r\   rb   rc   wait_for_pending_cleanupsr:   r   r   rA   r   r<   r=   r>   r0   r   r   RuntimeErrorr   f_localsrG   expect_greenlet_leak)r3   r   r   r}   r   r   r   r   *_do_test_throw_to_dead_thread_doesnt_crash  sD   


z7TestGreenlet._do_test_throw_to_dead_thread_doesnt_crashc                 C   r5   r   r   r6   r   r   r   &test_throw_to_dead_thread_doesnt_crash  s   z3TestGreenlet.test_throw_to_dead_thread_doesnt_crashc                 C   s   |  d d S rW   r   r6   r   r   r   +test_throw_to_dead_thread_doesnt_crash_wait  r   z8TestGreenlet.test_throw_to_dead_thread_doesnt_crash_waitc                 C   s2   G dd dt }| }| |dd d| _d S )Nc                   @   $   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__countr6   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 )Nr9   r   r   +   )r   r   r   r   r   r   r   r   r     s   
zHTestGreenlet.test_recursive_startup.<locals>.convoluted.__getattribute__c                 S   s   	 | j | qr   r   r   r3   rh   r   r   r   r9     s   z;TestGreenlet.test_recursive_startup.<locals>.convoluted.runN)r   r   r   r   r   r9   r   r   r   r   
convoluted  s    r   r   r   T)r   r0   r   r   )r3   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acquirer6   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   r   r   r   r   r   r   r   deallocator
  r   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   rY   ra   )r[   Lockr   objectr   r   bombr   r\   rb   r   r0   rc   )r3   r   r   r   r   r}   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               
      sV   t  j } d= t  j _zt  j|  W t   d S t   w r   )r   r   r   r   r   )rh   	initiatorr   r   r   r   r   ,  s   "z>TestGreenlet.test_dealloc_switch_args_not_lost.<locals>.workerc                   S      dS r   r   r   r   r   r   r   9  r!   zATestGreenlet.test_dealloc_switch_args_not_lost.<locals>.initiatorr   r   )r   r   r   r0   r   r   r   r   !test_dealloc_switch_args_not_lost*  s   

z.TestGreenlet.test_dealloc_switch_args_not_lostc                    sh   t jd dkrdd  nt G dd dt}| tdd fd	d
}t|}| |  d S )Nr   r-   c                 S   s   | |i | d S r   r   )r   rQ   kr   r   r   _applyU  r   z0TestGreenlet.test_tuple_subclass.<locals>._applyc                   @   r   )z1TestGreenlet.test_tuple_subclass.<locals>.mytuplec                 S   s   t    t| S r   )r   r   r   tuple__len__r6   r   r   r   r   [  s   
z9TestGreenlet.test_tuple_subclass.<locals>.mytuple.__len__N)r   r   r   r   r   r   r   r   mytupleZ  r   r   r   )rQ   c                      s    t  jj d S r   r   r   r   r   kwargsr   r   switchapply`  s   z5TestGreenlet.test_tuple_subclass.<locals>.switchapply)r   version_infoapplyr   dictr   r0   r   )r3   r   r   r$   r   r   r   test_tuple_subclassD  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AbstractSubclassr9   c                 S   r   r   r   r6   r   r   r   r   i  r   z7TestGreenlet.test_abstract_subclasses.<locals>.<lambda>c                   @   r   )z:TestGreenlet.test_abstract_subclasses.<locals>.BadSubclassNr   r   r   r   r   BadSubclassk  r   r  c                   @   r   )z;TestGreenlet.test_abstract_subclasses.<locals>.GoodSubclassc                 S   r   r   r   r6   r   r   r   r9   o  r!   z?TestGreenlet.test_abstract_subclasses.<locals>.GoodSubclass.runN)r   r   r   r9   r   r   r   r   GoodSubclassn  r   r  )r   r   r   r:   r   )r3   r  r  r  r   r   r   test_abstract_subclassese  s   z%TestGreenlet.test_abstract_subclassesc                    sP   t  sd S t  d   dk rd S  fdd}tdD ]}| r% d S q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
 rd n|
| qKt  d
 rw|	d dS |j| |D ]	}|j| qdS )NFc                      s       t    d S r   r   r   r   r   r   another_thread     zWTestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.another_threadrY   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   r6   r   r   recycledr   r   r     r  z\TestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.gc_callback.__del__Nr   r   r  r   r   gc_callback  s    r
  c                          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callbackr6   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      r  r-   r   ra   T)r[   r   r   r\   rb   r   r2   r   r   rc   r   rl   rm   r0   r   )	r  r}   r  lr   currentr$   _lastNr3   )r
  r   r   r	  r   attempt{  s>   

z?TestGreenlet.test_implicit_parent_with_threads.<locals>.attemptr/   )rl   	isenabledZget_thresholdr2   )r3   r  r  r   r  r   !test_implicit_parent_with_threadsu  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 ]}||  q(`t|| d S )	Nr   r   )rn   c                   @   r   )zTTestGreenlet.test_issue_245_reference_counting_subclass_no_threads.<locals>.GreenletNr   r   r   r   r   Greenlet  r   r  c                      s6   z	 j   W d S   y   j  Y d S w r   )r   r   gletsr   r   rn   r   r3   r   r   greenlet_main  s
   zYTestGreenlet.test_issue_245_reference_counting_subclass_no_threads.<locals>.greenlet_mainra   )	r   r   rn   r   rU   r  r2   r   r0   )r3   r  initial_refsr  r  r   r  r   5test_issue_245_reference_counting_subclass_no_threads  s   
zBTestGreenlet.test_issue_245_reference_counting_subclass_no_threadsc           
         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 }t|d D ]}| }|||fd}|| |	  q<|D ]}	|	
d qTd d =   |   | t | d S )Nr   )r\   )r|   r  c                   @   r   )zSTestGreenlet.test_issue_245_reference_counting_subclass_threads.<locals>.MyGreenletNr   r   r   r   r   
MyGreenlet  r   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 )Nra   )r   r   ry   rz   )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   ra   )r[   r\   r|   r   r   r   rU   r2   r   rb   rz   ry   r   r0   )
r3   r\   r|   r#  r  Zthread_ready_eventsr  eventr   r   r   r"  r   2test_issue_245_reference_counting_subclass_threads  s.   



z?TestGreenlet.test_issue_245_reference_counting_subclass_threadsc                 C   sZ   dd }t |}dd }t ||}| t |  W d    d S 1 s&w   Y  d S )Nc                   S   r   )N   r   r   r   r   r   no_args  r!   z\TestGreenlet.test_falling_off_end_switches_to_unstarted_parent_raises_error.<locals>.no_argsc                   S   r   r   r   r   r   r   r   leaf  r!   zYTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_raises_error.<locals>.leaf)r   r:   r   r   )r3   r(  parent_never_startedr)  childr   r   r   >test_falling_off_end_switches_to_unstarted_parent_raises_error  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_arg  r   zUTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_works.<locals>.one_argc                   S   r   r   r   r   r   r   r   r)  #  r!   zRTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_works.<locals>.leaf)r   r.  r   )r3   r/  r*  r)  r+  r   r   r   r   7test_falling_off_end_switches_to_unstarted_parent_works  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 s*w   Y  |  dg | t |  W d    n1 sJw   Y  |  dg d S )Nc                   @   r   )zXTestGreenlet.test_switch_to_dead_greenlet_with_unstarted_perverse_parent.<locals>.Parentc                 S   s   |dkrt d S r8   )r   r   r   r   r   r   -  s   ziTestGreenlet.test_switch_to_dead_greenlet_with_unstarted_perverse_parent.<locals>.Parent.__getattribute__Nr   r   r   r   r   Parent,  r   r1  c                      
     dS r   r   r   r   r   r   r   4  r   zZTestGreenlet.test_switch_to_dead_greenlet_with_unstarted_perverse_parent.<locals>.<lambda>r   )r   r:   r   r   r0   )r3   r1  r*  r+  r   r   r   ;test_switch_to_dead_greenlet_with_unstarted_perverse_parent+  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                      r2  r-  r3  r   r   r   r   r   B  r   zDTestGreenlet.test_switch_to_dead_greenlet_reparent.<locals>.<lambda>c                      r2  r   r3  r   r   r   r   r   C  r   r   r.  )r   r   r0   r   rG   )r3   r*  r+  r   r   r   r   %test_switch_to_dead_greenlet_reparent@  s   
z2TestGreenlet.test_switch_to_dead_greenlet_reparentN)F)-r   r   r   r4   r7   r@   rB   rF   rH   rO   rV   re   ri   rk   ro   rx   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  r4  r5  r   r   r   r   r'   +   sZ    ,
	8

/!7C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 sBw   Y  
  |d | t|jd d S )	Nc                      r   )Nc                   S   r   r   r   r   r   r   r   r   V  r   zQTestGreenletSetParentErrors.test_threaded_reparent.<locals>.run.<locals>.<lambda>r$   ra   r   r   r   r   r   r9   U  r   z?TestGreenletSetParentErrors.test_threaded_reparent.<locals>.runc                   S   r   r   r   r   r   r   r   blankZ  r   zATestGreenletSetParentErrors.test_threaded_reparent.<locals>.blankrY   ra   r$   z&parent cannot be on a different thread)r[   r|   r\   rb   rz   r   r   r:   r   r   ry   rc   r0   r=   r>   )r3   r9   r7  r   r$   r   r   r   r   test_threaded_reparentP  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 sEw   Y  | 
t|jd  d d = d S )Nc                     s"   t dd }  |  |   d S )Nc                   S   r   r   r   r   r   r   r   r   l  r   zYTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.worker.<locals>.<lambda>)r   r   r   rS   anotherr   r   r   k  s   
zGTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.workerrY   ra   r   )Zinitial_main_greenletsc                       r  )zKTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.convolutedc                       |dkr	 d | _ t| |S Nr9   r   r   r   r   r   r9  r   r   r        
z\TestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.convoluted.__getattribute__Nr   r   r9  r   r   r     r  r   c                   S   r   r   r   r   r   r   r   r     r   zITestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.<lambda>r   )r[   r\   rb   rc   r   Zmain_greenlets_before_testr   r:   r   r   r0   r=   r>   )r3   r   r}   r   r$   r   r   r9  r   test_unexpected_reparentingi  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 sGw   Y  | 	t
|jd	 W   |d  d d = d S   |d  d d = w )
Nc                     s4   t dd }  |  |     d d S )Nc                   S   r   r   r   r   r   r   r   r     r   zhTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.worker.<locals>.<lambda>ra   )r   r   r   ry   rz   rS   r:  Zkeep_main_aliveZswitched_to_greenletr   r   r     s
   
zVTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.workerc                       r  )zZTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.convolutedc                    r;  r<  r=  r   r9  r   r   r     r>  zkTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.convoluted.__getattribute__Nr   r   r9  r   r   r     r  r   rY   ra   c                   S   r   r   r   r   r   r   r   r     r   zXTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.<lambda>r   )r[   r|   r   r\   rb   rz   r:   r   r   r0   r=   r>   ry   rc   )r3   r   r   r}   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 s!w   Y  | t|jd d S )Nc                   S   r   r   r   r   r   r   r   r     r   zGTestGreenletSetParentErrors.test_cannot_delete_parent.<locals>.<lambda>can't delete attribute)	r   rD   r   r   r:   r;   r0   r=   r>   )r3   r   r   r   r   r   test_cannot_delete_parent  s   z5TestGreenletSetParentErrors.test_cannot_delete_parentc                 C   sH   |  t}t `W d    n1 sw   Y  | t|jd d S )NrB  r:   r;   r   r   r   r0   r=   r>   r3   r   r   r   r   !test_cannot_delete_parent_of_main  s   
z=TestGreenletSetParentErrors.test_cannot_delete_parent_of_mainc                 C   s   |  t j d S r   )rG   r   r   r   r6   r   r   r   !test_main_greenlet_parent_is_none  s   z=TestGreenletSetParentErrors.test_main_greenlet_parent_is_nonec                    sT   dd } fdd}t |} | || |   | || |  d S )Nc                   S   r   r   r   r   r   r   r   bg  s   zCTestGreenletSetParentErrors.test_set_parent_wrong_types.<locals>.bgc              	      sb   d d dfD ](}  t}|| _W d    n1 sw   Y   t|jdt|j  qd S )Nr   Z42z4GreenletChecker: Expected any type of greenlet, not )r:   r   r   r0   r=   r>   typer   )gletpr   r6   r   r   check  s   zFTestGreenletSetParentErrors.test_set_parent_wrong_types.<locals>.check)r   r   r   r   )r3   rH  rL  r$   r   r6   r   test_set_parent_wrong_types  s   


z7TestGreenletSetParentErrors.test_set_parent_wrong_typesc                 C   sR   t dd }| t}||_W d    n1 sw   Y  | t|jd d S )Nc                   S   r   r   r   r   r   r   r   r     r   z@TestGreenletSetParentErrors.test_trivial_cycle.<locals>.<lambda>cyclic parent chain)r   r:   r   r   r0   r=   r>   )r3   rJ  r   r   r   r   test_trivial_cycle  s
   z.TestGreenletSetParentErrors.test_trivial_cyclec                 C   sN   |  t}t t _W d    n1 sw   Y  | t|jd d S )Nz(cannot set the parent of a main greenletrD  rE  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 s+w   Y  | t|jd d S )Nc                   S   r   r   r   r   r   r   r   r     r   z?TestGreenletSetParentErrors.test_deeper_cycle.<locals>.<lambda>c                   S   r   r   r   r   r   r   r   r     r   c                   S   r   r   r   r   r   r   r   r     r   rN  )r   r   r:   r   r0   r=   r>   )r3   r%   rg   Zg3r   r   r   r   test_deeper_cycle  s   z-TestGreenletSetParentErrors.test_deeper_cycleN)r   r   r   r8  r?  rA  rC  rF  rG  rM  rO  rP  rQ  r   r   r   r   r6  O  s    )"r6  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)r3   gotsuffixr   r   r   assertEndsWith  s   zTestRepr.assertEndsWithc                 C   s   t t }| |d d S )N current active started main>)reprr   r   rV  r3   rr   r   r   test_main_while_running  s   z TestRepr.test_main_while_runningc                    s4   t    fdd}t |}| }| |d d S )Nc                      s   t  S r   )rX  r   r   r   r   r9     r   z-TestRepr.test_main_in_background.<locals>.run suspended active started main>)r   r   r   rV  )r3   r9   r$   rZ  r   r\  r   test_main_in_background  s
   z TestRepr.test_main_in_backgroundc                 C   s   t t }| |d d S )Nz	 pending>)rX  r   rV  rY  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 q-t	dD ]}| |jj t|j}| |d q9d 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   )rX  original_mainr   r   	main_gletr#  r6   r\  r   r   r9     rI   z3TestRepr.test_main_from_other_thread.<locals>.T.run)r   r   r   r`  r#  ra  r9   r   r\  r   r   T  s    rb  ra   r]  rW  r(   gMbP?z (thread exited) dead>)r   r   r[   r\   rb   rc   rV  r`  r#  r2   r   r   r   ra  rM   rX  )r3   rb  r}   r  rZ  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   r   r   r   r   r   r   r   r   4  r   z$TestRepr.test_dead.<locals>.<lambda>z dead>Z	suspendedstartedactive)r   r   rV  rX  ZassertNotInrE   r   r   r   	test_dead3  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   r   r   r   r   r   r   r   r   ?  r   z>TestRepr.test_formatting_produces_native_str.<locals>.<lambda>c                   S   r   r   r   r   r   r   r   r   @  r   z%sz%r)r   r   assertIsInstancer=   )r3   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   rV  r[  r^  r_  rc  rf  rh  r   r   r   r   rR    s    	#rR  c                   @   r   )TestMainGreenletc                 C   s4   dt t v s
J tt }dt |vsJ |S )Nr   )rX  r   r   rI  )r3   r}   r   r   r   _check_current_is_mainS  s   z'TestMainGreenlet._check_current_is_mainc                 C   s$   |   }td|fi }| | d S )Nsubclass)rj  rI  rC   )r3   Z	main_typerk  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   )rj  rg  r   r   r6   r   r   r   test_main_greenlet_is_greenlet`  s   z/TestMainGreenlet.test_main_greenlet_is_greenletN)r   r   r   rj  rl  rm  r   r   r   r   ri  O  s    ri  __main__)
__future__r   r   r   rl   r   r   r[   abcr   r   r    r	   Z	leakcheckr
   rq   r   r   r&   r'   r6  rR  ri  r   Zunittestr   r   r   r   r   <module>   s:    		      * ,U