o
    Pj                     @   sd  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 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G d d! d!eZe d"kre!  dS dS )#    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dS )	SomeErrorN__name__
__module____qualname__ r   r   Q/home/thesage/.local/lib/python3.10/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   crasher2      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edd Zdd Zdd Zdd Zdd ZeeoCe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d0d1 Zd2d3 Z d4d5 Z!d6d7 Z"e#ddd9d:Z$e#e%e&oe'j(d;d< d=kd>d?d@ Z)dAdB Z*e+dCdD Z,dEdF Z-dGdH Z.dIdJ Z/dKdL Z0dMdN Z1dOdP Z2eeoedQdRdS Z3dTdU Z4dVdW Z5dXdY Z6dZd[ Z7d\d] Z8d^d_ Z9d`da Z:dbdc Z;d;S )e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   f=   s   
z'TestGreenlet._do_simple_test.<locals>.fr            )r)   r   r   assertEquallistrange)selfr2   r*   r   r0   r   _do_simple_test:   s   


zTestGreenlet._do_simple_testc                 C      |    d S r#   )r:   r9   r   r   r   test_simpleI   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r9   r*   r$   r   r   r   (test_switch_no_run_raises_AttributeErrorL   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 r>   )r)   r@   rA   throwr   rB   rC   rD   rE   r   r   r   'test_throw_no_run_raises_AttributeErrorS   s
   z4TestGreenlet.test_throw_no_run_raises_AttributeErrorc                 C   s*   t d d}| | | |jt  d S )Nr'   )r)   assertIsNotNoneassertIsr   r   r   r9   r*   r   r   r   test_parent_equals_NoneZ   s   

z$TestGreenlet.test_parent_equals_Nonec                 C   s$   t d d}| | | |j d S )N)r?   )r)   rI   assertIsNoner?   rK   r   r   r   test_run_equals_None_      

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   r0   r   r   r2   g   s   
z)TestGreenlet.test_two_children.<locals>.fr   r3   r4   T   )r)   r   r6   lendead)r9   r2   r*   hr   r0   r   test_two_childrend   s   zTestGreenlet.test_two_childrenc                    s   g fdd  fdd}|  t|tsdnd t|}|  t|ts*dnd |  |  g d |  t|tsFd d S d d S )Nc                      s     d t j  d S )Nbr/   r   r0   r   r   r2   {   s   
z3TestGreenlet.test_two_recursive_children.<locals>.fc                     s(    d t } |    d d S )Nac)r   r)   r   r*   r2   r1   r   r   r*      s   
z3TestGreenlet.test_two_recursive_children.<locals>.gr3   r   )rW   rV   rX   )r6   r   getrefcountr	   r)   r   rK   r   rZ   r   test_two_recursive_childrenx   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)r:   r   r   )r9   successr   r   r2      s   z$TestGreenlet.test_threads.<locals>.fc                    s   g | ]}t j d qS )target	threadingThread).0ir2   r   r   
<listcomp>   s    z-TestGreenlet.test_threads.<locals>.<listcomp>
   )r8   startjoinr6   rR   )r9   thsthr   )r2   r9   r^   r   test_threads   s   
zTestGreenlet.test_threadsc                    s@   dd  dd d	 fdd	}t dD ]	}|  t  qd S )
Nc                 S   s0   dd }t  |}|  | | |  d S )Nc                   S   s   	 t  j  qr#   r   r   r   r   r   r   r   r   body   s   zPTestGreenlet.test_switching_many_threads.<locals>.creates_greenlet.<locals>.body)r   r   r   wait)	greenletswait_to_die_untilro   r*   r   r   r   creates_greenlet   s
   

