
    g9D                         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 e
j"                  sJ  G d	 d
e      Z G d de      Zedk(  r ed      j/                          yy)z)
Testing scenarios that may have leaked.
    )print_functionabsolute_importdivisionN   )TestCase)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     Y/var/www/api/v1/venv_getwork_v1/lib/python3.12/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>0   s!    =(--/66==tDr   d   )r   getrefcountr1   ranger4   assertEqual)r   r5   refcount_beforeg_s        r   test_arg_refszTestLeaks.test_arg_refs+   s]    //$/DFsAAHHdO .@r   c                     i }t        j                   d       }t        d      D ]  } |j                  di |  | j                  t	        j
                  |      d       y )Nc                  ^     t        j                         j                  j                  di | S )Nr(   r0   )kwargss    r   r6   z+TestLeaks.test_kwarg_refs.<locals>.<lambda>9   s$    @X00299@@J6Jr   r7      r(   )r1   r9   r4   r:   r   r8   )r   rA   r<   r=   s       r   test_kwarg_refszTestLeaks.test_kwarg_refs5   sS    JLsAAHHv 0!4r   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>.workerG   s    JJur   targetrF   
   )	threadingThreadstartrG   rH   join)rI   ts     r   __recycle_threadszTestLeaks.__recycle_threads?   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refr1   r2   )ggs   r   rI   z,TestLeaks.test_threaded_leak.<locals>.workerP   s!    IIgkk("5"5"789r   rB   rJ   rL   r9   rM   rN   rO   rP   r1   r2   _TestLeaks__recycle_threadsgccollectassertIsNoner   rI   r=   rQ   r<   rX   s        @r   test_threaded_leakzTestLeaks.test_threaded_leakN   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   )rU   r1   r2   )lls   r   
additionalzDTestLeaks.test_threaded_adv_leak.<locals>.worker.<locals>.additionale   s    		(--/0r   rB   )r1   r2   rc   r9   r4   rU   rV   rW   )rd   r=   rc   rX   s     @r   rI   z0TestLeaks.test_threaded_adv_leak.<locals>.workerb   sa    ,..B$$&)11X!!*-446 IIgkk("5"5"789r   rB   rJ   rL   rY   r^   s        @r   test_threaded_adv_leakz TestLeaks.test_threaded_adv_leak`   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   )r1   	_greenlet&get_clocks_used_doing_optional_cleanupassertGreaterEqualenable_optional_cleanupr:   assertGreaterCLOCKS_PER_SEC)r   usedused2s      r   assertClocksUsedzTestLeaks.assertClocksUsedw   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   r1   rg   set_thread_localr2   r3   r4   )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 )NrL   )	r1   rU   rV   rW   r3   r4   r&   waitr2   )gletbackground_glet_killedbackground_glet_runningrx   background_greenletsbg_main_wrefsmanually_collect_backgrounds    r   background_threadz4TestLeaks._check_issue251.<locals>.background_thread   sy    $$%89D  T[[!9: ''-KKM#'')"''+
 +##% +r   rJ   rL   Tr   r   )r[   
is_trackedr   r"   r1   r2   count_objectsrM   EventrN   rO   rz   listlenassertFalsedeadr&   rP   wait_for_pending_cleanupsassertLessEqualrg   rh   r:   r   ro   )r   r   rw   greenlets_beforer   rQ   lists_beforelists_aftergreenlets_afterr|   r}   rx   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)r1   rg   rj   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_leakr1   rg   rj   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   rw   r   r   s    r   9test_issue251_issue252_explicit_reference_not_collectablezCTestLeaks.test_issue251_issue252_explicit_reference_not_collectable&  s    (-)- 	 	/r   r7   c                     t         j                  d   dk\  sJ t         j                  d d dk  r| j                  d       t        r| j                  d       y y )Nr      rB   )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_versions.  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>.fB  s    r   '  c                  l    t              D ]%  } t        j                        j                          ' y r   )r9   r1   r4   )r=   ITERr   s    r   run_itz?TestLeaks.test_untracked_memory_doesnt_increase.<locals>.run_itF  s(    4[!!!$++- !r   r   r   )r   r9   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   )r1   r2   r3   r4   GreenletExit)
EXIT_COUNTs   r   r   z3TestLeaks._check_untracked_memory_thread.<locals>.fd  sP    ##%,,335  (( 1"s	   ,0 "Ar   c                      g } t              D ]8  }t        j                        }| j                  |       |j                          : | S r   )r9   r1   rU   r4   )gletsr=   r<   r   r   s      r   r   z8TestLeaks._check_untracked_memory_thread.<locals>.run_itm  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(   rB   c                 >   j                         | _        t        | j                        D ]%  }| xj                  t                      z  c_        ' | j                  D ]  }j                  dt        |              rd| _        j                         | _        y )Nzsuspended activer(   )	r   r   r9   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   rJ      r(   r   zafter attempts %d)r   r9   r   rM   rN   rO   rP   r   is_aliver   r   r:   r   r   r   r   r   )r   r   r   r   r   r   thread_funcrQ   r   r   r   r   r   s    `      @@@@@r   _check_untracked_memory_threadz(TestLeaks._check_untracked_memory_thread]  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>   rC   staticmethodrZ   r_   re   ro   r   r   r   r   r   r   r   r   r   r	   r   r   r   r   r(   r   r   r*   r*   )   s    A5  #$#.A 595:|$|= @ @( = = / /
 ? 4 4>OTb G G H Hr   r*   __main__unittest)__doc__
__future__r   r   r   r   r[   rG   rV   rM   r1    r   	leakcheckr   r	   r
   GREENLET_USE_GCobjectr   r*   r#   
__import__mainr(   r   r   <module>r      s    A @ 
 	      & ( +  %& % OH OHb zz! r   