B
    Ꮚ\!3              
   @   s  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Zd dlZd dl	Z	d dlm
Z
mZmZ ddl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 yd dl!Z!W n e"k
rT   Y nJX ye!#  W n: e$k
r Z% zdZ&e	'e&j(e%d W ddZ%[%X Y nX dS )    N)resultrunnersignals   )StringIOc                   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                s0   | dd | _tjd tt| j|| d S )NZsuitez:x)popdiscovered_suitesyswarnoptionsappendsuperr   __init__)selfargskwargs)	__class__ 7lib/python3.7/site-packages/llvmlite/tests/customize.pyr   "   s    zNumbaTestProgram.__init__c                s&   | j d k	r| j | _ntt|   d S )N)r	   testr   r   createTests)r   )r   r   r   r   *   s    

zNumbaTestProgram.createTestsc                sV   t t|  }| jd kr,|jdddddd |jddd	dd
d |jddddd |S )Nz-Rz	--refleakrefleak
store_truezDetect reference / memory leaks)destactionhelpz-mz--multiprocessmultiprocesszParallelize testsz	--profileprofilezProfile the test run)r   r   _getParentArgParser
testRunneradd_argument)r   parser)r   r   r   r   0   s    


z$NumbaTestProgram._getParentArgParserc                s^   t jdk r:d|kr"|d d| _d|kr:|d d| _tt| | | jdkrZd| _	d S )N)      z-RTz-mr   )
r
   version_inforemover   r   r   r   	parseArgs	verbositybuffer)r   argv)r   r   r   r&   A   s    



zNumbaTestProgram.parseArgsc          	      s    j r"t _ttds4td n jd kr4tj _ j	rTt
 j j j jd _ fdd} jrtjtjtjd jd d }tjtjd	}|  z|| W d |  td
|f  || X n|  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 )N)r   r   runTestsr   )r   r   r   r   run_tests_reala   s    z1NumbaTestProgram.runTests.<locals>.run_tests_real__main__r   z.prof)Z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timeZperf_counterenableZruncalldisableprintZ
dump_stats)r   r-   filenamep)r   )r   r   r,   P   s0    


zNumbaTestProgram.runTests)__name__
__module____qualname____doc__r   r   r   r   r   r   r&   r,   __classcell__r   r   )r   r   r      s   r   c              C   s   y
t j} W n tk
r&   dd } Y nX y
t j}W n tk
rN   dd }Y nX x,t jt jt jt jfD ]}|d k	rf|  qfW t 	  t
  |  | fS )Nc               S   s   dS )N*   r   r   r   r   r   <lambda>   s    z"_refleak_cleanup.<locals>.<lambda>c               S   s   dS )NrH   r   r   r   r   r   rI      s    )r
   getallocatedblocksAttributeErrorr*   stdoutstderr
__stdout__
__stderr__flush_clear_type_cachegcZcollect)Zfunc1Zfunc2streamr   r   r   _refleak_cleanup|   s    

rT   c               @   s   e Zd ZdS )ReferenceLeakErrorN)rC   rD   rE   r   r   r   r   rU      s   rU   c               @   s   e Zd Zdd ZdS )IntPoolc             C   s   |S )Nr   )r   keyr   r   r   __missing__   s    zIntPool.__missing__N)rC   rD   rE   rX   r   r   r   r   rV      s   rV   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 }xtddD ]}||  qDW xt|D ]}t }|| |	 s| j
|j
 | j|j t~t \}	}
||kr||
|  ||| < ||	|  ||| < |	|
 }}q^W ||fS )Nr   i8   )rS   rP   repetitionswarmuprV   ranger   Z
TestResultrunwasSuccessfulfailuresextenderrorsAssertionErrorrT   )r   r   ZrepcountZnwarmup	rc_deltasalloc_deltasZ	_int_pooliZresZalloc_afterZrc_afterZ	rc_beforeZalloc_beforer   r   r   
_huntLeaks   s,    


zRefleakTestResult._huntLeaksc          	      s   y|  |\}}W n  tk
r2   |  r.td S X dd }dd }d}x|d|f|d|fgD ]z\}}}	|	|r^d|||t|f }
d	}yt|
W n tk
r   t }Y nX | jr| j	
d
||f  | || q^W |stt| | d S )Nc             S   s   t | S )N)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set)rj   r   r   r   check_alloc_deltas   s
    z8RefleakTestResult.addSuccess.<locals>.check_alloc_deltasFZ