zBTestGreenlet.test_switching_many_threads.<locals>.creates_greenletc              	   S   sT   |   | s(t| D ]}z	|js|  W q tjy!   Y qw | rd S d S r#   )rp   is_setr7   rS   r   r   error)rq   wait_to_check_until
quit_afterr*   r   r   r   switches_from_other_thread   s   zLTestGreenlet.test_switching_many_threads.<locals>.switches_from_other_thread   c                    s   g t  t   fddt| D } fddt| D }|| D ]}|  q)t| k rAtd t| k s6  |D ]}|d qG   |D ]}|d qUd S )Nc                    s   g | ]}t j fd qS r`   argsra   rd   _)rs   rq   greenlets_all_createdr   r   rg      s    zLTestGreenlet.test_switching_many_threads.<locals>.run_it.<locals>.<listcomp>c                    s    g | ]}t j fd qS rz   ra   r}   )all_threads_deadrq   r   rx   r   r   rg      s    g-C6?g      ?)	rb   Eventr8   ri   rR   timesleepsetrj   )thread_countcreators	switcherstrs   rx   )r   rq   r   r   run_it   s*   

z8TestGreenlet.test_switching_many_threads.<locals>.run_it   )ry   )r8   gccollect)r9   r   r~   r   r   r   test_switching_many_threads   s   	$
z(TestGreenlet.test_switching_many_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   r6   r@   r   )r9   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,   r6   )r9   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   r6   r   r   r   GreenletExit)r9   r!   r+   r   r   r   r   test_dealloc   s   

zTestGreenlet.test_deallocc                    sp   dd }t |}|  g  tj} fdd}|t_z~W |t_n|t_w | t d |  d jt d S )Nc                   S   s,   z
t  j  W d S  t jy   td w r#   )r   r   r   r   r   r   r   r   r   r   r?     s
   zHTestGreenlet.test_dealloc_catches_GreenletExit_throws_other.<locals>.runc                         |  d S r#   r   )
unraisableunraisable_eventsr   r   _capture     zMTestGreenlet.test_dealloc_catches_GreenletExit_throws_other.<locals>._capturer   r   )	r)   r   r   unraisablehookr6   rR   assertIsInstance	exc_valuer   )r9   r?   r*   old_hookr   r   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sGt	  | 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   r   r   r   rp   r+   /bg_glet_created_running_and_no_longer_ref_in_bgbg_should_be_clearfg_ref_releasedok_to_exit_bg_threadr!   somerefr   r   r2   4  s   




z1TestGreenlet.test_dealloc_other_thread.<locals>.fr_   rh   r   r.   )rb   r   rc   ri   rp   r6   rR   r   r   r   r   r   r   rj   )r9   r2   r   r   r   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   	_getframer6   f_backr   r   r   r   rf   r<   r   r   f1c  s   
z#TestGreenlet.test_frame.<locals>.f1r   )r)   r   
assertTruegr_frameassertFalser6   )r9   r   r*   framefrom_gr   r<   r   
test_frameb  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#   )r   r   r   xr   r   <lambda>t     
 z>TestGreenlet.test_thread_bug.<locals>.runner.<locals>.<lambda>r(   )r   r*   r   r   r   runners  s   z,TestGreenlet.test_thread_bug.<locals>.runner)g?r{   )g333333?rh   )rb   rc   ri   rj   )r9   r   t1t2r   r   r   test_thread_bugr  s   
zTestGreenlet.test_thread_bugc                    s.    fdd}t |jddd} |d d S )Nc                    s     | d   |d dS )Nr4   r3   *   )r6   rW   rV   r<   r   r   r?   ~  s   z,TestGreenlet.test_switch_kwargs.<locals>.runr4   r3   r   r   r)   r   r6   )r9   r?   r   r   r<   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   r3   r.   rn   r   r   r   r   r?     s   z6TestGreenlet.test_switch_kwargs_to_parent.<locals>.runr   r.   )r3   )r.   	   )r)   r6   r   )r9   r?   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*   rh   r)   r   rp   r   created_eventdata
done_eventr   r   r?        z7TestGreenlet.test_switch_to_another_thread.<locals>.runr_   rh   r*   )rb   r   rc   ri   rp   r@   r   ru   r   r   rj   clear)r9   r?   threadr   r   r   test_switch_to_another_thread  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 )Nfun)
ValueErrorr   r   r)   r   r6   )r   rT   r9   r   r   r2     s   z&TestGreenlet.test_exc_state.<locals>.fc                      s     t d d S )N)NNN)r6   r   r   r   r<   r   r   rT        z&TestGreenlet.test_exc_state.<locals>.hr(   )r9   r2   r   r   r   test_exc_state  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   r2     r   z*TestGreenlet.test_instance_dict.<locals>.fc                 S   s   | ` d S r#   __dict__rY   r   r   r   deldict     z0TestGreenlet.test_instance_dict.<locals>.deldictc                 S   s
   || _ d S r#   r   )r*   r   r   r   r   setdict     
