a
    –§»aÔ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"�yR   Y nL0 ze! #¡  W n< e$�yœ Z% z"dZ&e 'e&j(e%d�¡ W Y dZ%[%n
dZ%[%0 0 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                    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__© úR/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/llvmlite/tests/customize.pyr   !   s    zNumbaTestProgram.__init__c                    s&   | j d u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 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                    s^   t jdk r:d|v r"| d¡ d| _d|v 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 u 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 | ¡  td
|f ƒ | |¡ qü| ¡  td
|f ƒ | |¡ 0 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ÚenableZruncallÚdisableÚprintZ
dump_stats)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y$   dd„ } Y n0 z
t j}W n tyJ   dd„ }Y n0 t jt jt jt jfD ]}|d ur`| ¡  q`t  	¡  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y2   |  ¡ r,J ‚Y d S 0 dd„ }dd„ }d}|d|f|d|ffD ]x\}}}	|	|ƒr\d|||t|ƒf }
d	}zt|
ƒ‚W n ty¬   t ¡ }Y n0 | 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   Úfailedrs   Z	item_nameÚcheckerÚmsgr}   r   r   r   r   Å   s2    	þÿ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   Ú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 i |¤Ž}| ¡ }t ¡  t |¡ |j|_|j|_|  	|¡� ||ƒ W d   ƒ n1 sr0    Y  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 t |jƒ| }|D ]}|j|= q&n"t |jƒ| }|D ]}|j|= qH0 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i |¤Ž || _|| _d S r   )r   r8   r   r�   rž   )r   r�   r   r   r   r   r   p  s    zParallelTestRunner.__init__c                 C   sp   t | j| jƒ}t ¡ }|j}z<||| jƒD ]}| |¡ |jr* qDq*|W | 	¡  | 
¡  S | 	¡  | 
¡  0 d S 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   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/%