
    Yh2E                        S r SSKJrJrJr  SSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKJr  SSKJr  SSKJr  SSKJr  SS	KJr  \
R$                  (       d   e " S
 S\5      r " S S\5      r\S:X  a  \" S5      R1                  5         gg)z)
Testing scenarios that may have leaked.
    )print_functionabsolute_importdivisionN   )TestCase)PY314)fails_leakcheck)ignores_leakcheck)RUNNING_ON_MANYLINUXc                   D    \ rS rSr\" 5       rS rS rS r\	S 5       r
Srg)HasFinalizerTracksInstances   c                     [         R                  " U5      U l        U R                  R	                  [        U 5      5        g N)sysinternmsgEXTANT_INSTANCESaddid)selfr   s     K/var/www/html/env/lib/python3.13/site-packages/greenlet/tests/test_leaks.py__init__$HasFinalizerTracksInstances.__init__   s)    ::c?!!"T(+    c                 L    U R                   R                  [        U 5      5        g r   )r   remover   r   s    r   __del__#HasFinalizerTracksInstances.__del__   s    $$RX.r   c                 6    S[        U 5      U R                  4-  $ )Nz(<HasFinalizerTracksInstances at 0x%x %r>)r   r   r   s    r   __repr__$HasFinalizerTracksInstances.__repr__!   s!    9tHdhh=
 
 	
r   c                 8    U R                   R                  5         g r   )r   clear)clss    r   reset!HasFinalizerTracksInstances.reset%   s    ""$r   )r   N)__name__
__module____qualname____firstlineno__setr   r   r   r"   classmethodr'   __static_attributes__ r   r   r   r      s-    u,/
 % %r   r   c                       \ rS rSrS rS r\S 5       rS rS r	S r
  SS jrS	 rS
 r\S 5       r\S 5       r\S 5       rSrS r\S 5       rSS jr\S 5       r\S 5       rSrg)	TestLeaks*   c                     Sn[         R                  " U5      n[        R                  " S 5      n[        S5       H  nUR                  " U6   M     U R                  [         R                  " U5      U5        g )N)abcc                  Z    [         R                  " 5       R                  R                  " U 6 $ r   greenlet
getcurrentparentswitch)argss    r   <lambda>)TestLeaks.test_arg_refs.<locals>.<lambda>1   s    (--/66==tDr   d   )r   getrefcountr:   ranger=   assertEqual)r   r>   refcount_beforeg_s        r   test_arg_refsTestLeaks.test_arg_refs,   s[    //$/DFsAHHdO .@r   c                 T   0 nU R                  [        R                  " U5      [        (       d  SOS5        [        R                  " S 5      n[        S5       H  nUR                  " S0 UD6  M     U R                  [        R                  " U5      [        (       d  S5        g S5        g )N   r   c                  `    [         R                  " 5       R                  R                  " S0 U D6$ )Nr0   r9   )gkwargss    r   r?   +TestLeaks.test_kwarg_refs.<locals>.<lambda>;   s!    h113::AALGLr   rA   r0   )rD   r   rB   r   r:   rC   r=   )r   kwargsrF   rG   s       r   test_kwarg_refsTestLeaks.test_kwarg_refs6   s{    055!aHLNsAHHv  	055!HaHr   c                      S n [         R                  " U S9nUR                  5         [        R                  " S5        UR                  S5        g )Nc                  0    [         R                  " S5        g )NMbP?)timesleepr0   r   r   worker+TestLeaks.__recycle_threads.<locals>.workerK   s    JJur   targetrT   
   )	threadingThreadstartrU   rV   join)rW   ts     r   __recycle_threadsTestLeaks.__recycle_threadsC   s7    	F+		

