o
    E6÷dE  ã                   @   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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 zdd
lmZ W n	 eyW   Y nw ejs]J ‚G dd„ deƒZG dd„ deƒZedkryedƒ ¡  dS dS )z)
Testing scenarios that may have leaked.
é    )Úprint_functionÚabsolute_importÚdivisionNé   )ÚTestCase)Úfails_leakcheck)Úignores_leakcheck)ÚRUNNING_ON_GITHUB_ACTIONS)ÚRUNNING_ON_MANYLINUX)Úinternc                   @   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)r   ÚmsgÚEXTANT_INSTANCESÚaddÚid)Úselfr   © r   úpC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\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                   @   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>6   s    z)TestLeaks.test_arg_refs.<locals>.<lambda>éd   )ÚsysÚgetrefcountr'   Úranger*   ÚassertEqual)r   r+   Zrefcount_beforeÚgÚ_r   r   r   Útest_arg_refs1   s   
ÿzTestLeaks.test_arg_refsc                 C   sF   i }t   dd„ ¡}tdƒD ]
}|jdi |¤Ž q|  t |¡d¡ d S )Nc                  [   s   t  ¡ jjdi | ¤ŽS )Nr   r&   )Úkwargsr   r   r   r,   ?   s    z+TestLeaks.test_kwarg_refs.<locals>.<lambda>r-   é   r   )r'   r0   r*   r1   r.   r/   )r   r5   r2   r3   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   ÚworkerM   s   z+TestLeaks.__recycle_threads.<locals>.worker©Útargetr8   é
   )Ú	threadingÚThreadÚstartr9   r:   Újoin)r;   Útr   r   r   Z__recycle_threadsE   s
   
zTestLeaks.__recycle_threadsc                    ó€   g ‰ ‡ fdd„}t dƒD ]}tj|d}| ¡  | d¡ ~qt ¡  |  ¡  t ¡  t 	¡  t ¡  ˆ D ]}|  
|ƒ ¡ q5d S )Nc                      s   ˆ   t t ¡ ¡¡ d S r   )ÚappendÚweakrefÚrefr'   r(   r   ©Zggr   r   r;   V   s   z,TestLeaks.test_threaded_leak.<locals>.workerr6   r<   r>   ©r0   r?   r@   rA   rB   r'   r(   Ú_TestLeaks__recycle_threadsÚgcZcollectZassertIsNone©r   r;   r3   rC   r2   r   rH   r   Útest_threaded_leakT   s   
ÿzTestLeaks.test_threaded_leakc                    rD   )Nc                     sN   g  ‰ t  ¡ _‡ fdd„} tdƒD ]	}t   | ¡ ¡  qˆ t t  ¡ ¡¡ d S )Nc                      s   ˆ   t ¡ ¡ d S r   )rE   r'   r(   r   ©Úllr   r   Ú
additionalk   s   zDTestLeaks.test_threaded_adv_leak.<locals>.worker.<locals>.additionalr6   )r'   r(   rO   r0   r*   rE   rF   rG   )rP   r3   rH   rN   r   r;   h   s
   z0TestLeaks.test_threaded_adv_leak.<locals>.workerr6   r<   r>   rI   rL   r   rH   r   Útest_threaded_adv_leakf   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_cleanupr1   ZassertGreaterZCLOCKS_PER_SEC)r   ÚusedZused2r   r   r   ÚassertClocksUsed}   s   

zTestLeaks.assertClocksUsedTFc           	         s`  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sVJ ‚|  ˆd j¡ ˆd d …= ˆ  ¡  | d¡ ~|  ¡  | jtd	d}| jtjdd}|  ||¡ ˆs tj ¡ d ur |  ||¡ ˆrŸ|  tjtƒ ¡ n	 tj ¡ d ur®|  ¡  d S d S )NF)Z
exact_kindc                     sN   t dƒ} tj dt dƒ¡ ˆ rt ¡ jj}|| gƒ d S t ¡ j | g¡ d S )NzDELETING STACK OBJECTZtest_leaks_keyzDELETING THREAD STATE)r   r'   rR   Zset_thread_localr(   r)   r*   )ZjdÚs)Úexplicit_reference_to_switchr   r   Úbackground_greenlet¥   s   þz6TestLeaks._check_issue251.<locals>.background_greenletc                     sV   t   ˆ¡} ˆ t | j¡¡ ˆ | ¡ |  ¡  ~ ˆ ¡  ˆ  d¡ ˆr)t  ¡  d S d S )Nr>   )	r'   rE   rF   rG   r)   r*   r    Úwaitr(   )Zglet)Úbackground_glet_killedÚbackground_glet_runningrY   Úbackground_greenletsÚbg_main_wrefsÚmanually_collect_backgroundr   r   Úbackground_threadµ   s   


ÿz4TestLeaks._check_issue251.<locals>.background_threadr<   r>   Tr   r   )rK   Z
is_trackedr   r   r'   r(   Zcount_objectsr?   ÚEventr@   rA   rZ   ÚlistÚlenÚassertFalseZdeadr    rB   Úwait_for_pending_cleanupsÚassertLessEqualrR   rS   r1   r   rV   )	r   r_   rX   Zgreenlets_beforer`   rC   Zlists_beforeZlists_afterZgreenlets_afterr   )r[   r\   rY   r]   r^   rX   r_   r   Ú_check_issue251†   sD   


