o
    i3                  
   @   s  d dl Z d dlZd dlZd dl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mZmZ G dd de
jZee
_dd ZG dd	 d	eZG d
d de jZG dd dejZG dd dejZdd ZG dd dejZG dd deZG dd deZG dd deZG dd dejZ zd dl!Z!W n
 e"y   Y dS w ze!#  W dS  e$y Z% zdZ&e'e&j(e%d W Y dZ%[%dS dZ%[%ww )    N)StringIO)resultrunnersignalsc                       s\   e Zd ZdZdZdZdZ fddZ fddZ fddZ	 fd	d
Z
 fddZ  ZS )NumbaTestProgramz
    A TestProgram subclass adding the following options:
    * a -R option to enable reference leak detection
    * a --profile option to enable profiling of the test run

    Currently the options are only added in 3.4+.
    Fc                    s4   | dd | _tjd tt| j|i | d S )NZsuitez:x)popdiscovered_suitesyswarnoptionsappendsuperr   __init__)selfargskwargs	__class__ g/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/llvmlite/tests/customize.pyr   !   s   zNumbaTestProgram.__init__c                    s(   | j d ur| j | _d S tt|   d S N)r   testr   r   createTestsr   r   r   r   r   (   s   
zNumbaTestProgram.createTestsc                    sV   t t|  }| jd u r|jdddddd |jddd	dd
d |jddddd |S )N-Rz	--refleakrefleak
store_truezDetect reference / memory leaks)destactionhelp-mz--multiprocessmultiprocesszParallelize testsz	--profileprofilezProfile the test run)r   r   _getParentArgParser
testRunneradd_argument)r   parserr   r   r   r"   .   s   


z$NumbaTestProgram._getParentArgParserc                    sb   t jdk rd|v r|d d| _d|v r|d d| _tt| | | jdkr/d| _	d S d S )N)      r   Tr   r   )
r	   version_inforemover   r    r   r   	parseArgs	verbositybuffer)r   argvr   r   r   r*   ?   s   




zNumbaTestProgram.parseArgsc              	      s    j rt _ttdstd n	 jd u rtj _ j	r*t
 j j j jd _ fdd} jr{tjtjtjd jd d }tjtjd	}|  z|| W |  td
|f  || d S |  td
|f  || w |  d S )Ngettotalrefcountz^detecting reference leaks requires a debug build of Python, only memory leaks will be detected)r+   failfastr,   c                      s   t t  d S r   )r   r   runTestsr   )r   r   r   r   run_tests_real`   s   z1NumbaTestProgram.runTests.<locals>.run_tests_real__main__r   z.prof)timerz!Writing test profile data into %r)r   RefleakTestRunnerr#   hasattrr	   warningswarnunittestTextTestRunnerr    ParallelTestRunnerr+   r/   r,   r!   ospathsplitextbasenamemodules__file__cProfileZProfiletimeperf_counterenableZruncalldisableprintZ
dump_stats)r   r1   filenamepr   r   r   r0   N   sB   



zNumbaTestProgram.runTests)__name__
__module____qualname____doc__r   r!   r    r   r   r"   r*   r0   __classcell__r   r   r   r   r      s    r   c                  C   s   zt j} W n ty   dd } Y nw zt j}W n ty%   dd }Y nw t jt jt jt jfD ]
}|d ur:|  q0t 	  t
  |  | fS )Nc                   S      dS N*   r   r   r   r   r   func1      z_refleak_cleanup.<locals>.func1c                   S   rN   rO   r   r   r   r   r   func2   rR   z_refleak_cleanup.<locals>.func2)r	   getallocatedblocksAttributeErrorr.   stdoutstderr
__stdout__
__stderr__flush_clear_type_cachegcZcollect)rQ   rS   streamr   r   r   _refleak_cleanup{   s"   

r^   c                   @   s   e Zd ZdS )ReferenceLeakErrorN)rI   rJ   rK   r   r   r   r   r_      s    r_   c                   @   s   e Zd Zdd ZdS )IntPoolc                 C   s   |S r   r   )r   keyr   r   r   __missing__   rR   zIntPool.__missing__N)rI   rJ   rK   rb   r   r   r   r   r`      s    r`   c                       s,   e Zd ZdZdZdd Z fddZ  ZS )RefleakTestResultr&      c                 C   s   | j   | j}| j}dg||  }dg||  }t }tddD ]}||  q!d }}	t|D ]B}t }
||
 |
	 sO| j
|
j
 | j|
j t~
t \}}||krm|||	  ||| < |||  ||| < ||}}	q0||fS )Nr   i8   )r]   rZ   repetitionswarmupr`   ranger   Z
TestResultrunwasSuccessfulfailuresextenderrorsAssertionErrorr^   )r   r   ZrepcountZnwarmup	rc_deltasalloc_deltasZ	_int_pooliZalloc_beforeZ	rc_beforeresZalloc_afterZrc_afterr   r   r   
_huntLeaks   s8   



zRefleakTestResult._huntLeaksc              	      s   z	|  |\}}W n ty   |  rJ Y d S w dd }dd }d}|d|f|d|ffD ]:\}}}	|	|rhd|||t|f }
d	}zt|
 tyT   t }Y nw | jrb| j	
d
||f  | || q.|sutt| | d S d S )Nc                 S   s   t | S r   )anydeltasr   r   r   check_rc_deltas   s   z5RefleakTestResult.addSuccess.<locals>.check_rc_deltasc                 S   s2   d|  d t| k rdS t| tdksdS dS )Nr&   r   T)   r   F)countlensetru   r   r   r   check_alloc_deltas   s
   z8RefleakTestResult.addSuccess.<locals>.check_alloc_deltasFZ
