a
    dÝìhœF  ã                   @   sÚ   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 dd	lmZ dd
lmZ e
jsšJ ‚G dd„ deƒZdd„ ZG dd„ deƒZedkrÖedƒ ¡  dS )z)
Testing scenarios that may have leaked.
é    )Úprint_functionÚabsolute_importÚdivisionNé   )ÚTestCase)ÚPY314)ÚRUNNING_ON_FREETHREAD_BUILD)Úfails_leakcheck)Úignores_leakcheck)ÚRUNNING_ON_MANYLINUXc                   @   s6   e Zd Zeƒ Zdd„ Zdd„ Zdd„ Zedd„ ƒZ	d	S )
ÚHasFinalizerTracksInstancesc                 C   s    t  |¡| _| j t| ƒ¡ d S ©N)ÚsysÚinternÚmsgÚEXTANT_INSTANCESÚaddÚid)Úselfr   © r   úU/var/www/html/requester/venv/lib/python3.9/site-packages/greenlet/tests/test_leaks.pyÚ__init__   s    z$HasFinalizerTracksInstances.__init__c                 C   s   | j  t| ƒ¡ d S r   )r   Úremover   ©r   r   r   r   Ú__del__!   s    z#HasFinalizerTracksInstances.__del__c                 C   s   dt | ƒ| jf S )Nz(<HasFinalizerTracksInstances at 0x%x %r>)r   r   r   r   r   r   Ú__repr__#   s    
ÿz$HasFinalizerTracksInstances.__repr__c                 C   s   | j  ¡  d S r   )r   Úclear)Úclsr   r   r   Úreset'   s    z!HasFinalizerTracksInstances.resetN)
Ú__name__Ú
__module__Ú__qualname__Úsetr   r   r   r   Úclassmethodr   r   r   r   r   r      s   r   c                 C   s   t r| S t| ƒS r   )r   r	   )Úfuncr   r   r   Ú&fails_leakcheck_except_on_free_thraded,   s    r%   c                   @   s¸   e Zd Zdd„ Zdd„ Zedd„ ƒZdd„ Zd	d
„ Zdd„ Z	d'dd„Z
dd„ Zdd„ Zedd„ ƒZedd„ ƒZedd„ ƒZdZdd„ Zedd„ ƒZd(d d!„Zed"d#„ ƒZed$d%„ ƒZd&S ))Ú	TestLeaksc                 C   sJ   d}t  |¡}t dd„ ¡}tdƒD ]}|j|Ž  q$|  t  |¡|¡ d S )N)ÚaÚbÚcc                  W   s   t  ¡ jj| Ž S r   ©ÚgreenletÚ
getcurrentÚparentÚswitch)Úargsr   r   r   Ú<lambda>;   ó    z)TestLeaks.test_arg_refs.<locals>.<lambda>éd   )r   Úgetrefcountr+   Úranger.   ÚassertEqual)r   r/   Zrefcount_beforeÚgÚ_r   r   r   Útest_arg_refs6   s    
ÿzTestLeaks.test_arg_refsc                 C   sh   i }|   t |¡tsdnd¡ t dd„ ¡}tdƒD ]}|jf i |¤Ž q4|   t |¡ts^dnd¡ d S )Né   r   c                  [   s   t  ¡ jjf i | ¤ŽS r   r*   )Zgkwargsr   r   r   r0   E   r1   z+TestLeaks.test_kwarg_refs.<locals>.<lambda>r2   )r5   r   r3   r   r+   r4   r.   )r   Úkwargsr6   r7   r   r   r   Útest_kwarg_refs@   s    ÿzTestLeaks.test_kwarg_refsc                  C   s4   dd„ } t j| d}| ¡  t d¡ | d¡ d S )Nc                   S   s   t  d¡ d S )Nçü©ñÒMbP?)ÚtimeÚsleepr   r   r   r   ÚworkerU   s    z+TestLeaks.__recycle_threads.<locals>.worker©Útargetr<   é
   )Ú	threadingÚThreadÚstartr=   r>   Újoin)r?   Útr   r   r   Z__recycle_threadsM   s
    
zTestLeaks.__recycle_threadsc                    s€   g ‰ ‡ fdd„}t dƒD ]$}tj|d}| ¡  | d¡ ~qt ¡  |  ¡  t ¡  t 	¡  t ¡  ˆ D ]}|  
|ƒ ¡ qjd S )Nc                      s   ˆ   t t ¡ ¡¡ d S r   )ÚappendÚweakrefÚrefr+   r,   r   ©Zggr   r   r?   ^   s    z,TestLeaks.test_threaded_leak.<locals>.workerr9   r@   rB   ©r4   rC   rD   rE   rF   r+   r,   Ú_TestLeaks__recycle_threadsÚgcZcollectZassertIsNone©r   r?   r7   rG   r6   r   rK   r   Útest_threaded_leak\   s    
zTestLeaks.test_threaded_leakc                    s€   g ‰ ‡ fdd„}t dƒD ]$}tj|d}| ¡  | d¡ ~qt ¡  |  ¡  t ¡  t 	¡  t ¡  ˆ D ]}|  
|ƒ ¡ qjd S )Nc                     sN   g  ‰ t  ¡ _‡ fdd„} tdƒD ]}t   | ¡ ¡  q"ˆ t t  ¡ ¡¡ d S )Nc                      s   ˆ   t ¡ ¡ d S r   )rH   r+   r,   r   ©Úllr   r   Ú
additionals   s    zDTestLeaks.test_threaded_adv_leak.<locals>.worker.<locals>.additionalr9   )r+   r,   rR   r4   r.   rH   rI   rJ   )rS   r7   rK   rQ   r   r?   p   s
    z0TestLeaks.test_threaded_adv_leak.<locals>.workerr9   r@   rB   rL   rO   r   rK   r   Útest_threaded_adv_leakn   s    
