o
    Zh>#                     @   s  d Z ddlmZ ddlmZ ddlm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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dd dkZejdZG dd deZG dd dedejfi ZdS )z
Tests for greenlet.

    )absolute_import)division)print_functionN)collect)get_objects)active_count)sleep)time)greenlet)
getcurrent)get_pending_cleanup_count)get_total_main_greenlets   )	leakcheck   )      winc                   @   s   e Zd Zdd ZdS )TestCaseMetaClassc                 C   s\   d}t | D ]\}}|dr%t|r%|| |r!t|}|||< qt| |||S )NTtest)	listitems
startswithcallablepopr   Zwrap_refcounttype__new__)cls	classnamebasesZ	classDictZcheck_totalrefcountkeyvalue r"   N/var/www/html/lang_env/lib/python3.10/site-packages/greenlet/tests/__init__.pyr   #   s   

zTestCaseMetaClass.__new__N)__name__
__module____qualname__r   r"   r"   r"   r#   r       s    r   c                       s   e Zd ZdZdZ		dddZedfddZd	Zd	Z	d	Z
d
Zdd Z fddZ fddZdd Zdd ZdddZdddZ  ZS )TestCasegMbP?r   Nc                 C   s   |p| j }|p	| j}| j}t| t | j }t s't |ks'| jsUt	 |krUt| t |krEt
d t
d|dt  t
d|dt	  nt s't |ks'| jsUt	 |ks't  d S )NzTime limit exceeded.zThreads: Waiting for onlyz-->zMGlets : Waiting for only)threads_before_testmain_greenlets_before_testcleanup_attempt_sleep_durationr   r	   cleanup_max_sleep_secondsr   active_thread_countexpect_greenlet_leakr   printr   )selfZinitial_active_threadsZinitial_main_greenletsZ
sleep_timeZ
quit_afterr"   r"   r#   wait_for_pending_cleanups>   s8   








	z"TestCase.wait_for_pending_cleanupsTc                    sH   t dD ]}t  q|rt fddt D S t fddt D S )Nr   c                 3   s     | ]}t | u rd V  qdS r   N)r   .0xkindr"   r#   	<genexpr>f   s    z)TestCase.count_objects.<locals>.<genexpr>c                 3   s    | ]
}t | rd V  qdS r1   )
isinstancer2   r5   r"   r#   r7   l   s    
)ranger   sumr   )r/   r6   Z
exact_kind_r"   r5   r#   count_objects`   s   zTestCase.count_objectsr   Fc                 C   s   |  tdS )zJ
        Find all the greenlets and subclasses tracked by the GC.
        F)r<   RawGreenletr/   r"   r"   r#   count_greenletsw   s   zTestCase.count_greenletsc                    s>   t    t  t | _t | _| | j| j |  | _	d S N)
supersetUpr   r,   r(   r   r)   r0   r?   greenlets_before_testr>   	__class__r"   r#   rB   }   s   
zTestCase.setUpc                    s.   t | ddrd S | | j| j t   d S )NZskipTearDownF)getattrr0   r(   r)   rA   tearDownr>   rD   r"   r#   rG      s   zTestCase.tearDownc                 C   s(   dd l }ts|j |j f}|S d}|S )Nr   )r   l   	   l       )signalWINSIGABRTSIGSEGV)r/   rH   Zexpected_exitr"   r"   r#   ,get_expected_returncodes_for_aborted_process   s   %z5TestCase.get_expected_returncodes_for_aborted_processc              
   C   s6   zt   jW S  ty } ztd|d}~ww )a  
        Return the current process's USS in bytes.

        uss is available on Linux, macOS, Windows. Also known as
        "Unique Set Size", this is the memory which is unique to a
        process and which would be freed if the process was terminated
        right now.

        If this is not supported by ``psutil``, this raises the
        :exc:`unittest.SkipTest` exception.
        zuss not supportedN)psutilProcessZmemory_full_infoZussAttributeErrorunittestZSkipTest)r/   er"   r"   r#   get_process_uss   s   zTestCase.get_process_ussc              
   C   s   dd l }dd l}|j|jt|}z|jtj|gd|j	dW S  |j
yF } z|rAtd td| td t|j td  d }~ww )Nr   zutf-8)encodingstderrz-----zFailed to run scriptz~~~~~z------)
subprocessospathjoindirname__file__check_outputsys
executableSTDOUTCalledProcessErrorr.   output)r/   script_nameshow_outputrU   rV   scriptexr"   r"   r#   
run_script   s*   


zTestCase.run_scriptc                 C   sl   dd l }| |j}| j|dd}|}W d    n1 sw   Y  |d u r+|  }| |jj| |jS )Nr   F)rb   )rU   assertRaisesr_   re   rL   ZassertIn	exception
returncode)r/   ra   Z	exitcodesrU   excr`   Z__traceback_info__r"   r"   r#   assertScriptRaises   s   zTestCase.assertScriptRaises)NN)Tr@   )r$   r%   r&   r*   r+   r0   r   r<   rC   r(   r)   r-   r?   rB   rG   rL   rR   re   rj   __classcell__r"   r"   rD   r#   r'   6   s$    
"
-
r'   ZNewBase) __doc__
__future__r   r   r   r\   rP   gcr   r   	threadingr   r,   r	   r   rM   r
   r=   r   Zgreenlet._greenletr   r    r   version_infoZPY312platformr   rI   r   r   r'   r"   r"   r"   r#   <module>   s2   
