HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/local/lib/python3.10/dist-packages/greenlet/tests/__pycache__/test_leaks.cpython-310.pyc
o

���g9D�@s�dZddlmZmZmZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZddl
mZddl
mZddl
mZe
jsAJ�Gd	d
�d
e�ZGdd�de�Zed
kr]ed���dSdS)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�D/usr/local/lib/python3.10/dist-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(<HasFinalizerTracksInstances at 0x%x %r>)rrrrrr�__repr__ s
�z$HasFinalizerTracksInstances.__repr__cCs|j��dSr)r�clear)�clsrrr�reset$sz!HasFinalizerTracksInstances.resetN)
�__name__�
__module__�__qualname__�setrrrr�classmethodrrrrrr
sr
c@s�eZdZdd�Zdd�Zedd��Zdd�Zd	d
�Zdd�Z		
	d'dd�Z
dd�Zdd�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�<lambda>0sz)TestLeaks.test_arg_refs.<locals>.<lambda>�d)r�getrefcountr'�ranger*�assertEqual)rr+�refcount_before�g�_rrr�
test_arg_refs+s
�zTestLeaks.test_arg_refscCsFi}t�dd��}td�D]
}|jdi|��q
|�t�|�d�dS)Nc[st��jjdi|��S)Nrr&)�kwargsrrrr,9sz+TestLeaks.test_kwarg_refs.<locals>.<lambda>r-�r)r'r/r*r0r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.<locals>.worker��targetr8�
)�	threading�Thread�startr9r:�join)r;�trrr�__recycle_threads?s

zTestLeaks.__recycle_threadsc�g��fdd�}td�D]}tj|d�}|��|�d�~qt��|��t��t�	�t���D]}|�
|��q5dS)Ncs��t�t����dSr)�append�weakref�refr'r(r��ggrrr;Psz,TestLeaks.test_threaded_leak.<locals>.workerr6r<r>�r/r?r@rArBr'r(�_TestLeaks__recycle_threads�gc�collect�assertIsNone�rr;r3rCr2rrIr�test_threaded_leakNs
�zTestLeaks.test_threaded_leakcrE)NcsNg�t��_�fdd�}td�D]	}t�|���q��t�t����dS)Ncs��t���dSr)rFr'r(r��llrr�
additionaleszDTestLeaks.test_threaded_adv_leak.<locals>.worker.<locals>.additionalr6)r'r(rSr/r*rFrGrH)rTr3rIrRrr;bs
z0TestLeaks.test_threaded_adv_leak.<locals>.workerr6r<r>rKrPrrIr�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_cleanup�assertGreaterEqual�enable_optional_cleanupr0�
assertGreater�CLOCKS_PER_SEC)r�used�used2rrr�assertClocksUsedws

