o
    no™i7½  ã                   @   s4  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G dd„ deƒZedkr˜e ¡  dS dS )é    N)ÚABCMeta)Úabstractmethod)Úgreenleté   )ÚTestCase)ÚRUNNING_ON_MANYLINUX)ÚPY313)ÚPY314)ÚRUNNING_ON_FREETHREAD_BUILD)Úfails_leakcheckc                   @   ó   e Zd ZdS )Ú	SomeErrorN©Ú__name__Ú
__module__Ú__qualname__© r   r   úP/home/ubuntu/.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   Úcrasher.   ó   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o=e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o¨e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   ó   |   ¡  d S r   )r6   ©r5   r   r   r   Útest_simpleE   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©r5   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 sw   Y  |  dt|jƒ¡ d S r:   )r%   r<   r=   Úthrowr   r>   r?   r@   rA   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   ©r5   r&   r   r   r   Útest_parent_equals_NoneV   s   

z$TestGreenlet.test_parent_equals_Nonec                 C   s$   t d d}|  |¡ |  |j¡ d S )N)r;   )r%   rE   ÚassertIsNoner;   rG   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   r,   r   r   r.   c   s   
z)TestGreenlet.test_two_children.<locals>.fr   r/   r0   Té   )r%   r   r2   ÚlenÚdead)r5   r.   r&   Úhr   r,   r   Útest_two_children`   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   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   )rS   rR   rT   )r2   r   Úgetrefcountr	   r%   r   rG   r   rV   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)r6   r   r   )r5   Ú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>é
   )r4   ÚstartÚjoinr2   rN   )r5   ÚthsÚthr   )r.   r5   rZ   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   r2   r<   r   )r5   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(   r2   )r5   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   r2   ÚgcÚcollectr   ÚGreenletExit)r5   r   r'   rj   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_w | ¡ }|  d|¡ |  d|¡ |  d|¡ d S )Nc                   S   s,   z
t  ¡ j ¡  W d S  t jy   td ‚w r   )r   r   r   r   rq   r   r   r   r   r   r;   É   s
   ÿzHTestGreenlet.test_dealloc_catches_GreenletExit_throws_other.<locals>.runr   )ÚStringIOÚ	ExceptionÚignoredr   )r%   r   r   ÚstderrÚiors   Úgetvaluer>   )r5   r;   r&   Ú	oldstderrrs   rv   Ú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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   ro   rp   ÚsetÚwait)r'   ©Ú/bg_glet_created_running_and_no_longer_ref_in_bgÚbg_should_be_clearÚfg_ref_releasedÚok_to_exit_bg_threadr   Úsomerefr   r   r.   ó   s   




z1TestGreenlet.test_dealloc_other_thread.<locals>.fr[   rc   r   r*   )r]   ÚEventr^   rd   r}   r2   rN   r
   ro   rp   r|   r   rq   re   )r5   r.   Útr   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   Ú	_getframer2   Úf_backr   r   r   r   ra   r8   r   r   Úf1"  s   
z#TestGreenlet.test_frame.<locals>.f1r‡   )r%   r   Ú
assertTrueÚgr_frameÚassertFalser2   )r5   rŠ   r&   ÚframeÚfrom_gr   r8   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  ó   
 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š™™™™™É?©r\   Úargs)g333333Ó?rc   )r]   r^   rd   re   )r5   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 )Nr0   r/   é*   )r2   ©rS   rR   r8   r   r   r;   =  s   z,TestGreenlet.test_switch_kwargs.<locals>.runr0   r/   rž   r   ©r%   r   r2   )r5   r;   r”   r   r8   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   r;   E  s   z6TestGreenlet.test_switch_kwargs_to_parent.<locals>.runr”   r*   )r/   )r*   é	   )r%   r2   r   )r5   r;   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 s8w   Y  ˆ 	¡  | 
d¡ ˆ ¡  d S )Nc                      ó&   t dd„ ƒˆd< ˆ  ¡  ˆ d¡ d S )Nc                   S   ó   d S r   r   r   r   r   r   r•   T  ó    zITestGreenlet.test_switch_to_another_thread.<locals>.run.<locals>.<lambda>r&   rc   ©r%   r|   r}   r   ©Úcreated_eventÚdataÚ
done_eventr   r   r;   S  ó   z7TestGreenlet.test_switch_to_another_thread.<locals>.runr[   rc   r&   )r]   r„   r^   rd   r}   r<   r   Úerrorr   r|   re   Úclear)r5   r;   Ú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                     s:   zt dƒ‚   t ¡ } tˆ ƒ ¡  ˆ | t ¡ ¡ Y d S )NÚfun)Ú
ValueErrorr   r   r%   r   r2   )r   ©rP   r5   r   r   r.   b  s   z&TestGreenlet.test_exc_state.<locals>.fc                      s   ˆ   t ¡ d¡ d S )N)NNN)r2   r   r   r   r8   r   r   rP   j  ó   z&TestGreenlet.test_exc_state.<locals>.hr$   )r5   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  ó   z*TestGreenlet.test_instance_dict.<locals>.fc                 S   s   | ` d S r   ©Ú__dict__rU   r   r   r   Údeldictr  ó   z0TestGreenlet.test_instance_dict.<locals>.deldictc                 S   s
   || _ d S r   r¹   )r&   rk   r   r   r   Úsetdictt  ó   
