U
    O»{jÔ3  ã                
   @   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"k
�rT   Y nJX ze! #¡  W n: e$k
�rœ Z% zdZ&e 'e&j(e%d�¡ W 5 dZ%[%X Y nX dS )é    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                    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__© úW/var/www/html/bwave/python/venv/lib/python3.8/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 )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                    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)é   é   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ƒs4t d¡ nˆ jd kr4tjˆ _ˆ j	rTt
ˆ 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 5 | ¡  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 r   )r   r   ÚrunTestsr   )r   r   r   r   Úrun_tests_real`   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ÚtimeÚperf_counterÚenableÚdisableÚprintZ
dump_statsZruncall)r   r1   ÚfilenameÚpr   r   r   r0   N   s:    

ýÿþþ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k
r&   dd„ } Y nX z
t j}W n tk
rN   dd„ }Y nX t jt jt jt jfD ]}|d k	rd| ¡  qdt  	¡  t
 ¡  | ƒ |ƒ fS )Nc                   S   s   dS ©Né*   r   r   r   r   r   Úfunc1€   s    z_refleak_cleanup.<locals>.func1c                   S   s   dS rM   r   r   r   r   r   Úfunc2…   s    z_refleak_cleanup.<locals>.func2)r	   ÚgetallocatedblocksÚAttributeErrorr.   ÚstdoutÚstderrÚ
__stdout__Ú
__stderr__ÚflushÚ_clear_type_cacheÚgcZcollect)rO   rP   Ústreamr   r   r   Ú_refleak_cleanup{   s    


r[   c                   @   s   e Zd ZdS )ÚReferenceLeakErrorN)rH   rI   rJ   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__š   s    zIntPool.__missing__N)rH   rI   rJ   r_   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 ]}||  qBd }}	t|ƒD ]„}t ¡ }
| |
¡ |
 	¡ sž| j
 |
j
¡ | j |
j¡ t‚~
tƒ \}}||krÚ|||	  ||| < |||  ||| < || }}	q`||fS )Nr   i8ÿÿÿéÈ   )rZ   rW   Ú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k
r4   |  ¡ r.t‚Y d S X dd„ }dd„ }d}|d|f|d|ffD ]z\}}}	|	|ƒr^d|||t|ƒf }
d	}zt|
ƒ‚W n tk
r°   t ¡ }Y nX | jrÌ| j	 
d
||f ¡ |  ||¡ q^|sîtt| ƒ |¡ 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Úsetrr   r   r   r   Úcheck_alloc_deltasÑ   s
    z8RefleakTestResult.addSuccess.<locals>.check_alloc_deltasFZ
referenceszmemory blocksz%s leaked %s %s, sum=%sTz%s = %r )rp   rk   rg   Úsumr\   Ú	Exceptionr	   Úexc_infoZshowAllrZ   ÚwriteZ
addFailurer   r`   Ú
addSuccess)r   r   rl   rm   rt   rz   Zfailedrs   Z	item_nameÚcheckerÚmsgr}   r   r   r   r   Å   s8    	þ   ÿzRefleakTestResult.addSuccess)rH   rI   rJ   rd   rc   rp   r   rL   r   r   r   r   r`   ž   s   "r`   c                   @   s   e Zd ZeZdS )r3   N)rH   rI   rJ   r`   Úresultclassr   r   r   r   r3   ï   s   r3   c                 C   s6   t | tjƒr,g }| D ]}| t|ƒ¡ q|S | gS dS )z$Expand suite into list of tests
    N)Ú
isinstancer7   Z	TestSuiteri   Ú_flatten_suite)r   Z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)rZ   r~   ÚgetvaluerW   ÚtestsRunrh   ri   rj   ÚskippedÚexpectedFailuresÚunexpectedSuccesses)r   r   r   r   r   Úadd_results  s    
zParallelTestResult.add_resultsN)rH   rI   rJ   rK   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.
    )rh   rj   r‰   rŠ   r‹   rZ   Ú
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 ]\}}|  |¡ qXd S r   )Ú	__slots__ÚsetattrÚgetattrrŠ   r�   rj   rh   )r   Zoriginal_resultÚattrr�   Ú_r   r   r   r   "  s    
z_MinimalResult.__init__N)rH   rI   rJ   rK   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)rH   rI   rJ   rK   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f |Ž}| ¡ }t ¡  t |¡ |j|_|j|_|  	|¡� ||ƒ W 5 Q R X t
|j ¡ ƒ|_t|ƒS )NrZ   )rœ   r   r›   Z_makeResultr   ZinstallHandlerZregisterResultr/   r,   Úcleanup_objectr–   rZ   r‡   r�   )r   r   r   r   r   r   r   r   Ú__call__F  s    

z_MinimalRunner.__call__c              	   c   s<   t |jƒ}z
|V  W 5 t |jƒ| }|D ]}|j|= q(X dS )zu
        A context manager which cleans up unwanted attributes on a test case
        (or any other object).
        N)ry   Ú__dict__)r   r   Zvanilla_attrsZspurious_attrsÚnamer   r   r   r�   Y  s    

z_MinimalRunner.cleanup_objectN)	rH   rI   rJ   rK   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 )r9   z\
    A test runner which delegates the actual running to a pool of child
    processes.
    c                 K   s    t jj| f|Ž || _|| _d S r   )r   r8   r   r›   rœ   )r   r›   r   r   r   r   r   p  s    zParallelTestRunner.__init__c                 C   sb   t | j| jƒ}t ¡ }|j}z.||| jƒD ]}| 	|¡ |j
r* qDq*|W ¢S | ¡  | ¡  X d S r   )rš   r›   rœ   ÚmultiprocessingZPoolZimap_unorderedÚ	terminateÚjoinÚ
_test_listrŒ   rŽ   )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   r9   rf   r©   )r   r   r   r   r   rf   †  s    
zParallelTestRunner.run)
rH   rI   rJ   rK   r†   r‚   r   r©   rf   rL   r   r   r   r   r9   h  s
   r9   z+Failed to enable faulthandler due to:
{err})Úerr))Úcollectionsr¡   r@   Úior   rY   r:   r£   r	   rA   r7   r5   r   r   r   Úmainr   r[   ÚRuntimeErrorr\   Údefaultdictr]   ZTextTestResultr`   r8   r3   r„   r†   Úobjectr�   r–   rš   r9   ÚfaulthandlerÚImportErrorrC   ÚBaseExceptionÚer�   r6   Úformatr   r   r   r   Ú<module>   sB   aQ/%