zTestLeaks.assertClocksUsedTFc	s`t�g�sJ�t��t��|jtjdd�}t���t���g��fdd��g�������fdd�}tj	|d�}|�
���d�t��|jtd	d�}t
��d
ksVJ�|��dj��dd�=���|�d�~|��|jtd	d�}|jtjdd�}|�||��s�tj��dur�|�||��r�|�tjt��n	tj��dur�|��dSdS)NF)�
exact_kindcsNtd�}tj�dtd���rt��jj}||g�dSt��j�|g�dS)NzDELETING STACK OBJECT�test_leaks_keyzDELETING THREAD STATE)r
r'rV�set_thread_localr(r)r*)�jd�s)�explicit_reference_to_switchrr�background_greenlet�s�z6TestLeaks._check_issue251.<locals>.background_greenletcsVt���}��t�|j����|�|��~�����d��r)t��dSdS)Nr>)	r'rFrGrHr)r*r �waitr()�glet)�background_glet_killed�background_glet_runningre�background_greenlets�
bg_main_wrefs�manually_collect_backgroundrr�background_thread�s


�z4TestLeaks._check_issue251.<locals>.background_threadr<r>Trr)rM�
is_trackedr
rr'r(�
count_objectsr?�Eventr@rArf�list�len�assertFalse�deadr rB�wait_for_pending_cleanups�assertLessEqualrVrWr0rr^)	rrlrd�greenlets_beforermrC�lists_before�lists_after�greenlets_afterr)rhrirerjrkrdrlr�_check_issue251�sF


�
�zTestLeaks._check_issue251cCs|��dSr�r{rrrr�-test_issue251_killing_cross_thread_leaks_list�sz7TestLeaks.test_issue251_killing_cross_thread_leaks_listc	Cs6tj�d�z
|��Wtj�d�dStj�d�w)NFT)r'rVrYr{rrrr�#test_issue251_with_cleanup_disableds
z-TestLeaks.test_issue251_with_cleanup_disabledcC�|jdd�dS)NF�rlr|rrrr�4test_issue251_issue252_need_to_collect_in_backgroundsz>TestLeaks.test_issue251_issue252_need_to_collect_in_backgroundc	Cs@d|_tj�d�z|jdd�Wtj�d�dStj�d�w)NTFr�)�expect_greenlet_leakr'rVrYr{rrrr�Etest_issue251_issue252_need_to_collect_in_background_cleanup_disableds
zOTestLeaks.test_issue251_issue252_need_to_collect_in_background_cleanup_disabledcCs|jddd�dS)NFT)rlrdr|rrrr�9test_issue251_issue252_explicit_reference_not_collectable&s
�zCTestLeaks.test_issue251_issue252_explicit_reference_not_collectabler-cCsDtjddks	J�tjdd�dkr|�d�tr |�d�dSdS)Nr�r6)r��zOnly observed on 3.11z!Slow and not worth repeating here)r�version_info�skipTestr	rrrr�_only_test_some_versions.s
�z"TestLeaks._only_test_some_versionscs�|��dd��d���fdd�}td�D]}|�q|��}t|j�D]}t||���}|�|��}||kr>|dkr>nq$|�||�dS)NcSsdS)Nrrrrrr�fBsz:TestLeaks.test_untracked_memory_doesnt_increase.<locals>.f�'cs t��D]	}t�����qdSr)r/r'r*)r3��ITERr�rr�run_itFs�z?TestLeaks.test_untracked_memory_doesnt_increase.<locals>.run_itr�r)r�r/�get_process_uss�UNTRACK_ATTEMPTS�maxrv)rr�r3�
uss_before�count�	uss_afterrr�r�%test_untracked_memory_doesnt_increase;s�z/TestLeaks.test_untracked_memory_doesnt_increasecsB|��dg��fdd��d���fdd��|�G���fdd�d�}d}}t|j�D]`}d�d<|�}tj|d	�}|��|�d
�|�|���|durR|j	}t
||j	�}�rl|�|jd�|��d�|j
�~~�sx|��dd��r~|��|��}||kr�|dkr�nq,|��|��}|�||d
|f�dS)Nrcs8z
t��j��WdStjy�dd7<�w)Nrr)r'r(r)r*�GreenletExitr)�
EXIT_COUNTrrr�ds��z3TestLeaks._check_untracked_memory_thread.<locals>.fr�cs2g}t��D]}t���}|�|�|��q|Sr)r/r'rFr*)�gletsr3r2r�rrr�ms


z8TestLeaks._check_untracked_memory_thread.<locals>.run_itcs,eZdZdZZdZdZ���fdd�ZdS)z<TestLeaks._check_untracked_memory_thread.<locals>.ThreadFuncrrr6csb���|_t|j�D]}|jt���7_q
|jD]
}��dt|��q�r*d|_���|_dS)Nzsuspended activer)	r�r�r/r�r��tuple�assertIn�strr�)rr3r2��deallocate_in_threadr��testrr�__call__s

zETestLeaks._check_untracked_memory_thread.<locals>.ThreadFunc.__call__N)rrrr�r�r�r�r�rr�rr�
ThreadFunc{s
r�r<�rrzafter attempts %d)r�r/r�r?r@rArBrs�is_aliver�r�r0r�r�rur�rv)rr�r�r�r�r��thread_funcrCr)r�r�r�r�r�r�r�_check_untracked_memory_thread]sD
�z(TestLeaks._check_untracked_memory_threadcCr)NT�r��r�rrrr�Itest_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_thread��zSTestLeaks.test_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_threadcCr)NFr�r�rrrr�Gtest_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_main�r�zQTestLeaks.test_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_mainN)TF)T)rrrr4r7�staticmethodrLrQrUr^r{r}r~rr�r�r�r�r�rr�r�r�r�rrrrr")s8




�~




Q
r"�__main__�unittest)�__doc__�
__future__rrrrrMr9rGr?r'�r�	leakcheckrrr	�GREENLET_USE_GC�objectr
r"r�
__import__�mainrrrr�<module>s*