z0TestGreenlet.test_instance_dict.<locals>.setdictr   r   )r)   r6   r   r   r   r@   	TypeError)r9   r2   r   r   r*   r   r   r   test_instance_dict  s   zTestGreenlet.test_instance_dictc                    s2   g   fdd}t |}|  |  dg d S )Nc                      s     tt d d S r>   )r   hasattrr   r   r   has_runr   r   func  s   z;TestGreenlet.test_running_greenlet_has_no_run.<locals>.funcFr   r9   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   )copyr@   r   r)   deepcopy)r9   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   r   r#   r   r   r   r   r   r     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   r   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   rJ   r   )r9   r   r*   r   r   r   test_parent_restored_on_kill  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@   rA   r   )r9   r+   r   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#   )	Exceptionr)   __getattribute__r9   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   rG   )r9   r   r*   r   r   r   test_throw_exception_not_lost  s   z*TestGreenlet.test_throw_exception_not_lostFc                    sT  g  dd  fdd}t j|d}|  |d ~r$|   | tj} d t	 W d    n1 s<w   Y  sTt
|j}| |dkpQd	|v  n	| t
|jd t d jd
r| t  d j  W d    n1 s|w   Y  st d jjd
r d jj  n|  d j ~d  d d = d| _d S )Nc                   S      t  j  d S r#   rn   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   r   rY   r   wait_for_cleanupr   r   r   creator  s   
zHTestGreenlet._do_test_throw_to_dead_thread_doesnt_crash.<locals>.creatorr_   rh   r   Bcannot switch to a different thread (which happens to have exited)zCannot switchr   T)rb   rc   ri   rj   wait_for_pending_cleanupsr@   r   ru   rG   r   rC   rD   r   r6   r   r   RuntimeErrorr   f_localsrM   expect_greenlet_leak)r9   r   r   r   r$   sr   r   r   *_do_test_throw_to_dead_thread_doesnt_crash   sF   



z7TestGreenlet._do_test_throw_to_dead_thread_doesnt_crashNr3   )r.   rh   z;does not leaks on Windows 3.10, but does on other platformsc                 C   r;   r#   r  r<   r   r   r   &test_throw_to_dead_thread_doesnt_crash:  s   z3TestGreenlet.test_throw_to_dead_thread_doesnt_crashc                 C   s   |  d d S r]   r  r<   r   r   r   +test_throw_to_dead_thread_doesnt_crash_waitB  r   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__countr<   r   r   r   r  H  s   

z@TestGreenlet.test_recursive_startup.<locals>.convoluted.__init__c                 S   s.   |dkr| j dkrd| _ | d t| |S )Nr?   r   r   +   )r  r   r)   r   r   r   r   r   r   K  s   
zHTestGreenlet.test_recursive_startup.<locals>.convoluted.__getattribute__c                 S   s   	 | j | qr#   r   r   r9   r   r   r   r   r?   P  s   z;TestGreenlet.test_recursive_startup.<locals>.convoluted.runN)r   r   r   r  r   r?   r   r   r   r   
convolutedG  s    r  r   r  T)r)   r6   r   r   )r9   r  r*   r   r   r   test_recursive_startupE  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acquirer<   lock1lock2r   r   __del__c  s   zCTestGreenlet.test_threaded_updatecurrent.<locals>.finalized.__del__Nr   r   r   r  r   r  r   r   	finalizedb  s    r  c                   S   r   r#   rn   r   r   r   r   deallocatorj  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   fthreadl  s   z9TestGreenlet.test_threaded_updatecurrent.<locals>.fthreadr   r_   rh   )rb   Lockr  objectr   r   r)   bombr   rc   ri   r  r6   rj   )r9   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   )r   	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    r&   zATestGreenlet.test_dealloc_switch_args_not_lost.<locals>.initiatorr   r   )r)   r   r   r6   r
  r   r  r   !test_dealloc_switch_args_not_lost  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   rW   kr   r   r   _apply  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__r<   r   r   r   r%    s   