€ÿzTestLeaks._check_issue251c                 C   s   |   ¡  d S r   ©rg   r   r   r   r   Ú-test_issue251_killing_cross_thread_leaks_list  s   z7TestLeaks.test_issue251_killing_cross_thread_leaks_listc              	   C   s6   t j d¡ z|  ¡  W t j d¡ d S t j d¡ w )NFT)r'   rR   rT   rg   r   r   r   r   Ú#test_issue251_with_cleanup_disabled  s   
z-TestLeaks.test_issue251_with_cleanup_disabledc                 C   ó   | j dd d S )NF©r_   rh   r   r   r   r   Ú4test_issue251_issue252_need_to_collect_in_background  s   z>TestLeaks.test_issue251_issue252_need_to_collect_in_backgroundc              	   C   s@   d| _ tj d¡ z| jdd W tj d¡ d S tj d¡ w )NTFrl   )Zexpect_greenlet_leakr'   rR   rT   rg   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)r_   rX   rh   r   r   r   r   Ú9test_issue251_issue252_explicit_reference_not_collectable+  s   
þzCTestLeaks.test_issue251_issue252_explicit_reference_not_collectabler-   c                 C   s\   t jd dkrt jd d… dk r|  d¡ t jd dkr#tr#|  d¡ tr,|  d¡ d S d S )Nr   é   r6   )rp   é   zOnly observed on 3.11z!Hard to get a stable pattern herez!Slow and not worth repeating here)r.   Úversion_infoZskipTestr	   r
   r   r   r   r   Ú_only_test_some_versions3  s    

ÿz"TestLeaks._only_test_some_versionsc                    s    |   ¡  dd„ ‰d‰ ‡ ‡fdd„}tdƒD ]}|ƒ  qt ¡  ¡ j}t| jƒD ] }t|t ¡  ¡ jƒ}|ƒ  t ¡  ¡ j}||krG|dkrG nq'|  ||¡ d S )Nc                   S   s   dS )Nr   r   r   r   r   r   ÚfG  s   z:TestLeaks.test_untracked_memory_doesnt_increase.<locals>.fé'  c                     s    t ˆ ƒD ]	} t ˆ¡ ¡  qd S r   )r0   r'   r*   )r3   ©ÚITERrt   r   r   Úrun_itK  s   ÿz?TestLeaks.test_untracked_memory_doesnt_increase.<locals>.run_itrp   r   )	rs   r0   ÚpsutilÚProcessÚmemory_full_infoÚussÚUNTRACK_ATTEMPTSÚmaxrf   )r   rx   r3   Ú
uss_beforeÚcountÚ	uss_afterr   rv   r   Ú%test_untracked_memory_doesnt_increase@  s   €z/TestLeaks.test_untracked_memory_doesnt_increasec                    sN  |   ¡  dg‰ ‡ fdd„‰d‰‡‡fdd„‰| ‰G ‡‡‡fdd„dƒ}d  }}t| jƒD ]c}dˆ d< |ƒ }tj|d	}| ¡  | d
¡ |  | ¡ ¡ |d u rR|j	}t
||j	ƒ}ˆrl|  |jd¡ |  ˆ d ˆ|j ¡ ~~ˆsx|  ˆ d d¡ ˆr~|  ¡  t ¡  ¡ j}||kr|dkr nq,|  ¡  t ¡  ¡ j}|  ||d|f ¡ d S )Nr   c                      s8   z
t  ¡ j ¡  W dS  t jy   ˆ d  d7  < ‚ w )Nr   r   )r'   r(   r)   r*   ZGreenletExitr   )Ú
EXIT_COUNTr   r   rt   i  s   ýþz3TestLeaks._check_untracked_memory_thread.<locals>.fru   c                     s2   g } t ˆ ƒD ]}t ˆ¡}|  |¡ | ¡  q| S r   )r0   r'   rE   r*   )Úgletsr3   r2   rv   r   r   rx   r  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   r6   c                    sn   t  ¡  ¡ j| _t| jƒD ]}|  jtˆƒ ƒ7  _q| jD ]
}ˆ 	dt
|ƒ¡ qˆ r-d| _t  ¡  ¡ j| _d S )Nzsuspended activer   )ry   rz   r{   r|   r   r0   rw   r„   ÚtupleZassertInÚstrr   )r   r3   r2   ©Údeallocate_in_threadrx   Útestr   r   Ú__call__„  s   
zETestLeaks._check_untracked_memory_thread.<locals>.ThreadFunc.__call__N)r   r   r   r   r   r„   rw   rŠ   r   r‡   r   r   Ú
ThreadFunc€  s
    r‹   r<   é   r   r   zafter attempts %d)rs   r0   r}   r?   r@   rA   rB   rd   Úis_aliver   r~   r1   r„   rw   re   ry   rz   r{   r|   rf   )r   rˆ   r‹   r   r   r€   Zthread_funcrC   r   )rƒ   rw   rˆ   rt   rx   r‰   r   Ú_check_untracked_memory_threadb  sD   
€z(TestLeaks._check_untracked_memory_threadc                 C   rk   )NT©rˆ   ©rŽ   r   r   r   r   ÚItest_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_thread³  ó   zSTestLeaks.test_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_threadc                 C   rk   )NFr   r   r   r   r   r   ÚGtest_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_main¹  r’   zQTestLeaks.test_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_mainN)TF)T)r   r   r   r4   r7   ÚstaticmethodrJ   rM   rQ   rV   rg   ri   rj   r   rm   rn   ro   r}   rs   r   r‚   rŽ   r‘   r“   r   r   r   r   r"   /   s8    




þ}




Q
r"   Ú__main__Zunittest)Ú__doc__Ú
__future__r   r   r   r.   rK   r9   rF   r?   ry   r'   Ú r   Z	leakcheckr   r   r	   r
   r   ÚImportErrorZGREENLET_USE_GCÚobjectr   r"   r   Ú
__import__Úmainr   r   r   r   Ú<module>   s8   þ
   ÿ