
    Bi*K                        d 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 dd	lmZ dd
lmZ ej                   sJ  G d de      Zd Z G d de      Zedk(  r ed      j/                          yy)z)
Testing scenarios that may have leaked.
    N   )TestCase)PY314)RUNNING_ON_FREETHREAD_BUILD)WIN)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     _/home/ubuntu/crypto_trading_bot/.venv/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                 (    t         r| S t        |       S r   )r   r   )funcs    r   &fails_leakcheck_except_on_free_thradedr+   +   s    " 4  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>:   s#    =(--/66==tD r   d   )r   getrefcountr4   ranger7   assertEqual)r   r8   refcount_beforeg_s        r   test_arg_refszTestLeaks.test_arg_refs5   sb    //$/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(   r3   )gkwargss    r   r9   z+TestLeaks.test_kwarg_refs.<locals>.<lambda>D   s&    Ah113::AALGL r   r:   r(   )r=   r   r;   r   r4   r<   r7   )r   kwargsr?   r@   s       r   test_kwarg_refszTestLeaks.test_kwarg_refs?   s~    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>.workerT   s    JJur   targetrJ   
   )	threadingThreadstartrK   rL   join)rM   ts     r   __recycle_threadszTestLeaks.__recycle_threadsL   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refr4   r5   )ggs   r   rM   z,TestLeaks.test_threaded_leak.<locals>.worker]   s!    IIgkk("5"5"789r   rC   rN   rP   r<   rQ   rR   rS   rT   r4   r5   _TestLeaks__recycle_threadsgccollectassertIsNoner   rM   r@   rU   r?   r\   s        @r   test_threaded_leakzTestLeaks.test_threaded_leak[   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   )rY   r4   r5   )lls   r   
additionalzDTestLeaks.test_threaded_adv_leak.<locals>.worker.<locals>.additionalr   s    		(--/0r   rC   )r4   r5   rg   r<   r7   rY   rZ   r[   )rh   r@   rg   r\   s     @r   rM   z0TestLeaks.test_threaded_adv_leak.<locals>.workero   sf    ,..B$$&)11X 7!!*-4467IIgkk("5"5"789r   rC   rN   rP   r]   rb   s        @r   test_threaded_adv_leakz TestLeaks.test_threaded_adv_leakm   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   )r4   	_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   r4   rk   set_thread_localr5   r6   r7   )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 )NrP   )	r4   rY   rZ   r[   r6   r7   r&   waitr5   )gletbackground_glet_killedbackground_glet_runningr|   background_greenletsbg_main_wrefsmanually_collect_backgrounds    r   background_threadz4TestLeaks._check_issue251.<locals>.background_thread   sy    $$%89D  T[[!9: ''-KKM#'')"''+
 +##% +r   rN   rP   Tr   r   )r_   
is_trackedr   r"   r4   r5   count_objectsrQ   EventrR   rS   r~   listlenassertFalsedeadr&   rT   wait_for_pending_cleanupsassertLessEqualrk   rl   r=   r   rs   )r   r   r{   greenlets_beforer   rU   lists_beforelists_aftergreenlets_afterr   r   r|   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)r4   rk   rn   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_leakr4   rk   rn   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   r{   r   r   s    r   9test_issue251_issue252_explicit_reference_not_collectablezCTestLeaks.test_issue251_issue252_explicit_reference_not_collectable3  s    (-)- 	 	/r   r:   c                 d    t         j                  d   dk\  sJ t        r| j                  d       y y )Nr      z!Slow and not worth repeating here)r   version_infor
   skipTestr   s    r   _only_test_some_versionsz"TestLeaks._only_test_some_versions;  s2     "a'''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>.fM  s    r   '  c                  l    t              D ]%  } t        j                        j                          ' y r   )r<   r4   r7   )r@   ITERr   s    r   run_itz?TestLeaks.test_untracked_memory_doesnt_increase.<locals>.run_itQ  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_increaseF  s     	%%'	 	.
 q 	AH	 ))+
4001 	EZ)=)=)?@JH,,.IJ&519	 	Y
3r   c                 v  	
 | j                          | 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                         }t         rdnd}| j#                  |||z   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   )r4   r5   r6   r7   GreenletExit)
EXIT_COUNTs   r   r   z3TestLeaks._check_untracked_memory_thread.<locals>.ft  sP    ##%,,335  (( 1"s	   ,0 "Ar   c                      g } t              D ]8  }t        j                        }| j                  |       |j                          : | S r   )r<   r4   rY   r7   )gletsr@   r?   r   r   s      r   r   z8TestLeaks._check_untracked_memory_thread.<locals>.run_it}  sH    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(   rC   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__  s    "&"6"6"8tyy) 2AJJ%/1J2  >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   rN      r(   r   i   zafter attempts %d)r   r   r<   r   rQ   rR   rS   rT   r   is_aliver   r   r=   r   r   r   r   r   )r   r   r   r   r   r   thread_funcrU   	tolerancer   r   r   r   r   s    `       @@@@@r   _check_untracked_memory_threadz(TestLeaks._check_untracked_memory_threadh  s   %%' 	S
	 
	 	8 	8$ "&%
Y4001 	EJqM$,K  4AGGIFF2JQZZ\*!(33
Z)?)?@J#  !2!2B7  A{7G7G0GH'  A2#..0,,.IJ&5195	8 	&&(((*	 #&J1	Y
Y(>0E8;	=r   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                 n    t         rt        rt        r| j                  d       | j	                  d       y )Nz6Windows 3.14t appears to leak. No other platform does.Fr   )r   r   r   r   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*     .5MMRS+++Gr   N)TF)T)r#   r$   r%   rA   rG   staticmethodr^   rc   ri   rs   r   r   r   r+   r   r   r   r   r   r	   r   r   r   r   r(   r   r   r-   r-   3   s    A
I  #$#.A 595:|$|= ,@ ,@( ,= ,= ,/ ,/
 	? 4 4>\=| G G H Hr   r-   __main__unittest)__doc__r   r_   rK   rZ   rQ   r4    r   r   r   r   	leakcheckr   r	   r
   GREENLET_USE_GCobjectr   r+   r-   r#   
__import__mainr(   r   r   <module>r      s     	       )  & ( +
  %& % !hH hHT zz! r   