
    h2E                        d Z ddlmZmZmZ ddlZ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mZ ddlmZ dd	lmZ e
j$                  sJ  G d
 de      Z G d de      Zedk(  r ed      j1                          yy)z)
Testing scenarios that may have leaked.
    )print_functionabsolute_importdivisionN   )TestCase)PY314)fails_leakcheck)ignores_leakcheck)RUNNING_ON_MANYLINUXc                   <    e Zd Z e       Zd Zd Zd Zed        Z	y)HasFinalizerTracksInstancesc                     t        j                  |      | _        | j                  j	                  t        |              y N)sysinternmsgEXTANT_INSTANCESaddid)selfr   s     BD:\EasyAligner\venv\Lib\site-packages\greenlet/tests/test_leaks.py__init__z$HasFinalizerTracksInstances.__init__   s)    ::c?!!"T(+    c                 L    | j                   j                  t        |              y r   )r   remover   r   s    r   __del__z#HasFinalizerTracksInstances.__del__   s    $$RX.r   c                 6    dt        |       | j                  fz  S )Nz(<HasFinalizerTracksInstances at 0x%x %r>)r   r   r   s    r   __repr__z$HasFinalizerTracksInstances.__repr__!   s!    9tHdhh=
 
 	
r   c                 8    | j                   j                          y r   )r   clear)clss    r   resetz!HasFinalizerTracksInstances.reset%   s    ""$r   N)
__name__
__module____qualname__setr   r   r   r   classmethodr#    r   r   r   r      s-    u,/
 % %r   r   c                       e Zd Zd Zd Zed        Zd Zd Zd Z		 	 ddZ
d Zd	 Zed
        Zed        Zed        ZdZd Zed        ZddZed        Zed        Zy)	TestLeaksc                     d}t        j                  |      }t        j                  d       }t        d      D ]  } |j                  |   | j                  t        j                  |      |       y )N)abcc                  X     t        j                         j                  j                  |  S r   greenlet
getcurrentparentswitch)argss    r   <lambda>z)TestLeaks.test_arg_refs.<locals>.<lambda>1   s!    =(--/66==tDr   d   )r   getrefcountr2   ranger5   assertEqual)r   r6   refcount_beforeg_s        r   test_arg_refszTestLeaks.test_arg_refs,   s]    //$/DFsAAHHdO .@r   c                 6   i }| j                  t        j                  |      t        sdnd       t	        j                  d       }t        d      D ]  } |j                  di |  | j                  t        j                  |      t        sd       y d       y )N   r   c                  ^     t        j                         j                  j                  di | S )Nr)   r1   )gkwargss    r   r7   z+TestLeaks.test_kwarg_refs.<locals>.<lambda>;   s$    Ah113::AALGLr   r8   r)   )r;   r   r9   r   r2   r:   r5   )r   kwargsr=   r>   s       r   test_kwarg_refszTestLeaks.test_kwarg_refs6   sy    05!aHLNsAAHHv  	05!HaHr   c                      d } t        j                  |       }|j                          t        j                  d       |j                  d       y )Nc                  .    t        j                  d       y )NMbP?)timesleepr)   r   r   workerz+TestLeaks.__recycle_threads.<locals>.workerK   s    JJur   targetrH   
   )	threadingThreadstartrI   rJ   join)rK   ts     r   __recycle_threadszTestLeaks.__recycle_threadsC   s7    	F+		

5	r
r   c                    g fd}t        d      D ]:  }t        j                  |      }|j                          |j	                  d       ~< t        j                          | j                          t        j                          t        j                          t        j                          D ]  }| j                   |               y )Nc                  r     j                  t        j                  t        j                                      y r   )appendweakrefrefr2   r3   )ggs   r   rK   z,TestLeaks.test_threaded_leak.<locals>.workerT   s!    IIgkk("5"5"789r   rA   rL   rN   r:   rO   rP   rQ   rR   r2   r3   _TestLeaks__recycle_threadsgccollectassertIsNoner   rK   r>   rS   r=   rZ   s        @r   test_threaded_leakzTestLeaks.test_threaded_leakR   s    	: qA  /AGGIFF2J	 
 	 


Aac" r   c                    g fd}t        d      D ]:  }t        j                  |      }|j                          |j	                  d       ~< t        j                          | j                          t        j                          t        j                          t        j                          D ]  }| j                   |               y )Nc                     g xt        j                         _        fd} t        d      D ]%  }t        j                   |       j	                          ' j                  t        j                  t        j                                      y )Nc                  L     j                  t        j                                y r   )rW   r2   r3   )lls   r   
additionalzDTestLeaks.test_threaded_adv_leak.<locals>.worker.<locals>.additionali   s    		(--/0r   rA   )r2   r3   re   r:   r5   rW   rX   rY   )rf   r>   re   rZ   s     @r   rK   z0TestLeaks.test_threaded_adv_leak.<locals>.workerf   sa    ,..B$$&)11X!!*-446 IIgkk("5"5"789r   rA   rL   rN   r[   r`   s        @r   test_threaded_adv_leakz TestLeaks.test_threaded_adv_leakd   s    	: qA  /AGGIFF2J	 
 	 


Aac" r   c                 V   t         j                  j                         }| j                  |d       t         j                  j	                  d       t         j                  j                         }| j                  ||       | j                  t         j                  j                  d       y )Nr   Tr   )r2   	_greenlet&get_clocks_used_doing_optional_cleanupassertGreaterEqualenable_optional_cleanupr;   assertGreaterCLOCKS_PER_SEC)r   usedused2s      r   assertClocksUsedzTestLeaks.assertClocksUsed{   sz    !!HHJa(2248""IIKu%8--<<a@r   c                   	
 t        j                  g       sJ t        j                          t	        j
                          | j                  t        j                  d      }t        j                         