z TestLeaks.test_threaded_adv_leakc                 C   sL   t j ¡ }|  |d¡ t j d¡ t j ¡ }|  ||¡ |  t jjd¡ d S )Nr   Tr   )r+   Ú	_greenletÚ&get_clocks_used_doing_optional_cleanupZassertGreaterEqualÚenable_optional_cleanupr5   ZassertGreaterZCLOCKS_PER_SEC)r   ÚusedZused2r   r   r   ÚassertClocksUsed…   s    

zTestLeaks.assertClocksUsedTFc           	         sb  t  g ¡sJ ‚t ¡  t ¡  | jtjdd}t ¡ ‰t ¡ ‰ g ‰‡fdd„‰g ‰‡ ‡‡‡‡‡fdd„}tj	|d}| 
¡  ˆ d¡ t ¡  | jtd	d}tˆƒd
ks¬J ‚|  ˆd j¡ ˆd d …= ˆ  ¡  | d¡ ~|  ¡  | jtd	d}| jtjdd}|  ||¡ ˆsFtj ¡ d urF|  ||¡ ˆrF|  tjtƒ ¡ n tj ¡ d ur^|  ¡  d S )NF)Z
exact_kindc                     sL   t dƒ} tj dt dƒ¡ ˆ r6t ¡ jj}|| gƒ nt ¡ j | g¡ d S )NzDELETING STACK OBJECTZtest_leaks_keyzDELETING THREAD STATE)r   r+   rU   Zset_thread_localr,   r-   r.   )ZjdÚs)Úexplicit_reference_to_switchr   r   Úbackground_greenlet­   s    þz6TestLeaks._check_issue251.<locals>.background_greenletc                     sR   t   ˆ¡} ˆ t | j¡¡ ˆ | ¡ |  ¡  ~ ˆ ¡  ˆ  d¡ ˆrNt  ¡  d S )NrB   )	r+   rH   rI   rJ   r-   r.   r"   Úwaitr,   )Zglet)Úbackground_glet_killedÚbackground_glet_runningr\   Úbackground_greenletsÚbg_main_wrefsÚmanually_collect_backgroundr   r   Úbackground_thread½   s    


z4TestLeaks._check_issue251.<locals>.background_threadr@   rB   Tr   r   )rN   Z
is_trackedr   r   r+   r,   Zcount_objectsrC   ÚEventrD   rE   r]   ÚlistÚlenÚassertFalseZdeadr"   rF   Úwait_for_pending_cleanupsÚassertLessEqualrU   rV   r5   r   rY   )	r   rb   r[   Zgreenlets_beforerc   rG   Zlists_beforeZlists_afterZgreenlets_afterr   )r^   r_   r\   r`   ra   r[   rb   r   Ú_check_issue251Ž   sB    


ÿzTestLeaks._check_issue251c                 C   s   |   ¡  d S r   ©rj   r   r   r   r   Ú-test_issue251_killing_cross_thread_leaks_list  s    z7TestLeaks.test_issue251_killing_cross_thread_leaks_listc              	   C   s8   t j d¡ z|  ¡  W t j d¡ nt j d¡ 0 d S )NFT)r+   rU   rW   rj   r   r   r   r   Ú#test_issue251_with_cleanup_disabled  s    
z-TestLeaks.test_issue251_with_cleanup_disabledc                 C   s   | j dd d S )NF©rb   rk   r   r   r   r   Ú4test_issue251_issue252_need_to_collect_in_background  s    z>TestLeaks.test_issue251_issue252_need_to_collect_in_backgroundc              	   C   sB   d| _ tj d¡ z| jdd W tj d¡ ntj d¡ 0 d S )NTFrn   )Zexpect_greenlet_leakr+   rU   rW   rj   r   r   r   r   ÚEtest_issue251_issue252_need_to_collect_in_background_cleanup_disabled+  s
    zOTestLeaks.test_issue251_issue252_need_to_collect_in_background_cleanup_disabledc                 C   s   | j ddd d S )NFT)rb   r[   rk   r   r   r   r   Ú9test_issue251_issue252_explicit_reference_not_collectable4  s    þzCTestLeaks.test_issue251_issue252_explicit_reference_not_collectabler2   c                 C   s@   t jd dksJ ‚t jd d… dk r.|  d¡ tr<|  d¡ d S )Nr   é   r9   )rr   é   zOnly observed on 3.11z!Slow and not worth repeating here)r   Úversion_infoZskipTestr   r   r   r   r   Ú_only_test_some_versions<  s
    
