
    lg9D                        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  \
R"                  (       d   e " S	 S
\5      r " S S\5      r\S:X  a  \" S5      R/                  5         gg)z)
Testing scenarios that may have leaked.
    )print_functionabsolute_importdivisionN   )TestCase)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     a/Users/Alptekin/Desktop/izin_takip/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                       \ 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>0   s    (--/66==tDr   d   )r   getrefcountr9   ranger<   assertEqual)r   r=   refcount_beforeg_s        r   test_arg_refsTestLeaks.test_arg_refs+   s[    //$/DFsAHHdO .@r   c                     0 n[         R                   " S 5      n[        S5       H  nUR                  " S0 UD6  M     U R                  [        R
                  " U5      S5        g )Nc                  `    [         R                  " 5       R                  R                  " S0 U D6$ )Nr/   r8   )kwargss    r   r>   +TestLeaks.test_kwarg_refs.<locals>.<lambda>9   s!    X00299@@J6Jr   r@      r/   )r9   rB   r<   rC   r   rA   )r   rK   rE   rF   s       r   test_kwarg_refsTestLeaks.test_kwarg_refs5   sQ    JLsAHHv 0!4r   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sleepr/   r   r   worker+TestLeaks.__recycle_threads.<locals>.workerG   s    JJur   targetrR   
   )	threadingThreadstartrS   rT   join)rU   ts     r   __recycle_threadsTestLeaks.__recycle_threads?   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refr9   r:   )ggs   r   rU   ,TestLeaks.test_threaded_leak.<locals>.workerP   s!    IIgkk("5"5"789r   rM   rW   rY   rB   rZ   r[   r\   r]   r9   r:   _TestLeaks__recycle_threadsgccollectassertIsNoner   rU   rF   r^   rE   rf   s        @r   test_threaded_leakTestLeaks.test_threaded_leakN   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   )rc   r9   r:   )lls   r   
additionalDTestLeaks.test_threaded_adv_leak.<locals>.worker.<locals>.additionale   s    		(--/0r   rM   )r9   r:   rs   rB   r<   rc   rd   re   )rt   rF   rs   rf   s     @r   rU   0TestLeaks.test_threaded_adv_leak.<locals>.workerb   sa    ,..B$$&)11X!!*-446 IIgkk("5"5"789r   rM   rW   rY   rh   rm   s        @r   test_threaded_adv_leak TestLeaks.test_threaded_adv_leak`   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   )r9   	_greenlet&get_clocks_used_doing_optional_cleanupassertGreaterEqualenable_optional_cleanuprC   assertGreaterCLOCKS_PER_SEC)r   usedused2s      r   assertClocksUsedTestLeaks.assertClocksUsedw   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   r9   rz   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 )NrY   )	r9   rc   rd   re   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   rW   rY   Tr   r   )rj   
is_trackedr   r&   r9   r:   count_objectsrZ   Eventr[   r\   r   listlenassertFalsedeadr,   r]   wait_for_pending_cleanupsassertLessEqualrz   r{   rC   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)r9   rz   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_leakr9   rz   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   r@   c                     [         R                  S   S:  d   e[         R                  S S S:  a  U R                  S5        [        (       a  U R                  S5        g g )Nr      rM   )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   r/   r/   r   r   f:TestLeaks.test_untracked_memory_doesnt_increase.<locals>.fB  s    r   '  c                  r   > [        T5       H'  n [        R                  " T5      R                  5         M)     g r   )rB   r9   r<   )rF   ITERr   s    r   run_it?TestLeaks.test_untracked_memory_doesnt_increase.<locals>.run_itF  s(    4[!!!$++- !r   r   r   )r   rB   get_process_ussUNTRACK_ATTEMPTSmaxr   )r   r   rF   
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   )r9   r:   r;   r<   GreenletExit)
EXIT_COUNTs   r   r   3TestLeaks._check_untracked_memory_thread.<locals>.fd  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   )rB   r9   rc   r<   )gletsrF   rE   r   r   s      r   r   8TestLeaks._check_untracked_memory_thread.<locals>.run_itm  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   r/   rM   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 activer/   )	r   r   rB   r   r   tupleassertInstrr   )r   rF   rE   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   rW      r/   r   zafter attempts %d)r   rB   r   rZ   r[   r\   r]   r   is_aliver   r   rC   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_thread]  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+   rG   rN   staticmethodri   rn   rw   r   r   r   r   r   r   r   r   r   r   r	   r   r   r   r   r.   r/   r   r   r1   r1   )   s    A5  #$#.A 595:|$|= @ @( = = / /
 ? 4 4>OTb G G H Hr   r1   __main__unittest)__doc__
__future__r   r   r   r   rj   rS   rd   rZ   r9    r   	leakcheckr   r	   r
   GREENLET_USE_GCobjectr   r1   r(   
__import__mainr/   r   r   <module>r      s    A @ 
 	      & ( +  %& % OH OHb zz! r   