t        j                         	g fdg 	
fd}t        j                  |      }|j                          
j                  d       t	        j
                          | j                  t        d      }t              dk(  sJ | j                  d	   j                         d d = 	j!                          |j#                  d       ~| j%                          | j                  t        d      }| j                  t        j                  d      }| j'                  ||       s[t        j(                  j+                         =| j-                  ||       r*| j-                  t        j.                  t!                      n	 t        j(                  j+                         | j1                          y y )
NF)
exact_kindc                  2   t        d      } t        j                  j                  dt        d             r2t        j                         j
                  j                  } || g       y t        j                         j
                  j                  | g       y )NzDELETING STACK OBJECTtest_leaks_keyzDELETING THREAD STATE)r   r2   ri   set_thread_localr3   r4   r5   )jdsexplicit_reference_to_switchs     r   background_greenletz6TestLeaks._check_issue251.<locals>.background_greenlet   sv    ,-DEB// +,CDF
 ,'')00772$##%,,33RD9r   c                  @   t        j                         } j                  t        j                  | j                               j                  |        | j                          ~ j                          j                  d       rt        j                          y y )NrN   )	r2   rW   rX   rY   r4   r5   r'   waitr3   )gletbackground_glet_killedbackground_glet_runningrz   background_greenletsbg_main_wrefsmanually_collect_backgrounds    r   background_threadz4TestLeaks._check_issue251.<locals>.background_thread   sy    $$%89D  T[[!9: ''-KKM#'')"''+
 +##% +r   rL   rN   Tr   r   )r]   
is_trackedr   r#   r2   r3   count_objectsrO   EventrP   rQ   r|   listlenassertFalsedeadr'   rR   wait_for_pending_cleanupsassertLessEqualri   rj   r;   r   rq   )r   r   ry   greenlets_beforer   rS   lists_beforelists_aftergreenlets_afterr~   r   rz   r   r   s    ``      @@@@@r   _check_issue251zTestLeaks._check_issue251   s    }}R   #))+--h.?.?E-R"+//"3!*!2!	: 	& 	&$ $56		$$R())$4)@'(A----a0556 !#""$ 	
r
 	&&((($(?,,X->->5,Q
 	[,7 ,!!HHJV_.>?*   !<!M!MsuU DDFR!!# Sr   c                 $    | j                          y r   r   r   s    r   -test_issue251_killing_cross_thread_leaks_listz7TestLeaks.test_issue251_killing_cross_thread_leaks_list  s    r   c                     t         j                  j                  d       	 | j                          t         j                  j                  d       y # t         j                  j                  d       w xY w)NFT)r2   ri   rl   r   r   s    r   #test_issue251_with_cleanup_disabledz-TestLeaks.test_issue251_with_cleanup_disabled  sP    2259	=  "66t<H66t<s   A !A2c                 (    | j                  d       y )NFr   r   r   s    r   4test_issue251_issue252_need_to_collect_in_backgroundz>TestLeaks.test_issue251_issue252_need_to_collect_in_background  s    & 	?r   c                     d| _         t        j                  j                  d       	 | j	                  d       t        j                  j                  d       y # t        j                  j                  d       w xY w)NTFr   )expect_greenlet_leakr2   ri   rl   r   r   s    r   Etest_issue251_issue252_need_to_collect_in_background_cleanup_disabledzOTestLeaks.test_issue251_issue252_need_to_collect_in_background_cleanup_disabled!  s]    $(!2259	=  U C66t<H66t<s   A !A;c                 *    | j                  dd       y )NFT)r   ry   r   r   s    r   9test_issue251_issue252_explicit_reference_not_collectablezCTestLeaks.test_issue251_issue252_explicit_reference_not_collectable*  s    (-)- 	 	/r   r8   c                     t         j                  d   dk\  sJ t         j                  d d dk  r| j                  d       t        r| j                  d       y y )Nr      rA   )r      zOnly observed on 3.11z!Slow and not worth repeating here)r   version_infoskipTestr   r   s    r   _only_test_some_versionsz"TestLeaks._only_test_some_versions2  sT     "a'''BQ&(MM12MM=>  r   c                 `   | j                          d dfd}t        d      D ]	  } |         | j                         }t        | j                        D ]@  }t	        || j                               } |        | j                         }||k  s:|dkD  s@ n | j                  |       y )Nc                       y)Nr   r)   r)   r   r   fz:TestLeaks.test_untracked_memory_doesnt_increase.<locals>.fF  s    r   '  c                  l    t              D ]%  } t        j                        j                          ' y r   )r:   r2   r5   )r>   ITERr   s    r   run_itz?TestLeaks.test_untracked_memory_doesnt_increase.<locals>.run_itJ  s(    4[!!!$++- !r   r   r   )r   r:   get_process_ussUNTRACK_ATTEMPTSmaxr   )r   r   r>   
uss_beforecount	uss_afterr   r   s         @@r   %test_untracked_memory_doesnt_increasez/TestLeaks.test_untracked_memory_doesnt_increase?  s     	%%'	 	.
 qAH  ))+
4001EZ)=)=)?@JH,,.IJ&519 2 	Y
3r   c                 <  	
 | j                          dgfd