z0TestGreenlet.test_instance_dict.<locals>.setdictr   r·   )r%   r2   rº   r   r·   r<   Ú	TypeError)r5   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 r:   )r   Úhasattrr   r   r   ©Úhas_runr   r   Úfunc‚  s   ÿz;TestGreenlet.test_running_greenlet_has_no_run.<locals>.funcFrŸ   ©r5   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)r5   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   rq   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‹   rF   r   )r5   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<   r=   r   )r5   r'   rj   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   )rt   r%   Ú__getattribute__©r5   Ú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   rC   )r5   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  ˆs•tˆ d jjd
ƒr”ˆ 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   r   rU   ©rÍ   Úwait_for_cleanuprÎ   r   r   ÚcreatorÅ  s   
ýzHTestGreenlet._do_test_throw_to_dead_thread_doesnt_crash.<locals>.creatorr[   rc   r   úBcannot switch to a different thread (which happens to have exited)zCannot switchr®   T)r]   r^   rd   re   Úwait_for_pending_cleanupsr<   r   r­   rC   r   r?   r@   r‹   r2   rÁ   rŒ   ÚRuntimeErrorr®   Úf_localsrI   Úexpect_greenlet_leak)r5   rÜ   rÝ   r…   r    Úsr   rÛ   r   Ú*_do_test_throw_to_dead_thread_doesnt_crash¿  sF   
ÿ
þþÿ€

