U W½g9Dã@sºdZddlmZmZmZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZddl mZddl mZe js‚t‚Gd d „d eƒZGd d „d e ƒZed kr¶edƒ ¡dS)z) Testing scenarios that may have leaked. é)Úprint_functionÚabsolute_importÚdivisionNé)ÚTestCase)Úfails_leakcheck)Úignores_leakcheck)ÚRUNNING_ON_MANYLINUXc@s6eZdZeƒZdd„Zdd„Zdd„Zedd„ƒZ d S) ÚHasFinalizerTracksInstancescCs t |¡|_|j t|ƒ¡dS©N)ÚsysÚinternÚmsgÚEXTANT_INSTANCESÚaddÚid)Úselfr©rúI/opt/hc_python/lib64/python3.8/site-packages/greenlet/tests/test_leaks.pyÚ__init__s z$HasFinalizerTracksInstances.__init__cCs|j t|ƒ¡dSr )rÚremover©rrrrÚ__del__sz#HasFinalizerTracksInstances.__del__cCsdt|ƒ|jfS)Nz()rrrrrrÚ__repr__ sÿz$HasFinalizerTracksInstances.__repr__cCs|j ¡dSr )rÚclear)ÚclsrrrÚreset$sz!HasFinalizerTracksInstances.resetN) Ú__name__Ú __module__Ú __qualname__ÚsetrrrrÚ classmethodrrrrrr s r c@s¸eZdZdd„Zdd„Zedd„ƒZdd„Zd d „Zd d „Z d'dd„Z dd„Z dd„Z e dd„ƒZe dd„ƒZe dd„ƒZdZdd„Zedd„ƒZd(d d!„Zed"d#„ƒZed$d%„ƒZd&S))Ú TestLeakscCsJd}t |¡}t dd„¡}tdƒD]}|j|Žq$| t |¡|¡dS)N)ÚaÚbÚccWst ¡jj|ŽSr ©ÚgreenletÚ getcurrentÚparentÚswitch)ÚargsrrrÚ0óz)TestLeaks.test_arg_refs..éd)r Ú getrefcountr'Úranger*Ú assertEqual)rr+Zrefcount_beforeÚgÚ_rrrÚ test_arg_refs+s ÿ  zTestLeaks.test_arg_refscCsBi}t dd„¡}tdƒD]}|jf|Žq| t |¡d¡dS)Nc[st ¡jjf|ŽSr r&)Úkwargsrrrr,9r-z+TestLeaks.test_kwarg_refs..r.é)r'r0r*r1r r/)rr5r2r3rrrÚtest_kwarg_refs5sÿ zTestLeaks.test_kwarg_refscCs4dd„}tj|d}| ¡t d¡| d¡dS)NcSst d¡dS)Nçü©ñÒMbP?)ÚtimeÚsleeprrrrÚworkerGsz+TestLeaks.__recycle_threads..worker©Útargetr8é )Ú threadingÚThreadÚstartr9r:Újoin)r;ÚtrrrZ__recycle_threads?s   zTestLeaks.__recycle_threadscs€g‰‡fdd„}tdƒD]$}tj|d}| ¡| d¡~qt ¡| ¡t ¡t  ¡t ¡ˆD]}|  |ƒ¡qjdS)Ncsˆ t t ¡¡¡dSr )ÚappendÚweakrefÚrefr'r(r©Zggrrr;Psz,TestLeaks.test_threaded_leak..workerr6r<r>© r0r?r@rArBr'r(Ú_TestLeaks__recycle_threadsÚgcZcollectZ assertIsNone©rr;r3rCr2rrGrÚtest_threaded_leakNs    zTestLeaks.test_threaded_leakcs€g‰‡fdd„}tdƒD]$}tj|d}| ¡| d¡~qt ¡| ¡t ¡t  ¡t ¡ˆD]}|  |ƒ¡qjdS)NcsNg‰t ¡_‡fdd„}tdƒD]}t |¡ ¡q"ˆ t t ¡¡¡dS)Ncsˆ t ¡¡dSr )rDr'r(r©ÚllrrÚ additionaleszDTestLeaks.test_threaded_adv_leak..worker..additionalr6)r'r(rNr0r*rDrErF)rOr3rGrMrr;bs   z0TestLeaks.test_threaded_adv_leak..workerr6r<r>rHrKrrGrÚtest_threaded_adv_leak`s    z TestLeaks.test_threaded_adv_leakcCsLtj ¡}| |d¡tj d¡tj ¡}| ||¡| tjjd¡dS)NrTr)r'Ú _greenletÚ&get_clocks_used_doing_optional_cleanupZassertGreaterEqualÚenable_optional_cleanupr1Z assertGreaterZCLOCKS_PER_SEC)rÚusedZused2rrrÚassertClocksUsedws      zTestLeaks.assertClocksUsedTFc sbt g¡st‚t ¡t ¡|jtjdd}t  ¡‰t  ¡‰g‰‡fdd„‰g‰‡‡‡‡‡‡fdd„}tj |d}|  ¡ˆ  d¡t ¡|jt d d}tˆƒd ks¬t‚| ˆd j¡ˆdd…=ˆ ¡| d¡~| ¡|jt d d}|jtjdd}| ||¡ˆsFtj ¡dk rF| ||¡ˆrF| tjtƒ¡ntj ¡dk r^| ¡dS) NF)Z exact_kindcsLtdƒ}tj dtdƒ¡ˆr6t ¡jj}||gƒnt ¡j |g¡dS)NzDELETING STACK OBJECTZtest_leaks_keyzDELETING THREAD STATE)r r'rQZset_thread_localr(r)r*)ZjdÚs)Úexplicit_reference_to_switchrrÚbackground_greenletŸsþ  z6TestLeaks._check_issue251..background_greenletcsRt ˆ¡}ˆ t |j¡¡ˆ |¡| ¡~ˆ ¡ˆ d¡ˆrNt ¡dS)Nr>) r'rDrErFr)r*r Úwaitr()Zglet)Úbackground_glet_killedÚbackground_glet_runningrXÚbackground_greenletsÚ bg_main_wrefsÚmanually_collect_backgroundrrÚbackground_thread¯s   z4TestLeaks._check_issue251..background_threadr<r>Trr)rJZ is_trackedÚAssertionErrorr rr'r(Z count_objectsr?ÚEventr@rArYÚlistÚlenÚ assertFalseZdeadr rBÚwait_for_pending_cleanupsÚassertLessEqualrQrRr1rrU) rr^rWZgreenlets_beforer_rCZ lists_beforeZ lists_afterZgreenlets_afterr)rZr[rXr\r]rWr^rÚ_check_issue251€sB        ÿ zTestLeaks._check_issue251cCs | ¡dSr ©rgrrrrÚ-test_issue251_killing_cross_thread_leaks_listþsz7TestLeaks.test_issue251_killing_cross_thread_leaks_listc Cs,tj d¡z | ¡W5tj d¡XdS)NFT)r'rQrSrgrrrrÚ#test_issue251_with_cleanup_disableds  z-TestLeaks.test_issue251_with_cleanup_disabledcCs|jdddS)NF©r^rhrrrrÚ4test_issue251_issue252_need_to_collect_in_backgroundsz>TestLeaks.test_issue251_issue252_need_to_collect_in_backgroundc Cs6d|_tj d¡z|jddW5tj d¡XdS)NTFrk)Zexpect_greenlet_leakr'rQrSrgrrrrÚEtest_issue251_issue252_need_to_collect_in_background_cleanup_disableds  zOTestLeaks.test_issue251_issue252_need_to_collect_in_background_cleanup_disabledcCs|jddddS)NFT)r^rWrhrrrrÚ9test_issue251_issue252_explicit_reference_not_collectable&sþzCTestLeaks.test_issue251_issue252_explicit_reference_not_collectabler.cCs@tjddkst‚tjdd…dkr.| d¡tr<| d¡dS)Nrér6)roézOnly observed on 3.11z!Slow and not worth repeating here)r Ú version_infor`ZskipTestr rrrrÚ_only_test_some_versions.s  z"TestLeaks._only_test_some_versionscsŽ| ¡dd„‰d‰‡‡fdd„}tdƒD] }|ƒq*| ¡}t|jƒD]4}t|| ¡ƒ}|ƒ| ¡}||krH|dkrHq~qH| ||¡dS)NcSsdS)NrrrrrrÚfBsz:TestLeaks.test_untracked_memory_doesnt_increase..fé'cs tˆƒD]}t ˆ¡ ¡qdSr )r0r'r*)r3©ÚITERrsrrÚrun_itFs z?TestLeaks.test_untracked_memory_doesnt_increase..run_itror)rrr0Úget_process_ussÚUNTRACK_ATTEMPTSÚmaxrf)rrwr3Ú uss_beforeÚcountÚ uss_afterrrurÚ%test_untracked_memory_doesnt_increase;s z/TestLeaks.test_untracked_memory_doesnt_increasecsD| ¡dg‰‡fdd„‰d‰‡‡fdd„‰|‰G‡‡‡fdd„dƒ}d}}t|jƒD]Â}dˆd<|ƒ}tj|d }| ¡| d ¡| | ¡¡|dkr¤|j }t ||j ƒ}ˆrØ|  |j d ¡|  ˆdˆ|j ¡~~ˆsð|  ˆdd¡ˆrü| ¡| ¡}||krX|d krXqqX| ¡| ¡}| ||d |f¡dS)Nrcs@zt ¡j ¡Wn(tjk r:ˆdd7<‚YnXdS)Nrr)r'r(r)r*Z GreenletExitr)Ú EXIT_COUNTrrrsds z3TestLeaks._check_untracked_memory_thread..frtcs2g}tˆƒD] }t ˆ¡}| |¡| ¡q |Sr )r0r'rDr*)Úgletsr3r2rurrrwms     z8TestLeaks._check_untracked_memory_thread..run_itcs,eZdZdZZdZdZ‡‡‡fdd„ZdS)z.ThreadFuncrrr6csbˆ ¡|_t|jƒD]}|jtˆƒƒ7_q|jD]}ˆ dt|ƒ¡q4ˆrTd|_ˆ ¡|_dS)Nzsuspended activer) rxr{r0rvr€ÚtupleZassertInÚstrr})rr3r2©Údeallocate_in_threadrwÚtestrrÚ__call__s  zETestLeaks._check_untracked_memory_thread..ThreadFunc.__call__N)rrrr{r}r€rvr†rrƒrrÚ ThreadFunc{sr‡r<érrzafter attempts %d)rrr0ryr?r@rArBrdÚis_aliver{rzr1r€rvrerxrf)rr„r‡r{r}r|Z thread_funcrCr)rrvr„rsrwr…rÚ_check_untracked_memory_thread]sB     z(TestLeaks._check_untracked_memory_threadcCs|jdddS)NT©r„©rŠrrrrÚItest_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_thread®szSTestLeaks.test_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_threadcCs|jdddS)NFr‹rŒrrrrÚGtest_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_main´szQTestLeaks.test_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_mainN)TF)T)rrrr4r7Ú staticmethodrIrLrPrUrgrirjrrlrmrnryrrrr~rŠrrŽrrrrr")s6    þ ~      Q r"Ú__main__Zunittest)Ú__doc__Ú __future__rrrr rJr9rEr?r'ÚrZ leakcheckrrr ZGREENLET_USE_GCr`Úobjectr r"rÚ __import__ÚmainrrrrÚs&