referenceszmemory blocksz%s leaked %s %s, sum=%sTz%s = %r )rs   rn   rj   sumr_   	Exceptionr	   exc_infoZshowAllr]   writeZ
addFailurer   rc   
addSuccess)r   r   ro   rp   rw   r}   failedrv   Z	item_namecheckermsgr   r   r   r   r      s:   	zRefleakTestResult.addSuccess)rI   rJ   rK   rg   rf   rs   r   rM   r   r   r   r   rc      s
    "rc   c                   @   s   e Zd ZeZdS )r4   N)rI   rJ   rK   rc   resultclassr   r   r   r   r4      s    r4   c                 C   s2   t | tjrg }| D ]	}|t| q
|S | gS )z$Expand suite into list of tests
    )
isinstancer8   Z	TestSuiterl   _flatten_suite)r   testsxr   r   r   r      s   r   c                   @   s   e Zd ZdZdd ZdS )ParallelTestResultzA
    A TestResult able to inject results from other results.
    c                 C   sv   | j |j   | j   |  j|j7  _| j|j | j|j | j|j | j	|j	 | j
|j
 dS )zI
        Add the results from the other *result* to this result.
        N)r]   r   getvaluerZ   testsRunrk   rl   rm   skippedexpectedFailuresunexpectedSuccesses)r   r   r   r   r   add_results  s   
zParallelTestResult.add_resultsN)rI   rJ   rK   rL   r   r   r   r   r   r      s    r   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )_MinimalResultz7
    A minimal, picklable TestResult-alike object.
    )rk   rm   r   r   r   r]   
shouldStopr   c                 C   s
   d|_ dS )zR
        Remove any unpicklable attributes from TestCase instance *case*.
        N)Z_outcomeForDoCleanups)r   caser   r   r   
fixup_case  s   
z_MinimalResult.fixup_casec                 C   sp   | j D ]}t| |t|| q| jD ]	\}}| | q| jD ]	\}}| | q| jD ]	\}}| | q,d S r   )	__slots__setattrgetattrr   r   rm   rk   )r   Zoriginal_resultattrr   _r   r   r   r   "  s   
z_MinimalResult.__init__N)rI   rJ   rK   rL   r   r   r   r   r   r   r   r     s
    r   c                   @   s    e Zd ZdZdd Zdd ZdS )_FakeStringIOz:
    A trivial picklable StringIO-alike for Python 2.
    c                 C   s
   || _ d S r   _value)r   valuer   r   r   r   2  s   
z_FakeStringIO.__init__c                 C   s   | j S r   r   r   r   r   r   r   5  s   z_FakeStringIO.getvalueN)rI   rJ   rK   rL   r   r   r   r   r   r   r   -  s    r   c                   @   s.   e Zd ZdZdd Zdd Zejdd ZdS )	_MinimalRunnerzu
    A minimal picklable object able to instantiate a runner in a
    child process and run a test case with it.
    c                 C   s   || _ || _d S r   )
runner_clsrunner_args)r   r   r   r   r   r   r   ?  s   
z_MinimalRunner.__init__c                 C   s   | j }t |d< | jdi |}| }t  t| |j|_|j|_| 	| || W d    n1 s9w   Y  t
|j |_t|S )Nr]   r   )r   r   r   Z_makeResultr   ZinstallHandlerZregisterResultr/   r,   cleanup_objectr   r]   r   r   )r   r   r   r   r   r   r   r   __call__F  s   


z_MinimalRunner.__call__c              	   c   s\    t |j}z|V  W t |j| }|D ]}|j|= qdS t |j| }|D ]}|j|= q&w )zu
        A context manager which cleans up unwanted attributes on a test case
        (or any other object).
        N)r|   __dict__)r   r   Zvanilla_attrsZspurious_attrsnamer   r   r   r   Y  s   


z_MinimalRunner.cleanup_objectN)	rI   rJ   rK   rL   r   r   
contextlibcontextmanagerr   r   r   r   r   r   9  s    r   c                       s4   e Zd ZdZeZdd Zdd Z fddZ  Z	S )r:   z\
    A test runner which delegates the actual running to a pool of child
    processes.
    c                 K   s$   t jj| fi | || _|| _d S r   )r   r9   r   r   r   )r   r   r   r   r   r   r   p  s   
zParallelTestRunner.__init__c                 C   sl   t | j| j}t }|j}z||| jD ]}|| |jr! nq|W |	  |
  S |	  |
  w r   )r   r   r   multiprocessingZPoolZimap_unordered
_test_listr   r   	terminatejoin)r   r   Zchild_runnerpoolimapZchild_resultr   r   r   
_run_inneru  s   


zParallelTestRunner._run_innerc                    s   t || _tt| | jS r   )r   r   r   r:   ri   r   )r   r   r   r   r   ri     s   
zParallelTestRunner.run)
rI   rJ   rK   rL   r   r   r   r   ri   rM   r   r   r   r   r:   h  s    r:   z+Failed to enable faulthandler due to:
{err})err))collectionsr   rA   ior   r\   r;   r   r	   rB   r8   r6   r   r   r   mainr   r^   RuntimeErrorr_   defaultdictr`   ZTextTestResultrc   r9   r4   r   r   objectr   r   r   r:   faulthandlerImportErrorrD   BaseExceptioner   r7   formatr   r   r   r   <module>   sJ   aQ/% 