z9TestGreenlet.test_tuple_subclass.<locals>.mytuple.__len__N)r   r   r   r%  r   r   r   r   mytuple  r   r&  r   )rW   c                      s    t  jj d S r#   rn   r   r#  r|   kwargsr   r   switchapply  s   z5TestGreenlet.test_tuple_subclass.<locals>.switchapply)r$  dictr)   r6   r   )r9   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AbstractSubclassr?   c                 S   r   r#   r   r<   r   r   r   r     r   z7TestGreenlet.test_abstract_subclasses.<locals>.<lambda>c                   @   r   )z:TestGreenlet.test_abstract_subclasses.<locals>.BadSubclassNr   r   r   r   r   BadSubclass  r   r-  c                   @   r   )z;TestGreenlet.test_abstract_subclasses.<locals>.GoodSubclassc                 S   r   r#   r   r<   r   r   r   r?     r&   z?TestGreenlet.test_abstract_subclasses.<locals>.GoodSubclass.runN)r   r   r   r?   r   r   r   r   GoodSubclass  r   r.  )r   r)   r   r@   r   )r9   r,  r-  r.  r   r   r   test_abstract_subclasses  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_threadr_   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  r<   r  r  recycledr   r   r    r2  z\TestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.gc_callback.__del__Nr  r   r3  r   r   gc_callback  s    r5  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callbackr<   r5  r   r   r    s   zYTestGreenlet.test_implicit_parent_with_threads.<locals>.attempt.<locals>.garbage.__init__N)r   r   r   r  r   r9  r   r   garbage      r:  r3   r   rh   T)rb   r  r  rc   ri   r  r8   r   r   rj   r)   r   r   r   r6   r   )	r1  r   r:  lr   currentr*   r~   lastNr9   )r5  r  r  r4  r   attempt  s>   

