XjPdZddlZddlZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddlm Z ddl m Z dd l mZdd l mZej sJGd d eZd ZGddeZedk(redj/yy)z) Testing scenarios that may have leaked. N)TestCase)PY314)RUNNING_ON_FREETHREAD_BUILD)WIN)fails_leakcheck)ignores_leakcheck)RUNNING_ON_MANYLINUXc<eZdZeZdZdZdZedZ y)HasFinalizerTracksInstancesctj||_|jj t |yN)sysinternmsgEXTANT_INSTANCESaddid)selfrs m/mnt/ssd/data/Dropbox/adrian/vault-secondbrain/venv/lib/python3.12/site-packages/greenlet/tests/test_leaks.py__init__z$HasFinalizerTracksInstances.__init__s)::c? !!"T(+cL|jjt|yr)rremoverrs r__del__z#HasFinalizerTracksInstances.__del__ s $$RX.rc6dt||jfzS)Nz()rrrs r__repr__z$HasFinalizerTracksInstances.__repr__"s!9 tHdhh=   rc8|jjyr)rclear)clss rresetz!HasFinalizerTracksInstances.reset&s ""$rN) __name__ __module__ __qualname__setrrrr classmethodr"rrr r s-u,/ %%rr c(tr|St|Sr)rr)funcs r&fails_leakcheck_except_on_free_thradedr++s" 4  rceZdZdZdZedZdZdZdZ ddZ dZ d Z e d Ze d Ze d Zd ZdZedZddZedZedZy) TestLeakscd}tj|}tjd}tdD]}|j||j tj||y)N)abccXtjjj|Srgreenlet getcurrentparentswitch)argss rz)TestLeaks.test_arg_refs..:s#=(--/66==tDrd)r getrefcountr4ranger7 assertEqual)rr8refcount_beforeg_s r test_arg_refszTestLeaks.test_arg_refs5sb//$/    D Fs A AHHdO  .@rc6i}|jtj|tsdndt jd}t dD]}|j di||jtj|tsdydy)Nrc^tjjjdi|S)Nr(r3)gkwargss rr9z+TestLeaks.test_kwarg_refs..Ds&Ah113::AALGLrr:r()r=rr;rr4r<r7)rkwargsr?r@s rtest_kwarg_refszTestLeaks.test_kwarg_refs?s~ 05!aH    L Ns A AHH v   05!HaHrcd}tj|}|jtjd|j dy)Nc.tjdy)NMbP?)timesleepr(rrworkerz+TestLeaks.__recycle_threads..workerTs JJu rtargetrJ ) threadingThreadstartrKrLjoin)rMts r__recycle_threadszTestLeaks.__recycle_threadsLs7    F +   5 r rcgfd}tdD]:}tj|}|j|j d~<t j |jt j tjt j D]}|j|y)Ncrjtjtjyr)appendweakrefrefr4r5)ggsrrMz,TestLeaks.test_threaded_leak..worker]s! IIgkk("5"5"78 9rrCrNrP r<rQrRrSrTr4r5_TestLeaks__recycle_threadsgccollect assertIsNonerrMr@rUr?r\s @rtest_threaded_leakzTestLeaks.test_threaded_leak[s  :q A  /A GGI FF2J       #A   ac " #rcgfd}tdD]:}tj|}|j|j d~<t j |jt j tjt j D]}|j|y)Ncgxtj_fd}tdD]%}tj|j 'j t jtjy)NcLjtjyr)rYr4r5)llsr additionalzDTestLeaks.test_threaded_adv_leak..worker..additionalrs (--/0rrC)r4r5rgr<r7rYrZr[)rhr@rgr\s @rrMz0TestLeaks.test_threaded_adv_leak..workerosf,. .B$$&) 11X 7!!*-446 7 IIgkk("5"5"78 9rrCrNrPr]rbs @rtest_threaded_adv_leakz TestLeaks.test_threaded_adv_leakms  :q A  /A GGI FF2J       #A   ac " #rcVtjj}|j|dtjj dtjj}|j |||j tjjdy)NrTr)r4 _greenlet&get_clocks_used_doing_optional_cleanupassertGreaterEqualenable_optional_cleanupr= assertGreaterCLOCKS_PER_SEC)rusedused2s rassertClocksUsedzTestLeaks.assertClocksUsedsz!!HHJ a(2248""IIK u% 8--<.background_greenletsv,-DEB    / / +,CD F ,'')00772$##%,,33RD9rc@tj}jtj|jj||j ~j jdrtjyy)NrP) r4rYrZr[r6r7r&waitr5)gletbackground_glet_killedbackground_glet_runningr|background_greenlets bg_main_wrefsmanually_collect_backgrounds rbackground_threadz4TestLeaks._check_issue251..background_threadsy$$%89D  T[[!9 : ' ' - KKM # ' ' ) " ' ' + +##%+rrNrPTrr)r_ is_trackedr r"r4r5 count_objectsrQEventrRrSr~listlen assertFalsedeadr&rTwait_for_pending_cleanupsassertLessEqualrkrlr=rrs)rrr{greenlets_beforerrU lists_before lists_aftergreenlets_afterrrr|rrs `` @@@@@r_check_issue251zTestLeaks._check_issue251s}}R   #))+--h.?.?E-R"+//"3!*!2! :  & &$   $5 6  $$R())$4)@ '(A--- -a0556 ! #""$ r  &&((($(? ,,X->->5,Q [,7,!!HHJV   _.> ?*  !TestLeaks.test_issue251_issue252_need_to_collect_in_backgrounds& ?rcd|_tjjd |j dtjjdy#tjjdwxYw)NTFr)expect_greenlet_leakr4rkrnrrs rEtest_issue251_issue252_need_to_collect_in_background_cleanup_disabledzOTestLeaks.test_issue251_issue252_need_to_collect_in_background_cleanup_disabled*s]$(!2259 =  U C    6 6t  rc`|jddfd}tdD] }| |j}t|jD]@}t ||j}||j}||ks:|dkDs@n|j |y)Ncy)Nrr(r(rrfz:TestLeaks.test_untracked_memory_doesnt_increase..fMsr'cltD]%}tjj'yr)r<r4r7)r@ITERrs rrun_itz?TestLeaks.test_untracked_memory_doesnt_increase..run_itQs-4[ .!!!$++- .rrr)rr<get_process_ussUNTRACK_ATTEMPTSmaxr)rrr@ uss_beforecount uss_afterrrs @@r%test_untracked_memory_doesnt_increasez/TestLeaks.test_untracked_memory_doesnt_increaseFs %%'  . q A H  ))+ 4001 EZ)=)=)?@J H,,.IJ&519   Y 3rcf |j|jdg fd d fd | G fdd}dx}}t|jD]}d d<|}t j |}|j |jd|j|j| |j}t||j}r>|j|jd |j d |jz~~s|j ddr|j|j}||ks |d kDsn|j|j}d }|j!|||zd fzy) Nrc tjjjy#tj$rdxxdz cc<wxYw)Nrr)r4r5r6r7 GreenletExit) EXIT_COUNTsrrz3TestLeaks._check_untracked_memory_thread..ftsP ##%,,335(( 1 "  s ,0"Arcg}tD]8}tj}|j||j:|Sr)r<r4rYr7)gletsr@r?rrs rrz8TestLeaks._check_untracked_memory_thread..run_it}sHE4[ %%a( Q  Lrc,eZdZdxZZdZdZfdZy).ThreadFuncrr(rCc>j|_t|jD]%}|xjt z c_'|jD]}j dt|rd|_j|_y)Nzsuspended activer() rrr<rrtupleassertInstrr)rr@r?deallocate_in_threadrtests r__call__zETestLeaks._check_untracked_memory_thread..ThreadFunc.__call__s"&"6"6"8tyy)2AJJ%/1J2>AMM"4c!f=>(!#DJ!%!5!5!7rN)r#r$r%rrrrr)rrrsr ThreadFuncrs%& &JED 8rrrNr(ri0zafter attempts %d)rrr<rrQrRrSrTris_aliverrr=rrrr)rrrrrr thread_funcrU tolerancerrrrrs ` @@@@@r_check_untracked_memory_threadz(TestLeaks._check_untracked_memory_threadhs %%' S    8 8$"&% Y4001 EJqM$,K  4A GGI FF2J   QZZ\ *!(33 Z)?)?@J#  !2!2B7  A{7G7G0GH'  A2#..0,,.IJ&5195 8 &&(((* 4$  Y Y(>0E8; =rc(|jdy)NTr)rrs rItest_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_threadzSTestLeaks.test_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_threads +++Frcntrtrtr|jd|j dy)Nz6Windows 3.14t appears to leak. No other platform does.Fr)rrrrrrs rGtest_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_mainzQTestLeaks.test_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_mains* .5 MMR S +++GrN)TF)T)r#r$r%rArG staticmethodr^rcrirsrrrr+rrrrrr rrrrr(rrr-r-3sA I  #$#.A595:|$|=,@,@(,=,=,/,/  ?44>o=bGG H Hrr-__main__unittest)__doc__rr_rKrZrQr4rrrr leakcheckrr r GREENLET_USE_GCobjectr r+r-r# __import__mainr(rrrs )&(+  %& % !{H{Hz  zz!r