referenceszmemory blocksz%s leaked %s %s, sum=%sTz%s = %r )rh   rd   r`   sumrU   	Exceptionr
   exc_infoZshowAllrS   writeZ
addFailurer   rY   
addSuccess)r   r   re   rf   rk   rq   Zfailedrj   Z	item_nameZcheckermsgrt   )r   r   r   rv      s0    	zRefleakTestResult.addSuccess)rC   rD   rE   r]   r\   rh   rv   rG   r   r   )r   r   rY      s   rY   c               @   s   e Zd ZeZdS )r/   N)rC   rD   rE   rY   resultclassr   r   r   r   r/      s   r/   c             C   s:   t | tjr0g }x| D ]}|t| qW |S | gS dS )z$Expand suite into list of tests
    N)
isinstancer3   Z	TestSuiterb   _flatten_suite)r   Ztestsxr   r   r   rz      s    
rz   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)rS   ru   getvaluerP   testsRunra   rb   rc   skippedexpectedFailuresunexpectedSuccesses)r   r   r   r   r   add_results   s    
zParallelTestResult.add_resultsN)rC   rD   rE   rF   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.
    )ra   rc   r   r   r   rS   
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   s   x | j D ]}t| |t|| qW x| jD ]\}}| | q*W x| jD ]\}}| | qHW x| jD ]\}}| | qfW d S )N)	__slots__setattrgetattrr   r   rc   ra   )r   Zoriginal_resultattrr   _r   r   r   r     s    z_MinimalResult.__init__N)rC   rD   rE   rF   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 )N)_value)r   valuer   r   r   r   -  s    z_FakeStringIO.__init__c             C   s   | j S )N)r   )r   r   r   r   r}   0  s    z_FakeStringIO.getvalueN)rC   rD   rE   rF   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 )N)
runner_clsrunner_args)r   r   r   r   r   r   r   :  s    z_MinimalRunner.__init__c          	   C   s|   | j }t |d< | jf |}| }t  t| |j|_|j|_| 	| || W d Q R X t
|j |_t|S )NrS   )r   r   r   Z_makeResultr   ZinstallHandlerZregisterResultr+   r(   cleanup_objectr   rS   r}   r   )r   r   r   r   r   r   r   r   __call__A  s    

z_MinimalRunner.__call__c          	   c   s@   t |j}z
|V  W dt |j| }x|D ]}|j|= q*W X dS )zu
        A context manager which cleans up unwanted attributes on a test case
        (or any other object).
        N)rp   __dict__)r   r   Zvanilla_attrsZspurious_attrsnamer   r   r   r   T  s    


z_MinimalRunner.cleanup_objectN)	rC   rD   rE   rF   r   r   
contextlibcontextmanagerr   r   r   r   r   r   4  s   r   c                   s4   e Zd ZdZeZdd Zdd Z fddZ  Z	S )r5   z\
    A test runner which delegates the actual running to a pool of child
    processes.
    c             K   s    t jj| f| || _|| _d S )N)r   r4   r   r   r   )r   r   r   r   r   r   r   k  s    zParallelTestRunner.__init__c             C   s`   t | j| j}t }|j}z,x&||| jD ]}|| |jr,P q,W |S |	  |
  X d S )N)r   r   r   multiprocessingZPoolZimap_unordered
_test_listr   r   Z	terminatejoin)r   r   Zchild_runnerZpoolZimapZchild_resultr   r   r   
_run_innerp  s    
zParallelTestRunner._run_innerc                s   t || _tt| | jS )N)rz   r   r   r5   r_   r   )r   r   )r   r   r   r_     s    
zParallelTestRunner.run)
rC   rD   rE   rF   r|   rx   r   r   r_   rG   r   r   )r   r   r5   c  s
   r5   z+Failed to enable faulthandler due to:
{err})err))collectionsr   r<   rR   r6   r   r
   r=   r3   r1   r   r   r   Zsixr   mainr   rT   RuntimeErrorrU   defaultdictrV   ZTextTestResultrY   r4   r/   rz   r|   objectr   r   r   r5   ZfaulthandlerImportErrorr>   BaseExceptionerw   r2   formatr   r   r   r   <module>   sB   aM/%