z?TestGreenlet.test_implicit_parent_with_threads.<locals>.attemptr5   )r   	isenabledget_thresholdr8   )r9   rA  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 ]}||  q(`t	r@
t|| d S t|| d S )	Nr   r   )r   c                   @   r   )zTTestGreenlet.test_issue_245_reference_counting_subclass_no_threads.<locals>.GreenletNr   r   r   r   r   Greenlet  r   rF  c                      s6   z	 j   W d S   y   j  Y d S w r#   )r   r   gletsr   r   r   r   r9   r   r   greenlet_main  s
   zYTestGreenlet.test_issue_245_reference_counting_subclass_no_threads.<locals>.greenlet_mainrh   )r   r   r   r)   r   r[   rG  r8   r   r   assertGreaterEqualr6   )r9   rF  initial_refsrI  r~   r   rH  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}	|
| |	  qB|D ]}
|
d qZd d =   |   | t | d S )Nr   )rc   )r   rE  c                   @   r   )zSTestGreenlet.test_issue_245_reference_counting_subclass_threads.<locals>.MyGreenletNr   r   r   r   r   
MyGreenlet?  r   rM  c                      s     j   d S r#   r	  r   rE  r   r   rI  E  s   zVTestGreenlet.test_issue_245_reference_counting_subclass_threads.<locals>.greenlet_mainc                    s8    } | |  ~|   d   d S )Nrh   )r   r   r   rp   )greenlet_running_eventminerM  r   rG  rI  ref_clearedr   r   thread_mainH  s   


zTTestGreenlet.test_issue_245_reference_counting_subclass_threads.<locals>.thread_main-   r0  r{   rh   )rb   rc   r   r   r   r)   r   r[   r   r8   r   ri   rp   r   r   r6   )r9   rc   r   rR  rK  thread_ready_eventsr   r~   eventr   r   r   rP  r   2test_issue_245_reference_counting_subclass_threads1  s0   	



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   )r9   rX  parent_never_startedrY  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   rY    r&   zRTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_works.<locals>.leaf)r   r^  r   )r9   r_  rZ  rY  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 r>   )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   ra  c                      
     dS r   r   r   r    r   r   r     r   zZTestGreenlet.test_switch_to_dead_greenlet_with_unstarted_perverse_parent.<locals>.<lambda>r   )r)   r@   r   r   r6   )r9   ra  rZ  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                      rb  r]  r   r   r    r   r   r     r   zDTestGreenlet.test_switch_to_dead_greenlet_reparent.<locals>.<lambda>c                      rb  r   r   r   r    r   r   r     r   r   r^  )r)   r   r6   r   rM   )r9   rZ  r[  r   r   r    r   %test_switch_to_dead_greenlet_reparent  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  r   zHTestGreenlet.test_can_access_f_back_of_suspended_greenlet.<locals>.outerc                      s     td d S r   )r   r   r   r   r   r   r   rf    r   zHTestGreenlet.test_can_access_f_back_of_suspended_greenlet.<locals>.innerc                   S   r   r#   r   r   r   r   r   r     r   zKTestGreenlet.test_can_access_f_back_of_suspended_greenlet.<locals>.<lambda>rf  rg  )r   r   r)   r   rI   r   r6   f_codeco_namer   rM   )r9   rg  r   	unrelatedr   rf  r   r   ,test_can_access_f_back_of_suspended_greenlet  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
}q$| |j	d d | |j
 | |d	d
 d S )Nr   )partialr   )_test_extension_cppc                    s.   | dkr|   | d  S t j S )Nr   r   )	test_callr   r   r   r   )vro  rn  recurser   r   rs    s   z@TestGreenlet.test_get_stack_with_nested_c_calls.<locals>.recurser5   rq  rh   i  )	functoolsrn   ro  r)   r   r   r8   r6   r   r   rM   )r9   grr   re   r   rr  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   re  r   r   rg    r   z6TestGreenlet.test_frames_always_exposed.<locals>.outerc                    r   r#   r   )r   rh  r   r   rf    r   z6TestGreenlet.test_frames_always_exposed.<locals>.innerc                   S   r   r#   r   r   r   r   r   r     r   z9TestGreenlet.test_frames_always_exposed.<locals>.<lambda>rg  )	r   r   r)   r   r6   ri  rj  rM   r   )r9   rg  rv  r   rk  r   rl  r   test_frames_always_exposed  s   z'TestGreenlet.test_frames_always_exposedc                    sR   fdd fddfdd}fdd}t  | t  |   d S )	Nc                           | S r#   rx  r   )r   r   r   k1     zITestGreenlet.test_switching_holding_critical_section_no_crash.<locals>.k1c                    rz  r#   rx  r   r   r   r   k2  r|  zITestGreenlet.test_switching_holding_critical_section_no_crash.<locals>.k2c                      s   t dgd    d S )Nr   key)sortedr   r   )r   r{  r   r   g1_body  s   zNTestGreenlet.test_switching_holding_critical_section_no_crash.<locals>.g1_bodyc                     sJ   t dg d ttddd} |   W d    d S 1 sw   Y  d S )Nr   r~  rtzutf-8)encoding)r  open__file__readrf   )r}  r   r   g2_body"  s   
"zNTestGreenlet.test_switching_holding_critical_section_no_crash.<locals>.g2_body)r   r   )r9   r  r  r   )r+   r   r{  r}  r   0test_switching_holding_critical_section_no_crash  s   

z=TestGreenlet.test_switching_holding_critical_section_no_crash)F)<r   r   r   r:   r=   rF   rH   rL   rN   rU   r\   rm   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   r  r  r!  r+  r/  rD  rL  rV  r\  r`  rc  rd  rm  rw  ry  r  r   r   r   r   r-   8   s~    
?
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 sBw   Y  
  |d | t|jd d S )	Nc                      r   )Nc                   S   r   r#   r   r   r   r   r   r   6  r   zQTestGreenletSetParentErrors.test_threaded_reparent.<locals>.run.<locals>.<lambda>r*   rh   r   r   r   r   r   r?   5  r   z?TestGreenletSetParentErrors.test_threaded_reparent.<locals>.runc                   S   r   r#   rn   r   r   r   r   blank:  r   zATestGreenletSetParentErrors.test_threaded_reparent.<locals>.blankr_   rh   r*   z&parent cannot be on a different thread)rb   r   rc   ri   rp   r)   r   r@   r   r   r   rj   r6   rC   rD   )r9   r?   r  r   r*   r$   r   r   r   test_threaded_reparent0  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   rY   anotherr   r   r   K  s   
zGTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.workerr_   rh   r   )initial_main_greenletsc                       r6  )zKTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.convolutedc                       |dkr	 d | _ t| |S Nr?   r   r   r)   r   r   r  r   r   r   g     
z\TestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.convoluted.__getattribute__Nr   r   r  r   r   r  f  r;  r  c                   S   r   r#   r   r   r   r   r   r   k  r   zITestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.<lambda>r   )rb   rc   ri   rj   r   main_greenlets_before_testr)   r@   r   ru   r   r6   rC   rD   )r9   r   r   r  r*   r$   r   r  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 zL|dd }| tj}|	  W d    n1 sGw   Y  | 
d	t|j | 
d
t|j | 
dt|j 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   y  r   zhTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.worker.<locals>.<lambda>rh   )r)   r   r   r   rp   rY   r  keep_main_aliveswitched_to_greenletr   r   r   x  s
   
zVTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.workerc                       r6  )zZTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.convolutedc                    r  r  r  r   r  r   r   r     r  zkTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.convoluted.__getattribute__Nr   r   r  r   r   r  ~  r;  r  r_   rh   c                   S   r   r#   r   r   r   r   r   r     r   zXTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.<lambda>z#Cannot switch to a different threadExpectedCurrent)rb   r   r)   rc   ri   rp   r@   r   ru   r   rB   rC   rD   r   rj   )r9   r   r  r   r*   r$   r   r  r   *test_unexpected_reparenting_thread_runningr  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)   rJ   r   r   r   r@   rA   r6   rC   rD   )r9   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 )Nr  r@   rA   r   r   r   r6   rC   rD   r9   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#   )rM   r   r   r   r<   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#   rn   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   42z4GreenletChecker: Expected any type of greenlet, not )r@   r   r   r6   rC   rD   typer   )gletpr$   r<   r   r   check  s   zFTestGreenletSetParentErrors.test_set_parent_wrong_types.<locals>.check)r)   r   r   r   )r9   r  r  r*   r   r<   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   r6   rC   rD   )r9   r  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 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 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   r  )r)   r   r@   r   r6   rC   rD   )r9   r+   r   g3r$   r   r   r   test_deeper_cycle  s   z-TestGreenletSetParentErrors.test_deeper_cycleN)r   r   r   r  r  r  r  r  r  r  r  r  r  r   r   r   r   r  /  s    )$r  c                   @   sP   e Zd ZeedsdddZdd Zdd Zd	d
 Zdd Z	dd Z
dd ZdS )TestReprassertEndsWithNc                 C   s   |  |||||f d S r#   )r   endswith)r9   r  suffixmsgr   r   r   r    s   zTestRepr.assertEndsWithc                 C   s   t t }| |d d S )N current active started main>)reprr   r   r  r9   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#   )r  r   rh  r   r   r?     r   z-TestRepr.test_main_in_background.<locals>.run suspended active started main>)r   r   r)   r   r  )r9   r?   r*   r  r   rh  r   test_main_in_background  s
   z TestRepr.test_main_in_backgroundc                 C   s   t t }| |d d S )Nz	 pending>)r  r)   r  r  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#   )r  original_mainr   r   	main_gletrR  r<   rh  r   r   r?     rO   z3TestRepr.test_main_from_other_thread.<locals>.T.run)r   r   r   r  rR  r  r?   r   rh  r   r   T  s    r  rh   r  r  r.   gMbP?z (thread exited) dead>)r   r   rb   rc   ri   rj   r  r  rR  r8   r   r   r   r  rS   r  )r9   r  r   r~   r  r   rh  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     r   z$TestRepr.test_dead.<locals>.<lambda>z dead>	suspendedstartedactive)r)   r   r  r  assertNotInrK   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   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   r   r   rC   )r9   g_deadg_not_startedg_curr*   r   r   r   #test_formatting_produces_native_str  s   z,TestRepr.test_formatting_produces_native_strr#   )r   r   r   r   r   r  r  r  r  r  r  r  r   r   r   r   r    s    

	#r  c                   @   s,   e Zd Zd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sJ |S )Nr   )r  r   r   r  )r9   r   r   r   r   _check_current_is_main6  s   z'TestMainGreenlet._check_current_is_mainc                 C   s$   |   }td|fi }| | d S )Nsubclass)r  r  rI   )r9   	main_typer  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#   )r  r   r   r   r)   r<   r   r   r   test_main_greenlet_is_greenletC  s   z/TestMainGreenlet.test_main_greenlet_is_greenletc                    sZ    fdd}t  |    t t d W d    d S 1 s&w   Y  d S )Nc                      s:    t t d W d    d S 1 sw   Y  d S r>   )r@   rA   getattrr   r*   r9   r   r   do_itH  s   "z1TestMainGreenlet.test_greenlet_run.<locals>.do_itr?   )r   r   r@   rA   r  )r9   r  r   r  r   test_greenlet_runG  s   