z7TestGreenlet._do_test_throw_to_dead_thread_doesnt_crashc                 C   r7   r   ©rä   r8   r   r   r   Ú&test_throw_to_dead_thread_doesnt_crashù  s   z3TestGreenlet.test_throw_to_dead_thread_doesnt_crashc                 C   s   |   d¡ d S rY   rå   r8   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__Úcountr8   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 )Nr;   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   ©r5   rk   r   r   r   r;     s   ÿz;TestGreenlet.test_recursive_startup.<locals>.convoluted.runN)r   r   r   ré   rÒ   r;   r   r   r   r   Ú
convoluted  s    rî   r   rë   T)r%   r2   r   râ   )r5   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Úacquirer8   ©Ú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   r[   rc   )r]   ÚLockrò   Úobjectr   r   r%   Úbombr   r^   rd   rñ   r2   re   )r5   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   )rk   ©Ú	initiatorr   rÎ   r   r   rÎ   G  s   "z>TestGreenlet.test_dealloc_switch_args_not_lost.<locals>.workerc                   S   ó   dS r¶   r   r   r   r   r   r  T  r"   zATestGreenlet.test_dealloc_switch_args_not_lost.<locals>.initiatorr   r   )r%   r   r‹   r2   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Ä   rS   Úkr   r   r   Ú_applym  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__r8   r   r   r   r  q  s   
z9TestGreenlet.test_tuple_subclass.<locals>.mytuple.__len__N)r   r   r   r  r   r   r   r   Úmytuplep  r×   r	  r   )rS   c                      s   ˆ t  ¡ jjˆˆƒ d S r   r¡   r   ©r  r™   Úkwargsr   r   Úswitchapplyv  s   z5TestGreenlet.test_tuple_subclass.<locals>.switchapply)r  Údictr%   r2   r   )r5   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   r8   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   r8   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¿   )r5   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|¡ q‚d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ð   r8   ©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Úcallbackr8   ©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   rc   T)r]   rý   rò   r^   rd   rþ   r4   r   r   re   r%   r   ro   rp   r2   r   )	r  r…   r  Úlr”   Úcurrentr&   Ú_Úlast©ÚNr5   )r  rô   rõ   r  r   Úattempt‘  s>   

z?TestGreenlet.test_implicit_parent_with_threads.<locals>.attemptr1   )ro   Ú	isenabledÚget_thresholdr4   )r5   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 ]}||ƒ ¡  q(ˆ`t	r@ˆ 
t |¡|¡ d S ˆ t |¡|¡ d S )	Nr   ©r   )rq   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   ©rq   r   r5   r   r   Úgreenlet_mainÔ  s
   ÿzYTestGreenlet.test_issue_245_reference_counting_subclass_no_threads.<locals>.greenlet_mainrc   )r   r   rq   r%   r   rW   r+  r4   r   r
   ÚassertGreaterEqualr2   )r5   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}	| 
|¡ |	 ¡  qB|D ]}
|
 d¡ qZˆ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   r1  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 )Nrc   )r   r   r|   r}   )Úgreenlet_running_eventÚmine©r1  r   r+  r-  Úref_clearedr   r   Úthread_main  s   


zTTestGreenlet.test_issue_245_reference_counting_subclass_threads.<locals>.thread_mainé-   r  r˜   rc   )r]   r^   r„   r   r   r%   r   rW   r
   r4   r   rd   r}   r|   rß   r2   )r5   r^   r„   r6  r/  Úthread_ready_eventsÚthread_countr!  Úeventr¯   r«   r   r4  r   Ú2test_issue_245_reference_counting_subclass_threadsì  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   )r5   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  r¼   zUTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_works.<locals>.one_argc                   S   r  r¶   r   r   r   r   r   r>  N  r"   zRTestGreenlet.test_falling_off_end_switches_to_unstarted_parent_works.<locals>.leaf)r   rC  rŸ   )r5   rD  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 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Ò   X  s   ÿziTestGreenlet.test_switch_to_dead_greenlet_with_unstarted_perverse_parent.<locals>.Parent.__getattribute__NrÕ   r   r   r   r   ÚParentW  r×   rF  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   r2   )r5   rF  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                      rG  rB  rH  r   r   r   r   r•   m  r–   zDTestGreenlet.test_switch_to_dead_greenlet_reparent.<locals>.<lambda>c                      rG  r¶   rH  r   r   r   r   r•   n  r–   r   rC  )r%   r   r2   r   rI   )r5   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  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   rL  ‚  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>rL  rM  )r   r   r%   r   rE   rŒ   r2   Úf_codeÚco_namer‰   rI   )r5   rM  rË   Ú	unrelatedr   ©rL  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
}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   )rz   ©rU  rT  Úrecurser   r   rX  š  s   z@TestGreenlet.test_get_stack_with_nested_c_calls.<locals>.recurser1   rz   rc   i°  )Ú	functoolsrT  Ú rU  r%   r   rŒ   r4   r2   rá   r‰   rI   )r5   ÚgrrŽ   r`   r   rW  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   rK  r   r   rM  ¯  rÚ   z6TestGreenlet.test_frames_always_exposed.<locals>.outerc                    s   ˆ   | ¡ d S r   )r   )rŽ   rN  r   r   rL  ²  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>rM  )	r   r   r%   r   r2   rO  rP  rI   r‰   )r5   rM  r[  rŽ   rQ  r   rR  r   Útest_frames_always_exposed©  s   z'TestGreenlet.test_frames_always_exposedN)F)4r   r   r   r6   r9   rB   rD   rH   rJ   rQ   rX   rh   rl   rn   rr   r{   ÚunittestÚskipIfr   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  r;  rA  rE  rI  rJ  rS  r\  r]  r   r   r   r   r)   4   sp    þ
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•   É  r¦   zQTestGreenletSetParentErrors.test_threaded_reparent.<locals>.run.<locals>.<lambda>r&   rc   r§   r   r¨   r   r   r;   È  r¬   z?TestGreenletSetParentErrors.test_threaded_reparent.<locals>.runc                   S   rÙ   r   r¡   r   r   r   r   ÚblankÍ  rÚ   zATestGreenletSetParentErrors.test_threaded_reparent.<locals>.blankr[   rc   r&   z&parent cannot be on a different thread)r]   r„   r^   rd   r}   r%   r   r<   r²   r   r|   re   r2   r?   r@   )r5   r;   ra  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 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•   ß  r¦   zYTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.worker.<locals>.<lambda>)r%   r   r   rU   ©Úanotherr   r   rÎ   Þ  s   
zGTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.workerr[   rc   r   )Úinitial_main_greenletsc                       r  )zKTestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.convolutedc                    ó   |dkr	ˆ d | _ t | |¡S ©Nr;   r   ©r   r%   rÒ   rÓ   rc  r   r   rÒ   ú  ó   
z\TestGreenletSetParentErrors.test_unexpected_reparenting.<locals>.convoluted.__getattribute__NrÕ   r   rc  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^   rd   re   rß   Úmain_greenlets_before_testr%   r<   r   r­   r   r2   r?   r@   )r5   rÎ   r…   rî   r&   r    r   rc  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¡ 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•     r¦   zhTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.worker.<locals>.<lambda>rc   )r%   r   r   r|   r}   rU   ©rd  Úkeep_main_aliveÚswitched_to_greenletr   r   rÎ     s
   
zVTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.workerc                       r  )zZTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.convolutedc                    rf  rg  rh  rÓ   rc  r   r   rÒ     ri  zkTestGreenletSetParentErrors.test_unexpected_reparenting_thread_running.<locals>.convoluted.__getattribute__NrÕ   r   rc  r   r   rî     r  rî   r[   rc   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)r]   r„   r%   r^   rd   r}   r<   r   r­   r   r>   r?   r@   r|   re   )r5   rÎ   rî   r…   r&   r    r   rl  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%   rF   r   r   r   r<   r=   r2   r?   r@   )r5   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 )Nrr  ©r<   r=   r   r   r   r2   r?   r@   ©r5   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   )rI   r   r   r   r8   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   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   Ú42z4GreenletChecker: Expected any type of greenlet, not )r<   r¿   r   r2   r?   r@   Útyper   )ÚgletÚpr    r8   r   r   Úcheck@  s   ÿþüzFTestGreenletSetParentErrors.test_set_parent_wrong_types.<locals>.check)r%   r   r   r‹   )r5   rx  r}  r&   r   r8   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•   X  r¦   z@TestGreenletSetParentErrors.test_trivial_cycle.<locals>.<lambda>úcyclic parent chain)r%   r<   r²   r   r2   r?   r@   )r5   r{  r    r   r   r   Útest_trivial_cycleW  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 greenletrt  ru  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•   d  r¦   z?TestGreenletSetParentErrors.test_deeper_cycle.<locals>.<lambda>c                   S   r¥   r   r   r   r   r   r   r•   e  r¦   c                   S   r¥   r   r   r   r   r   r   r•   f  r¦   r  )r%   r   r<   r²   r2   r?   r@   )r5   r'   rj   Úg3r    r   r   r   Útest_deeper_cyclec  s   ÿz-TestGreenletSetParentErrors.test_deeper_cycleN)r   r   r   rb  rk  rq  rs  rv  rw  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)r5   rã   ÚsuffixÚmsgr   r   r   r…  r  s   zTestRepr.assertEndsWithc                 C   s   t t ¡ ƒ}|  |d¡ d S )Nú current active started main>)Úreprr   r   r…  ©r5   Úrr   r   r   Útest_main_while_runningu  s   z TestRepr.test_main_while_runningc                    s4   t  ¡ ‰ ‡ fdd„}t|ƒ}| ¡ }|  |d¡ d S )Nc                      s   t ˆ ƒS r   )rŠ  r   rN  r   r   r;   {  r¼   z-TestRepr.test_main_in_background.<locals>.runú suspended active started main>)r   r   r%   r   r…  )r5   r;   r&   rŒ  r   rN  r   Útest_main_in_backgroundy  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_gletr6  r8   rN  r   r   r;   Œ  rK   z3TestRepr.test_main_from_other_thread.<locals>.T.run)r   r   r   r‘  r6  r’  r;   r   rN  r   r   ÚT‰  s    r“  rc   rŽ  r‰  r*   gü©ñÒMbP?z (thread exited) dead>)r   r   r]   r^   rd   re   r…  r‘  r6  r4   r‘   r’   r‹   r’  rO   rŠ  )r5   r“  r…   r!  rŒ  r   rN  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Š  ÚassertNotInrG   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   ÚassertIsInstancer?   )r5   Ú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„  o  s    

	#r„  c                   @   rè   )ÚTestMainGreenletc                 C   s4   dt t ¡ ƒv s