z"TestLeaks._only_test_some_versionsc                    sŽ   |   ¡  dd„ ‰d‰ ‡ ‡fdd„}tdƒD ]
}|ƒ  q*|  ¡ }t| jƒD ]4}t||  ¡ ƒ}|ƒ  |  ¡ }||krH|dkrH q~qH|  ||¡ d S )Nc                   S   s   dS )Nr   r   r   r   r   r   ÚfP  s    z:TestLeaks.test_untracked_memory_doesnt_increase.<locals>.fé'  c                     s    t ˆ ƒD ]} t ˆ¡ ¡  qd S r   )r4   r+   r.   )r7   ©ÚITERrv   r   r   Úrun_itT  s    z?TestLeaks.test_untracked_memory_doesnt_increase.<locals>.run_itrr   r   )ru   r4   Úget_process_ussÚUNTRACK_ATTEMPTSÚmaxri   )r   rz   r7   Ú
uss_beforeÚcountÚ	uss_afterr   rx   r   Ú%test_untracked_memory_doesnt_increaseI  s    z/TestLeaks.test_untracked_memory_doesnt_increasec                    sD  |   ¡  dg‰ ‡ fdd„‰d‰‡‡fdd„‰| ‰G ‡‡‡fdd„dƒ}d  }}t| jƒD ]Â}dˆ d< |ƒ }tj|d	}| ¡  | d
¡ |  | ¡ ¡ |d u r¤|j	}t
||j	ƒ}ˆrØ|  |jd¡ |  ˆ d ˆ|j ¡ ~~ˆsð|  ˆ d d¡ ˆrü|  ¡  |  ¡ }||krX|dkrX qqX|  ¡  |  ¡ }|  ||d|f ¡ d S )Nr   c                      s>   zt  ¡ j ¡  W n& t jy8   ˆ d  d7  < ‚ Y n0 dS )Nr   r   )r+   r,   r-   r.   ZGreenletExitr   )Ú
EXIT_COUNTr   r   rv   r  s    z3TestLeaks._check_untracked_memory_thread.<locals>.frw   c                     s2   g } t ˆ ƒD ] }t ˆ¡}|  |¡ | ¡  q| S r   )r4   r+   rH   r.   )Úgletsr7   r6   rx   r   r   rz   {  s    


z8TestLeaks._check_untracked_memory_thread.<locals>.run_itc                       s,   e Zd Zd ZZdZdZ‡ ‡‡fdd„ZdS )z<TestLeaks._check_untracked_memory_thread.<locals>.ThreadFuncr   r   r9   c                    sb   ˆ  ¡ | _t| jƒD ]}|  jtˆƒ ƒ7  _q| jD ]}ˆ dt|ƒ¡ q4ˆ rTd| _ˆ  ¡ | _d S )Nzsuspended activer   )	r{   r~   r4   ry   rƒ   ÚtupleZassertInÚstrr€   )r   r7   r6   ©Údeallocate_in_threadrz   Útestr   r   Ú__call__  s    

zETestLeaks._check_untracked_memory_thread.<locals>.ThreadFunc.__call__N)r   r    r!   r~   r€   rƒ   ry   r‰   r   r†   r   r   Ú
ThreadFunc‰  s   rŠ   r@   é   r   r   zafter attempts %d)ru   r4   r|   rC   rD   rE   rF   rg   Úis_aliver~   r}   r5   rƒ   ry   rh   r{   ri   )r   r‡   rŠ   r~   r€   r   Zthread_funcrG   r   )r‚   ry   r‡   rv   rz   rˆ   r   Ú_check_untracked_memory_threadk  sB    
z(TestLeaks._check_untracked_memory_threadc                 C   s   | j dd d S )NT©r‡   ©r   r   r   r   r   ÚItest_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_thread¼  s    zSTestLeaks.test_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_threadc                 C   s   | j dd d S )NFrŽ   r   r   r   r   r   ÚGtest_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_mainÂ  s    zQTestLeaks.test_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_mainN)TF)T)r   r    r!   r8   r;   ÚstaticmethodrM   rP   rT   rY   rj   rl   rm   r%   ro   rp   rq   r|   ru   r
   r   r   r   r‘   r   r   r   r   r&   4   s6   


  þ
~




Q
r&   Ú__main__Zunittest)Ú__doc__Ú
__future__r   r   r   r   rN   r=   rI   rC   r+   Ú r   r   r   Z	leakcheckr	   r
   r   ZGREENLET_USE_GCÚobjectr   r%   r&   r   Ú
__import__Úmainr   r   r   r   Ú<module>   s,   
   