"z"TestMainGreenlet.test_greenlet_runN)r   r   r   r  r  r  r  r   r   r   r   r  2  s
    r  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   sT   dd }t j|}d|_| td |  W d    d S 1 s#w   Y  d S )Nc                   S   s   t d)NzNever get here)AssertionErrorr   r   r   r   r   W  r   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_initialstubV  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 s=w   Y  d|_|  |  g d d S )	Nc                      s>     d t j    d t j    d d S )Nr   r3   r.   r/   r   runsr   r   r   d  s
   

zDTestBrokenGreenlets.test_failed_to_switch_into_running.<locals>.funcr   r3   Tz0Failed to switch stacks into a running greenlet.F)r   r3   r.   )r   r  r  r   r6   r  r  r  r   r   r  r   "test_failed_to_switch_into_runningb  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)assertScriptRaisesrB   output
returncode,get_expected_returncodes_for_aborted_processr9   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_scriptrB   r9   r  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   )	exitcodesr   zpositional arguments)r  rB   r  r  r   r   r   %test_reentrant_switch_three_greenlets  s   z9TestBrokenGreenlets.test_reentrant_switch_three_greenletsc                 C      |  d}| d| d S )Nzfail_switch_three_greenlets2.pya  RESULTS: [('trace', 'switch'), ('trace', 'switch'), ('g2 arg', 'g2 from tracefunc'), ('trace', 'switch'), ('main g1', 'from g2_run'), ('trace', 'switch'), ('g1 arg', 'g1 from main'), ('trace', 'switch'), ('main g2', 'from g1_run'), ('trace', 'switch'), ('g1 from parent', 'g1 from main 2'), ('trace', 'switch'), ('main g1.2', 'g1 done'), ('trace', 'switch'), ('g2 from parent', ()), ('trace', 'switch'), ('main g2.2', 'g2 done')]r  r  r   r   r   &test_reentrant_switch_three_greenlets2  s
   
z:TestBrokenGreenlets.test_reentrant_switch_three_greenlets2c                 C   r  )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)]r  r  r   r   r   4test_reentrant_switch_GreenletAlreadyStartedInPython  s
   
zHTestBrokenGreenlets.test_reentrant_switch_GreenletAlreadyStartedInPythonc                 C   r  )Nzfail_clearing_run_switches.pyzRESULTS [('G.__getattribute__', 'run'), ('RunCallable', '__del__'), ('main: g.switch()', 'from RunCallable'), ('run_func', 'enter')]r  r  r   r   r   *test_reentrant_switch_run_callable_has_del  s
   
z>TestBrokenGreenlets.test_reentrant_switch_run_callable_has_delN)r   r   r   r  r  r  r  r  r  r  r  r   r   r   r   r  R  s    
	r  c                   @   s&   e Zd Zeeedddd ZdS )
TestModule_is_gil_enabledz,Needs 3.13 and above for sys._is_gil_enabledc                 C   s(   t r| t  d S | t  d S r#   )r   r   r   r  r   r<   r   r   r   test_no_gil_on_free_threaded  s   z'TestModule.test_no_gil_on_free_threadedN)r   r   r   r  
skipUnlessr   r   r  r   r   r   r   r    s
    r  __main__)"r   r   r   rb   r  abcr   r   r   r)   ru  r   r   r   r	   r
   r   	leakcheckr   r   r   r   r   r   r"   r,   r-   r  r  r  r  r  r   r   r   r   r   r   <module>   sN    		       ~ .V m
