o
    ڭh2E                     @   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 e
jsGJ G d
d deZG dd deZedkrced  dS dS )z)
Testing scenarios that may have leaked.
    )print_functionabsolute_importdivisionN   )TestCase)PY314)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   ^/var/www/html/stock_analysis/be/venv/lib/python3.10/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>1   s    z)TestLeaks.test_arg_refs.<locals>.<lambda>d   )r   getrefcountr(   ranger+   assertEqual)r   r,   refcount_beforeg_r   r   r   test_arg_refs,   s   
zTestLeaks.test_arg_refsc                 C   sn   i }|  t|tsdnd tdd }tdD ]
}|jdi | q|  t|ts2d d S d d S )N   r   c                  [   s   t  jjdi | S )Nr   r'   )gkwargsr   r   r   r-   ;   s    z+TestLeaks.test_kwarg_refs.<locals>.<lambda>r.   r   )r1   r   r/   r   r(   r0   r+   )r   kwargsr3   r4   r   r   r   test_kwarg_refs6   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 )NMbP?)timesleepr   r   r   r   workerK   s   z+TestLeaks.__recycle_threads.<locals>.workertargetr:   
   )	threadingThreadstartr;   r<   join)r=   tr   r   r   __recycle_threadsC   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   ggr   r   r=   T   s   z,TestLeaks.test_threaded_leak.<locals>.workerr6   r>   r@   r0   rA   rB   rC   rD   r(   r)   _TestLeaks__recycle_threadsgccollectassertIsNoner   r=   r4   rE   r3   r   rK   r   test_threaded_leakR   s   
zTestLeaks.test_threaded_leakc                    rG   )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   
additionali   s   zDTestLeaks.test_threaded_adv_leak.<locals>.worker.<locals>.additionalr6   )r(   r)   rU   r0   r+   rH   rI   rJ   )rV   r4   rK   rT   r   r=   f   s
   z0TestLeaks.test_threaded_adv_leak.<locals>.workerr6   r>   r@   rM   rR   r   rK   r   test_threaded_adv_leakd   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_cleanupassertGreaterEqualenable_optional_cleanupr1   assertGreaterCLOCKS_PER_SEC)r   used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}| || stj d ur| || r| tjt  n	 tj d ur|   d S d S )NF)
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 OBJECTtest_leaks_keyzDELETING THREAD STATE)r   r(   rX   set_thread_localr)   r*   r+   )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(   rH   rI   rJ   r*   r+   r!   waitr)   )glet)background_glet_killedbackground_glet_runningrg   background_greenletsbg_main_wrefsmanually_collect_backgroundr   r   background_thread   s   


z4TestLeaks._check_issue251.<locals>.background_threadr>   r@   Tr   r   )rO   
is_trackedr   r   r(   r)   count_objectsrA   EventrB   rC   rh   listlenassertFalsedeadr!   rD   wait_for_pending_cleanupsassertLessEqualrX   rY   r1   r   r`   )	r   rn   rf   greenlets_beforero   rE   lists_beforelists_aftergreenlets_afterr   )rj   rk   rg   rl   rm   rf   rn   r   _check_issue251   sH   


zTestLeaks._check_issue251c                 C   s   |    d S r   r}   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(   rX   r[   r}   r   r   r   r   #test_issue251_with_cleanup_disabled  s   
z-TestLeaks.test_issue251_with_cleanup_disabledc                 C      | j dd d S )NFrn   r~   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 )NTFr   )expect_greenlet_leakr(   rX   r[   r}   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)rn   rf   r~   r   r   r   r   9test_issue251_issue252_explicit_reference_not_collectable*  s   
zCTestLeaks.test_issue251_issue252_explicit_reference_not_collectabler.   c                 C   sD   t jd dks	J t jd d dk r| d tr | d d S d S )Nr      r6   )r      zOnly observed on 3.11z!Slow and not worth repeating here)r   version_infoskipTestr
   r   r   r   r   _only_test_some_versions2  s   
z"TestLeaks._only_test_some_versionsc                    s   |    dd d  fdd}tdD ]}|  q|  }t| jD ]}t||  }|  |  }||kr>|dkr> nq$| || d S )Nc                   S   s   dS )Nr   r   r   r   r   r   fF  s   z:TestLeaks.test_untracked_memory_doesnt_increase.<locals>.f'  c                     s    t  D ]	} t  qd S r   )r0   r(   r+   )r4   ITERr   r   r   run_itJ  s   z?TestLeaks.test_untracked_memory_doesnt_increase.<locals>.run_itr   r   )r   r0   get_process_ussUNTRACK_ATTEMPTSmaxrx   )r   r   r4   
uss_beforecount	uss_afterr   r   r   %test_untracked_memory_doesnt_increase?  s   z/TestLeaks.test_untracked_memory_doesnt_increasec                    sB  |    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 rR|j	}t
||j	}rl| |jd |  d |j  ~~sx|  d d r~|   |  }||kr|dkr nq,|   |  }| ||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+   GreenletExitr   )
EXIT_COUNTr   r   r   h  s   z3TestLeaks._check_untracked_memory_thread.<locals>.fr   c                     s2   g } t  D ]}t}| | |  q| S r   )r0   r(   rH   r+   )gletsr4   r3   r   r   r   r   q  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                    sb     | _t| jD ]}|  jt 7  _q
| jD ]
}dt| q r*d| _  | _d S )Nzsuspended activer   )	r   r   r0   r   r   tupleassertInstrr   )r   r4   r3   deallocate_in_threadr   testr   r   __call__  s   

zETestLeaks._check_untracked_memory_thread.<locals>.ThreadFunc.__call__N)r   r   r    r   r   r   r   r   r   r   r   r   
ThreadFunc  s
    r   r>      r   r   zafter attempts %d)r   r0   r   rA   rB   rC   rD   ru   is_aliver   r   r1   r   r   rw   r   rx   )r   r   r   r   r   r   thread_funcrE   r   )r   r   r   r   r   r   r   _check_untracked_memory_threada  sD   
z(TestLeaks._check_untracked_memory_threadc                 C   r   )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   r   )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    r5   r9   staticmethodrN   rS   rW   r`   r}   r   r   r   r   r   r   r   r   r	   r   r   r   r   r   r   r   r   r#   *   s8    



~




Q
r#   __main__unittest)__doc__
__future__r   r   r   r   rO   r;   rI   rA   r(    r   r   	leakcheckr   r	   r
   GREENLET_USE_GCobjectr   r#   r   
__import__mainr   r   r   r   <module>   s,   
   