J ‚tt ¡ ƒ}dt |ƒvsJ ‚|S )NrÌ   )rŠ  r   r   rz  )r5   r…   r   r   r   Ú_check_current_is_mainÉ  s   z'TestMainGreenlet._check_current_is_mainc                 C   s$   |   ¡ }td|fi ƒ}|  |¡ d S )NÚsubclass)r   rz  rE   )r5   Ú	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%   r8   r   r   r   Útest_main_greenlet_is_greenletÖ  s   z/TestMainGreenlet.test_main_greenlet_is_greenletN)r   r   r   r   r£  r¤  r   r   r   r   rŸ  Å  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Ä   á  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_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 s=w   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   r§  r¨  r   r2   r©  rª  r«  rÅ   r   r­  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Ú
returncodeÚ,get_expected_returncodes_for_aborted_process©r5   Ú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>   ©r5   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°  r>   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_GreenletAlreadyStartedInPython3  s
   
úzHTestBrokenGreenlets.test_reentrant_switch_GreenletAlreadyStartedInPythonc                 C   r¾  )Nzfail_clearing_run_switches.pyz„RESULTS [('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¥  Ü  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‹   r8   r   r   r   Útest_no_gil_on_free_threadedK  s   z'TestModule.test_no_gil_on_free_threadedN)r   r   r   r^  Ú
skipUnlessrÁ   r   rÄ  r   r   r   r   rÂ  I  s
    ÿrÂ  Ú__main__)ro   r   r‘   r]   r^  Úabcr   r   r   r%   rZ  r   r   r   r	   r
   Ú	leakcheckr   rt   r   r   r(   r)   r`  r„  rŸ  r¥  rÂ  r   rÌ   r   r   r   r   Ú<module>   sF    		        .Vmÿ