d		
fd|  G fdd      }d x}}t        | j                        D ]  }dd<    |       }t        j                  |      }|j                          |j                  d       | j                  |j                                ||j                  }t        ||j                        }r>| j                  |j                  d	       | j                  d   	|j                  z         ~~s| j                  d   d       r| j                          | j                         }||k  s|d
kD  s n | j                          | j                         }| j!                  ||dfz         y )Nr   c                      	 t        j                         j                  j                          y# t         j                  $ r  dxx   dz  cc<    w xY w)Nr   r   )r2   r3   r4   r5   GreenletExit)
EXIT_COUNTs   r   r   z3TestLeaks._check_untracked_memory_thread.<locals>.fh  sP    ##%,,335  (( 1"s	   ,0 "Ar   c                      g } t              D ]8  }t        j                        }| j                  |       |j                          : | S r   )r:   r2   rW   r5   )gletsr>   r=   r   r   s      r   r   z8TestLeaks._check_untracked_memory_thread.<locals>.run_itq  sC    E4[ %%a(Q
 ! Lr   c                   ,    e Zd ZdxZZdZdZ fdZy)<TestLeaks._check_untracked_memory_thread.<locals>.ThreadFuncr   r)   rA   c                 >   j                         | _        t        | j                        D ]%  }| xj                  t                      z  c_        ' | j                  D ]  }j                  dt        |              rd| _        j                         | _        y )Nzsuspended activer)   )	r   r   r:   r   r   tupleassertInstrr   )r   r>   r=   deallocate_in_threadr   tests      r   __call__zETestLeaks._check_untracked_memory_thread.<locals>.ThreadFunc.__call__  sy    "&"6"6"8tyy)AJJ%/1J * AMM"4c!f= $ (!#DJ!%!5!5!7r   N)r$   r%   r&   r   r   r   r   r   )r   r   r   s   r   
ThreadFuncr     s    %&&JED8r   r   rL      r)   r   zafter attempts %d)r   r:   r   rO   rP   rQ   rR   r   is_aliver   r   r;   r   r   r   r   r   )r   r   r   r   r   r   thread_funcrS   r   r   r   r   r   s    `      @@@@@r   _check_untracked_memory_threadz(TestLeaks._check_untracked_memory_threada  s   %%' S
	 
	 	8 	8$ "&%
Y4001EJqM$,K  4AGGIFF2JQZZ\*!(33
Z)?)?@J#  !2!2B7  A{7G7G0GH'  A2#..0,,.IJ&5195 28 	&&(((*	Y
4G5(4RSr   c                 (    | j                  d       y )NTr   r   r   s    r   Itest_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_threadzSTestLeaks.test_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_thread  s     	+++Fr   c                 (    | j                  d       y )NFr   r   r   s    r   Gtest_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_mainzQTestLeaks.test_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_main  s     	+++Gr   N)TF)T)r$   r%   r&   r?   rE   staticmethodr\   ra   rg   rq   r   r   r   r	   r   r   r   r   r   r
   r   r   r   r   r)   r   r   r+   r+   *   s    A
I  #$#.A 595:|$|= @ @( = = / /
 ? 4 4>OTb G G H Hr   r+   __main__unittest)__doc__
__future__r   r   r   r   r]   rI   rX   rO   r2    r   r   	leakcheckr	   r
   r   GREENLET_USE_GCobjectr   r+   r$   
__import__mainr)   r   r   <module>r      s    A @ 
 	       & ( +  %& % RH RHh zz! r   