
    AhF                     &   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  SS
KJr  \
R&                  (       d   e " S S\5      rS r " S S\5      r\S:X  a  \" S5      R5                  5         gg)z)
Testing scenarios that may have leaked.
    )print_functionabsolute_importdivisionN   )TestCase)PY314)RUNNING_ON_FREETHREAD_BUILD)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     \/home/kali/devsecops-assessor/venv/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                 2    [         (       a  U $ [        U 5      $ r   )r	   r
   )funcs    r   &fails_leakcheck_except_on_free_thradedr4   ,   s    "" 4  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)	TestLeaks4   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>;   s    (--/66==tDr   d   )r   getrefcountr>   rangerA   assertEqual)r   rB   refcount_beforeg_s        r   test_arg_refsTestLeaks.test_arg_refs6   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$ )Nr1   r=   )gkwargss    r   rC   +TestLeaks.test_kwarg_refs.<locals>.<lambda>E   s!    h113::AALGLr   rE   r1   )rH   r   rF   r   r>   rG   rA   )r   kwargsrJ   rK   s       r   test_kwarg_refsTestLeaks.test_kwarg_refs@   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sleepr1   r   r   worker+TestLeaks.__recycle_threads.<locals>.workerU   s    JJur   targetrX   
   )	threadingThreadstartrY   rZ   join)r[   ts     r   __recycle_threadsTestLeaks.__recycle_threadsM   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   r[   ,TestLeaks.test_threaded_leak.<locals>.worker^   s!    IIgkk("5"5"789r   rO   r]   r_   rG   r`   ra   rb   rc   r>   r?   _TestLeaks__recycle_threadsgccollectassertIsNoner   r[   rK   rd   rJ   rl   s        @r   test_threaded_leakTestLeaks.test_threaded_leak\   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   )ri   r>   r?   )lls   r   
additionalDTestLeaks.test_threaded_adv_leak.<locals>.worker.<locals>.additionals   s    		(--/0r   rO   )r>   r?   ry   rG   rA   ri   rj   rk   )rz   rK   ry   rl   s     @r   r[   0TestLeaks.test_threaded_adv_leak.<locals>.workerp   sa    ,..B$$&)11X!!*-446 IIgkk("5"5"789r   rO   r]   r_   rn   rs   s        @r   test_threaded_adv_leak TestLeaks.test_threaded_adv_leakn   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_cleanuprH   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@   rA   )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>   ri   rj   rk   r@   rA   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   r]   r_   Tr   r   )rp   
is_trackedr   r(   r>   r?   count_objectsr`   Eventra   rb   r   listlenassertFalsedeadr.   rc   wait_for_pending_cleanupsassertLessEqualr   r   rH   r   r   )r   r   r   greenlets_beforer   rd   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_collectable4  s    (-)- 	 	/r   rE   c                     [         R                  S   S:  d   e[         R                  S S S:  a  U R                  S5        [        (       a  U R                  S5        g g )Nr      rO   )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_versions<  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   r1   r1   r   r   f:TestLeaks.test_untracked_memory_doesnt_increase.<locals>.fP  s    r   '  c                  r   > [        T5       H'  n [        R                  " T5      R                  5         M)     g r   )rG   r>   rA   )rK   ITERr   s    r   run_it?TestLeaks.test_untracked_memory_doesnt_increase.<locals>.run_itT  s(    4[!!!$++- !r   r   r   )r   rG   get_process_ussUNTRACK_ATTEMPTSmaxr   )r   r   rK   
uss_beforecount	uss_afterr   r   s         @@r   %test_untracked_memory_doesnt_increase/TestLeaks.test_untracked_memory_doesnt_increaseI  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@   rA   GreenletExit)
EXIT_COUNTs   r   r   3TestLeaks._check_untracked_memory_thread.<locals>.fr  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   )rG   r>   ri   rA   )gletsrK   rJ   r   r   s      r   r   8TestLeaks._check_untracked_memory_thread.<locals>.run_it{  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   r1   rO   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 activer1   )	r   r   rG   r   r   tupleassertInstrr   )r   rK   rJ   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   r0   )r   r   r   s   r   
ThreadFuncr     s!    %&&JED8 8r   r   r]      r1   r   zafter attempts %d)r   rG   r   r`   ra   rb   rc   r   is_aliver   r   rH   r   r   r   r   r   )r   r   r   r   r   r   thread_funcrd   r   r   r   r   r   s    `      @@@@@r   _check_untracked_memory_thread(TestLeaks._check_untracked_memory_threadk  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-   rL   rT   staticmethodro   rt   r}   r   r   r   r   r4   r   r   r   r   r   r   r   r   r   r   r0   r1   r   r   r6   r6   4   s    A
I  #$#.A 595:|$|= ,@ ,@( ,= ,= ,/ ,/
 ? 4 4>OTb G G H Hr   r6   __main__unittest)__doc__
__future__r   r   r   r   rp   rY   rj   r`   r>    r   r   r	   	leakcheckr
   r   r   GREENLET_USE_GCobjectr   r4   r6   r*   
__import__mainr1   r   r   <module>r     s    A @ 
 	       ) & ( +
  %& % !RH RHh zz! r   