5	r
r   c                   ^ / mU4S jn[        S5       H9  n[        R                  " US9nUR                  5         UR	                  S5        AM;     [
        R                  " 5         U R                  5         [
        R                  " 5         [        R                  " 5         [
        R                  " 5         T H  nU R                  U" 5       5        M     g )Nc                  v   > T R                  [        R                  " [        R                  " 5       5      5        g r   )appendweakrefrefr:   r;   )ggs   r   rW   ,TestLeaks.test_threaded_leak.<locals>.workerT   s!    IIgkk("5"5"789r   rK   rY   r[   rC   r\   r]   r^   r_   r:   r;   _TestLeaks__recycle_threadsgccollectassertIsNoner   rW   rG   r`   rF   rh   s        @r   test_threaded_leakTestLeaks.test_threaded_leakR   s    	: qA  /AGGIFF2J	 
 	 


Aac" r   c                   ^ / mU4S jn[        S5       H9  n[        R                  " US9nUR                  5         UR	                  S5        AM;     [
        R                  " 5         U R                  5         [
        R                  " 5         [        R                  " 5         [
        R                  " 5         T H  nU R                  U" 5       5        M     g )Nc                  (  >^ / =m[         R                  " 5       l        U4S jn [        S5       H'  n[         R                   " U 5      R	                  5         M)     TR                  [        R                  " [         R                  " 5       5      5        g )Nc                  N   > T R                  [        R                  " 5       5        g r   )re   r:   r;   )lls   r   
additionalDTestLeaks.test_threaded_adv_leak.<locals>.worker.<locals>.additionali   s    		(--/0r   rK   )r:   r;   ru   rC   r=   re   rf   rg   )rv   rG   ru   rh   s     @r   rW   0TestLeaks.test_threaded_adv_leak.<locals>.workerf   sa    ,..B$$&)11X!!*-446 IIgkk("5"5"789r   rK   rY   r[   rj   ro   s        @r   test_threaded_adv_leak TestLeaks.test_threaded_adv_leakd   s    	: qA  /AGGIFF2J	 
 	 


Aac" r   c                 T   [         R                  R                  5       nU R                  US5        [         R                  R	                  S5        [         R                  R                  5       nU R                  X5        U R                  [         R                  R                  S5        g )Nr   Tr   )r:   	_greenlet&get_clocks_used_doing_optional_cleanupassertGreaterEqualenable_optional_cleanuprD   assertGreaterCLOCKS_PER_SEC)r   usedused2s      r   assertClocksUsedTestLeaks.assertClocksUsed{   sx    !!HHJa(2248""IIK%8--<<a@r   c                   ^^^	^
^^^ [         R                  " / 5      (       d   e[        R                  5         [        R
                  " 5         U R                  [        R                  SS9n[        R                  " 5       m
[        R                  " 5       m	/ mU4S jm/ mU	U
UUUU4S jn[        R                  " US9nUR                  5         T
R                  S5        [        R
                  " 5         U R                  [        SS9n[        T5      S:X  d   eU R                  TS	   R                  5        TS S 2	 T	R!                  5         UR#                  S5        AU R%                  5         U R                  [        SS9nU R                  [        R                  SS9nU R'                  Xv5        T(       d`  [        R(                  R+                  5       bA  U R-                  X5        T(       a(  U R-                  [        R.                  [!        5       5        O [        R(                  R+                  5       b  U R1                  5         g g )
NF)
exact_kindc                  @  > [        S5      n [        R                  R                  S[        S5      5        T(       a3  [        R                  " 5       R
                  R                  nU" U /5        g [        R                  " 5       R
                  R                  U /5        g )NzDELETING STACK OBJECTtest_leaks_keyzDELETING THREAD STATE)r   r:   r|   set_thread_localr;   r<   r=   )jdsexplicit_reference_to_switchs     r   background_greenlet6TestLeaks._check_issue251.<locals>.background_greenlet   sv    ,-DEB// +,CDF
 ,'')00772$##%,,33RD9r   c                  P  > [         R                   " T5      n TR                  [        R                  " U R                  5      5        TR                  U 5        U R                  5         A TR                  5         TR                  S5        T(       a  [         R                  " 5         g g )Nr[   )	r:   re   rf   rg   r<   r=   r-   waitr;   )gletbackground_glet_killedbackground_glet_runningr   background_greenletsbg_main_wrefsmanually_collect_backgrounds    r   background_thread4TestLeaks._check_issue251.<locals>.background_thread   sy    $$%89D  T[[!9: ''-KKM#'')"''+
 +##% +r   rY   r[   Tr   r   )rl   
is_trackedr   r'   r:   r;   count_objectsr\   Eventr]   r^   r   listlenassertFalsedeadr-   r_   wait_for_pending_cleanupsassertLessEqualr|   r}   rD   r   r   )r   r   r   greenlets_beforer   r`   lists_beforelists_aftergreenlets_afterr   r   r   r   r   s    ``      @@@@@r   _check_issue251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                 $    U R                  5         g r   r   r   s    r   -test_issue251_killing_cross_thread_leaks_list7TestLeaks.test_issue251_killing_cross_thread_leaks_list  s    r   c                     [         R                  R                  S5         U R                  5         [         R                  R                  S5        g ! [         R                  R                  S5        f = f)NFT)r:   r|   r   r   r   s    r   #test_issue251_with_cleanup_disabled-TestLeaks.test_issue251_with_cleanup_disabled  sP    2259	=  "66t<H66t<s   A !A2c                 "    U R                  SS9  g )NFr   r   r   s    r   4test_issue251_issue252_need_to_collect_in_background>TestLeaks.test_issue251_issue252_need_to_collect_in_background  s    & 	?r   c                     SU l         [        R                  R                  S5         U R	                  SS9  [        R                  R                  S5        g ! [        R                  R                  S5        f = f)NTFr   )expect_greenlet_leakr:   r|   r   r   r   s    r   Etest_issue251_issue252_need_to_collect_in_background_cleanup_disabledOTestLeaks.test_issue251_issue252_need_to_collect_in_background_cleanup_disabled!  s]    $(!2259	=  U C66t<H66t<s   A !A8c                 $    U R                  SSS9  g )NFT)r   r   r   r   s    r   9test_issue251_issue252_explicit_reference_not_collectableCTestLeaks.test_issue251_issue252_explicit_reference_not_collectable*  s    (-)- 	 	/r   rA   c                     [         R                  S   S:  d   e[         R                  S S S:  a  U R                  S5        [        (       a  U R                  S5        g g )Nr      rK   )r      zOnly observed on 3.11z!Slow and not worth repeating here)r   version_infoskipTestr   r   s    r   _only_test_some_versions"TestLeaks._only_test_some_versions2  sW     "a'''BQ&(MM12MM=>  r   c                 l  ^^ U R                  5         S mSmUU4S jn[        S5       H
  nU" 5         M     U R                  5       n[        U R                  5       HB  n[	        X0R                  5       5      nU" 5         U R                  5       nXS::  d  M:  US:  d  MB    O   U R                  WU5        g )Nc                      g)Nr   r0   r0   r   r   f:TestLeaks.test_untracked_memory_doesnt_increase.<locals>.fF  s    r   '  c                  r   > [        T5       H'  n [        R                  " T5      R                  5         M)     g r   )rC   r:   r=   )rG   ITERr   s    r   run_it?TestLeaks.test_untracked_memory_doesnt_increase.<locals>.run_itJ  s(    4[!!!$++- !r   r   r   )r   rC   get_process_ussUNTRACK_ATTEMPTSmaxr   )r   r   rG   
uss_beforecount	uss_afterr   r   s         @@r   %test_untracked_memory_doesnt_increase/TestLeaks.test_untracked_memory_doesnt_increase?  s     	%%'	 	.
 qAH  ))+
4001EZ)=)=)?@JH,,.I&519 2 	Y
3r   c                 `  ^^^	^
^^ U R                  5         S/mU4S jm
Sm	U	U
4S jmU m " UUU4S jS5      nS =p4[        U R                  5       GH"  nSTS'   U" 5       n[        R                  " US9nUR                  5         UR                  S5        U R                  UR                  5       5        Uc  UR                  n[        X6R                  5      nT(       a>  U R                  UR                  S	5        U R                  TS   T	UR                  -  5        AAT(       d  U R                  TS   S5        T(       a  U R                  5         U R                  5       nXC::  d  GM  US
:  d  GM#    O   U R                  5         U R                  5       nU R!                  XCSW4-  5        g )Nr   c                     >  [         R                  " 5       R                  R                  5         g! [         R                   a    T S==   S-  ss'   e f = f)Nr   r   )r:   r;   r<   r=   GreenletExit)
EXIT_COUNTs   r   r   3TestLeaks._check_untracked_memory_thread.<locals>.fh  sP    ##%,,335  (( 1"s	   -1 #Ar   c                     > / n [        T5       H:  n[        R                  " T5      nU R                  U5        UR                  5         M<     U $ r   )rC   r:   re   r=   )gletsrG   rF   r   r   s      r   r   8TestLeaks._check_untracked_memory_thread.<locals>.run_itq  sC    E4[ %%a(Q
 ! Lr   c                   6   > \ rS rSrS=rrSrSrU UU4S jrSr	g)<TestLeaks._check_untracked_memory_thread.<locals>.ThreadFunci  r   r0   rK   c                 P  > TR                  5       U l        [        U R                  5       H&  nU =R                  [        T" 5       5      -  sl        M(     U R                   H  nTR                  S[        U5      5        M      T(       a  SU l        TR                  5       U l        g )Nzsuspended activer0   )	r   r   rC   r   r   tupleassertInstrr   )r   rG   rF   deallocate_in_threadr   tests      r   __call__ETestLeaks._check_untracked_memory_thread.<locals>.ThreadFunc.__call__  sy    "&"6"6"8tyy)AJJ%/1J * AMM"4c!f= $ (!#DJ!%!5!5!7r   )r   r   r   N)
r)   r*   r+   r,   r   r   r   r   r   r/   )r   r   r   s   r   
ThreadFuncr     s!    %&&JED8 8r   r   rY      r0   r   zafter attempts %d)r   rC   r   r\   r]   r^   r_   r   is_aliver   r   rD   r   r   r   r   r   )r   r   r   r   r   r   thread_funcr`   r   r   r   r   r   s    `      @@@@@r   _check_untracked_memory_thread(TestLeaks._check_untracked_memory_threada  sw   %%' S
	 
	 	8 	8$ "&%
4001EJqM$,K  4AGGIFF2JQZZ\*!(33
Z)?)?@J#  !2!2B7  A{7G7G0GH'  A2#..0,,.I&5195 28 	&&(((*	Y4G5(4RSr   c                 "    U R                  SS9  g )NTr   r   r   s    r   Itest_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_threadSTestLeaks.test_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_thread  s     	+++Fr   c                 "    U R                  SS9  g )NFr   r   r   s    r   Gtest_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_mainQTestLeaks.test_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_main  s     	+++Gr   )r   N)TF)T)r)   r*   r+   r,   rH   rP   staticmethodrk   rp   ry   r   r   r   r   r	   r   r   r   r   r   r
   r   r   r   r   r/   r0   r   r   r2   r2   *   s    A
I  #$#.A 595:|$|= @ @( = = / /
 ? 4 4>OTb G G H Hr   r2   __main__unittest)__doc__
__future__r   r   r   r   rl   rU   rf   r\   r:    r   r   	leakcheckr	   r
   r   GREENLET_USE_GCobjectr   r2   r)   
__import__mainr0   r   r   <module>r      s    A @ 
 	       & ( +  %& % RH RHh zz! r   