a
    þ
þdÃ’  ã                   @   s  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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mZmZmZmZmZmZm Z m!Z!m"Z"m#Z# zd dl$Z$W n e%yò   dZ$Y n0 dd	„ Z&e&ƒ Z'd
Z(e )e'e(¡Z*dd„ Z+dd„ Z,dd„ Z-dd„ Z.dd„ Z/dd„ Z0G dd„ deeƒZ1dd„ Z2G dd„ deƒZ3G dd„ de3ƒZ4G dd „ d e4ƒZ5e"G d!d"„ d"e4ƒƒZ6G d#d$„ d$e4ƒZ7G d%d&„ d&e3ƒZ8e!G d'd(„ d(ejƒƒZ9d)d*„ Z:G d+d,„ d,ejƒZ;G d-d.„ d.e3ƒZ<e=d/kre >¡  dS )0é    N)Únjit)Úcodegen)Ú_UserWideCacheLocator)ÚNumbaWarning)Úparfor)
ÚTestCaseÚSerialMixinÚcapture_cache_logÚimport_dynamicÚoverride_configÚoverride_env_configÚrun_in_new_process_cachingÚskip_if_typeguardÚskip_parfors_unsupportedÚtemp_directoryc               
   C   s,  t dƒ} tj | d¡}t |¡ ttj |d¡dƒ}| d¡ W d   ƒ n1 sT0    Y  t |d¡ z¤z\ttj |d¡dƒ}| d¡ W d   ƒ n1 s¢0    Y  W W t |d	¡ t 	|¡ d
S  t
yô   Y W t |d	¡ t 	|¡ dS 0 W t |d	¡ t 	|¡ nt |d	¡ t 	|¡ 0 d S )NÚ
test_cacheZwritable_testZwrite_okÚwtZcheck1é@  Zwrite_forbiddenZcheck2iý  FT)r   ÚosÚpathÚjoinÚmkdirÚopenÚwriteÚchmodÚshutilÚrmtreeÚPermissionError)ÚtempdirZtest_dirÚf© r    úa/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/numba/tests/test_caching.pyÚcheck_access_is_preventable%   s,    
((	
ö
ýÿr"   z9Cannot create a directory to which writes are preventablec                  C   s   d} t | ƒ| fS )NZabcd)Úhash)Úcr    r    r!   Úconstant_unicode_cacheI   s    r%   c                  C   sB   t } tdd| ƒ}| ƒ \}}|ƒ \}}||ks2J ‚||ks>J ‚d S ©NT©Úcache)r%   r   )ÚpyfuncÚcfuncZexp_hvZexp_strZgot_hvZgot_strr    r    r!   Úcheck_constant_unicode_cacheN   s    

r+   c                   C   s
   dddœS )Né   é   )ÚaÚbr    r    r    r    r!   Ú
dict_cacheW   s    r0   c                  C   s.   t } tdd| ƒ}| ƒ }|ƒ }||ks*J ‚d S r&   )r0   r   ©r)   r*   ÚexpÚgotr    r    r!   Úcheck_dict_cache[   s
    r4   c                  c   s   dD ]
} | V  qd S )N)r,   r-   é   r    )Úvr    r    r!   Úgenerator_cachec   s    r7   c                  C   s6   t } tdd| ƒ}t| ƒ ƒ}t|ƒ ƒ}||ks2J ‚d S r&   )r7   r   Úlistr1   r    r    r!   Úcheck_generator_cacheh   s
    

r9   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚTestCachingc                 C   s"   |ƒ  t |ƒ}|  |d d¡ d S )NÚexitcoder   )r   ÚassertEqual)ÚselfÚfuncÚresr    r    r!   Úrun_testq   s    zTestCaching.run_testc                 C   s   |   t¡ d S ©N)r@   r+   ©r=   r    r    r!   Útest_constant_unicode_cachev   s    z'TestCaching.test_constant_unicode_cachec                 C   s   |   t¡ d S rA   )r@   r4   rB   r    r    r!   Útest_dict_cachey   s    zTestCaching.test_dict_cachec                 C   s   |   t¡ d S rA   )r@   r9   rB   r    r    r!   Útest_generator_cache|   s    z TestCaching.test_generator_cachec                 C   s¸   t | jjƒ}t ¡ }| ¡ }|jt||dfd}| ¡  | 	¡  | 
¡ \}}|sZ|  |¡ |  |dd¡ |jt||dfd}| ¡  | 	¡  | 
¡ \}}|s¦|  |¡ |  |dd¡ d S )NF©ÚtargetÚargséè  z-Omitted function returned an incorrect outputT)r   Ú	__class__Ú__name__ÚmultiprocessingÚget_contextÚQueueÚProcessÚomitted_child_test_wrapperÚstartr   ÚgetÚfailr<   )r=   Ú	cache_dirÚctxÚresult_queueÚprocÚsuccessÚoutputr    r    r!   Útest_omitted   s>    þ
ýþ
ýzTestCaching.test_omittedN)rK   Ú
__module__Ú__qualname__r@   rC   rD   rE   rZ   r    r    r    r!   r:   p   s
   r:   c                 C   sî   t d|ƒÐ tddddd„ƒ}z„|ƒ }|rf|j|jd  dksHJ d	ƒ‚|j|jd  dksžJ d
ƒ‚n8|j|jd  dks‚J dƒ‚|j|jd  dksžJ dƒ‚d}W n   t ¡ }d}Y n0 |  ||f¡ W d   ƒ n1 sà0    Y  d S )NZ	CACHE_DIRTr'   rI   c                 S   s   | S rA   r    )Únumr    r    r!   Útest¬   s    z(omitted_child_test_wrapper.<locals>.testr   r,   zCache did not hit as expectedzCache has an unexpected misszCache did not miss as expectedzCache has an unexpected hitF)rI   )r   r   Z_cache_hitsZ
signaturesZ_cache_missesÚ	tracebackÚ
format_excÚput)rV   rT   Zsecond_callr^   rY   rX   r    r    r!   rP   ª   s.    ÿÿÿÿ
rP   c                   @   sL   e Zd ZdZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ ZdS )ÚBaseCacheTestNc                 C   st   t dƒ| _tj d| j¡ tj | j| jd ¡| _tj | jd¡| _	t
 | j| j¡ t | jtjtjB ¡ d | _d S )Nr   r   z.pyÚ__pycache__)r   r   Úsysr   Úinsertr   r   ÚmodnameÚmodfilerT   r   ÚcopyÚusecases_filer   ÚstatÚS_IREADÚS_IWRITEZmaxDiffrB   r    r    r!   ÚsetUpÌ   s    
zBaseCacheTest.setUpc                 C   s"   t j | jd ¡ t j | j¡ d S rA   )rd   ÚmodulesÚpoprf   r   Úremover   rB   r    r    r!   ÚtearDownÕ   s    zBaseCacheTest.tearDownc              	   C   sp   t j | jd ¡}|d urL|jg}|D ]&}zt |¡ W q$ tyH   Y q$0 q$t| jƒ}|  	|j
 d¡| j¡ |S )NÚco)rd   rn   ro   rf   Ú
__cached__r   ÚunlinkÚFileNotFoundErrorr
   r<   Ú__file__Úrstriprg   )r=   ÚoldÚcachedÚfnÚmodr    r    r!   Úimport_moduleÙ   s    
zBaseCacheTest.import_modulec                 C   s4   zdd„ t  | j¡D ƒW S  ty.   g  Y S 0 d S )Nc                 S   s   g | ]}|  d ¡s|‘qS ))z.pycz.pyo)Úendswith©Ú.0rz   r    r    r!   Ú
<listcomp>ì   s   
ÿz0BaseCacheTest.cache_contents.<locals>.<listcomp>)r   ÚlistdirrT   ru   rB   r    r    r!   Úcache_contentsê   s    zBaseCacheTest.cache_contentsc                    s   t ‡ fdd„tˆ  ¡ ƒD ƒƒS )Nc                 3   s*   | ]"}|t j t j ˆ j|¡¡fV  qd S rA   )r   r   Úgetmtimer   rT   r~   rB   r    r!   Ú	<genexpr>ò   s   ÿz1BaseCacheTest.get_cache_mtimes.<locals>.<genexpr>)ÚdictÚsortedr‚   rB   r    rB   r!   Úget_cache_mtimesñ   s    
ÿzBaseCacheTest.get_cache_mtimesc                 C   s   |   ¡ }|  t|ƒ||¡ d S rA   )r‚   r<   Úlen)r=   Únr$   r    r    r!   Úcheck_pycacheõ   s    zBaseCacheTest.check_pycachec                 C   s   d S rA   r    rB   r    r    r!   Ú
dummy_testù   s    zBaseCacheTest.dummy_test)rK   r[   r\   ri   rf   rm   rq   r|   r‚   r‡   rŠ   r‹   r    r    r    r!   rb   Æ   s   	rb   c                   @   s<   e Zd Zej e¡Zej ed¡Z	dZ
dd„ Zddd„ZdS )	ÚDispatcherCacheUsecasesTestúcache_usecases.pyÚdispatcher_caching_test_fodderc                 C   sf   dt | j| jd }tjtjd|gtjtjd}| ¡ \}}|j	dkrbt
d|j	| ¡ | ¡ f ƒ‚d S )Nzœif 1:
            import sys

            sys.path.insert(0, %(tempdir)r)
            mod = __import__(%(modname)r)
            mod.self_test()
            ©r   rf   ú-c©ÚstdoutÚstderrr   zBprocess failed with code %s: 
stdout follows
%s
stderr follows
%s
©r…   r   rf   Ú
subprocessÚPopenrd   Ú
executableÚPIPEÚcommunicateÚ
returncodeÚAssertionErrorÚdecode©r=   ÚcodeÚpopenÚoutÚerrr    r    r!   Úrun_in_separate_process  s    úÿ
ýÿz3DispatcherCacheUsecasesTest.run_in_separate_processNc                 C   sF   |j }|  t|j ¡ ƒ||j¡ |d urB|  t|j ¡ ƒ||j¡ d S rA   )Ústatsr<   ÚsumÚ
cache_hitsÚvaluesZcache_misses)r=   r>   ÚhitsÚmissesÚstr    r    r!   Ú
check_hits  s    ÿz&DispatcherCacheUsecasesTest.check_hits)N)rK   r[   r\   r   r   Údirnamerv   Úherer   ri   rf   r¢   rª   r    r    r    r!   rŒ   ý   s
   rŒ   c                   @   sö   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zee ejd!kd"¡d#d$„ ƒƒZee ejd!kd"¡d%d&„ ƒƒZd'd(„ Ze ed)u päejd* d+k d,¡d-d.„ ƒZd)S )/Ú	TestCachec                 C   s@  |   d¡ |  ¡ }|   d¡ |j}|  |ddƒd¡ |   d¡ |  |ddƒd¡ |   d¡ |  |dd¡ |j}|  |ddƒd¡ |   d¡ |  |ddƒd¡ |   d¡ |  |dd¡ |j}||jdƒ}|  t|ƒd	¡ ||j	dƒ}|  t|ƒd	¡ |   d
¡ |  |dd¡ |j
}|  |ddƒd¡ |  |ddƒd¡ |  ¡  d S )Nr   r-   r5   é   ç      @ç      @é   r,   )r-   g     ÀE@é	   y              @y       @      @)rŠ   r|   Úadd_usecaseÚassertPreciseEqualrª   Úadd_objmode_usecaseÚrecord_returnÚaligned_arrÚtupleÚ
packed_arrÚgenerated_usecaser¢   )r=   r{   r   Zrecr    r    r!   Útest_caching#  s4    






zTestCache.test_cachingc                 C   s€   |   d¡ |  ¡ }|   d¡ |j}|  |ddƒd¡ |   d¡ |  |dt d¡ƒdt d¡ d ¡ |   d¡ |  |dd¡ d S )Nr   r-   r5   r®   r,   )rŠ   r|   r³   r´   ÚnpZarangerª   ©r=   r{   r   r    r    r!   Útest_caching_nrt_prunedE  s    


&
z!TestCache.test_caching_nrt_prunedc                 C   s´   |   ¡ }|  | dd¡d¡ |  d¡ |j}|  |ddƒd¡ |  d¡ |   ¡ }|j}|  |ddƒd¡ |  d¡ |j}|  |ddƒd¡ |  d¡ |  |ddƒd¡ |  d¡ d S )Nr5   r-   r®   é   ç      @r¯   )r|   r´   ÚinnerrŠ   Úouter_uncachedÚouterr½   r    r    r!   Útest_inner_then_outerR  s    



zTestCache.test_inner_then_outerc                 C   sŽ   |   ¡ }|  | dd¡d¡ |  d¡ |  | dd¡d¡ |  d¡ |   ¡ }|j}|  |ddƒd¡ |  d¡ |  |ddƒd¡ |  d¡ d S )Nr5   r-   r¿   r®   rÀ   r°   r±   )r|   r´   rÃ   rŠ   rÂ   rÁ   r½   r    r    r!   Útest_outer_then_innerf  s    


zTestCache.test_outer_then_innerc                 C   s.   |   ¡ }|j}|  |ddƒd¡ |  d¡ d S )Nr-   r5   r®   r   )r|   Úadd_nocache_usecaser´   rŠ   r½   r    r    r!   Útest_no_cachingt  s    zTestCache.test_no_cachingc                 C   sŠ   |   ¡ }tjdd<}t dt¡ |j}|  |dƒd¡ |  d¡ W d   ƒ n1 sV0    Y  |  t	|ƒd¡ |  
dt|d jƒ¡ d S )	NT©ÚrecordÚalwaysr¿   r®   r   r,   zBCannot cache compiled function "looplifted" as it uses lifted code)r|   ÚwarningsÚcatch_warningsÚsimplefilterr   Z
loopliftedr´   rŠ   r<   rˆ   ÚassertInÚstrÚmessage©r=   r{   Úwr   r    r    r!   Útest_looplifted{  s    (ÿzTestCache.test_loopliftedc                 C   sŒ   |   ¡ }tjdd>}t dt¡ |j}tj |ƒ |j	¡ |  
d¡ W d   ƒ n1 sX0    Y  |  t|ƒd¡ |  dt|d jƒ¡ d S )NTrÈ   rÊ   r   r,   zICannot cache compiled function "use_big_array" as it uses dynamic globals)r|   rË   rÌ   rÍ   r   Zuse_big_arrayr¼   ZtestingZassert_equalZbiggierŠ   r<   rˆ   rÎ   rÏ   rÐ   rÑ   r    r    r!   Útest_big_arrayŠ  s    (ÿzTestCache.test_big_arrayc              	   C   s¢   |   ¡ }|j|j|jfD ]„}tjdd6}t dt¡ |  |dƒd¡ |  	d¡ W d   ƒ n1 sd0    Y  |  
t|ƒd¡ |  d |j¡t|d jƒ¡ qd S )NTrÈ   rÊ   g        r   r,   z#Cannot cache compiled function "{}")r|   Z	use_c_sinZuse_c_sin_nest1Zuse_c_sin_nest2rË   rÌ   rÍ   r   r´   rŠ   r<   rˆ   rÎ   ÚformatrK   rÏ   rÐ   )r=   r{   r   rÒ   r    r    r!   Útest_ctypes˜  s    (
þzTestCache.test_ctypesc                 C   s¢   |   ¡ }t ¡ ~ t dt¡ |j}|  |dƒd¡ |j}|  |dƒd¡ |j}|  |dƒd¡ |j	}|  |dƒd¡ |  
d¡ W d   ƒ n1 s”0    Y  d S )NÚerrorr5   r®   é   é
   é   r±   )r|   rË   rÌ   rÍ   r   Zclosure1r´   Zclosure2Zclosure3Zclosure4rŠ   r½   r    r    r!   Útest_closureª  s    
zTestCache.test_closurec                 C   sl   |   ¡ }|j}|  ||jdƒd¡ |  ||jdƒd¡ |  ||jdƒd¡ |  ||jdƒd¡ |  d¡ d S )Nr,   rÙ   éd   r-   é   é   )r|   Zfirst_class_function_usecaser<   Zfirst_class_function_mulZfirst_class_function_addrŠ   r½   r    r    r!   Útest_first_class_functionº  s    z#TestCache.test_first_class_functionc                 C   s  |   ¡ }| dd¡ | dd¡ | dd¡ | dd¡ | dd¡ | |jd¡ | |jd¡ | dd¡ |  	¡ }|  
|jdd¡ |   ¡ }|  ||¡ |j}|ddƒ |  
|dd¡ |ddƒ |  
|dd¡ |j}|ddƒ |  
|dd¡ |  |  	¡ |¡ |  ¡  |  |  	¡ |¡ d S )Nr-   r5   r¯   rÀ   r   r,   )r|   r³   rµ   rÂ   rÃ   r¶   r¹   r·   rº   r‡   rª   ZassertIsNotr<   r¢   )r=   r{   ÚmtimesÚmod2r   r    r    r!   Útest_cache_reuseÅ  s0    


zTestCache.test_cache_reusec                 C   s’   |   ¡ }|j}|  |ddƒd¡ t| jdƒ}| d¡ W d   ƒ n1 sL0    Y  |   ¡ }|j}|  |ddƒd¡ |j}|  |ddƒd¡ d S )Nr-   r5   r®   r.   z
Z = 10
é   )r|   r³   r´   r   rg   r   rµ   r½   r    r    r!   Útest_cache_invalidateä  s    (zTestCache.test_cache_invalidatec                 C   s„   |   ¡ }|j}|  |ddƒd¡ |   ¡ }|j}d|_|  |ddƒd¡ | ¡  |  |ddƒd¡ |   ¡ }|j}|  |ddƒd¡ d S )Nr-   r5   r®   rÙ   rã   )r|   r³   r´   ÚZZ	recompiler½   r    r    r!   Útest_recompileó  s    zTestCache.test_recompilec                 C   s8   |   ¡ }|j}|  |dƒd¡ |j}|  |dƒd¡ d S )Nr-   r¿   rØ   )r|   Zrenamed_function1r´   Zrenamed_function2r½   r    r    r!   Útest_same_names  s
    zTestCache.test_same_namesc                 C   s‚   ddl m} |j}tdddƒ}zR||_t |¡}t ||¡}|  |¡ dt	_
t ||¡}|  |t¡ W ||_t	`
n||_t	`
0 d S )Nr,   )ÚfunctionÚpassztests/dummy_module.pyÚexecT)Zdummy_modulerè   Ú__code__ÚcompileÚinspectÚgetfiler   Úfrom_functionZassertIsNonerd   ÚfrozenZassertIsInstance)r=   rè   Zold_codeZcode_objÚsourceÚlocatorr    r    r!   Útest_frozen  s    

ÿzTestCache.test_frozenc                 C   s€   |   ¡ }|j}| jtj|jjdd |  |ddƒd¡ |  |dd¡ |   ¡ }|j}|  |ddƒd¡ |  |dd¡ |  	d¡ dS )	zy
        With a disabled __pycache__, test there is a working fallback
        (e.g. on the user-wide cache dir)
        T)Úignore_errorsr-   r5   r®   r   r,   N)
r|   r³   Ú
addCleanupr   r   r£   Ú
cache_pathr´   rª   rŠ   )r=   r{   r   rá   r    r    r!   Ú_test_pycache_fallback$  s    z TestCache._test_pycache_fallbackÚntz3cannot easily make a directory read-only on Windowsc                 C   s:   t  | j¡j}t  | jd¡ |  t j| j|¡ |  ¡  d S )Nr   )r   rj   r   Úst_moder   rõ   r÷   )r=   Ú	old_permsr    r    r!   Útest_non_creatable_pycache=  s    z$TestCache.test_non_creatable_pycachec                 C   sN   t j | jd¡}t  |¡ t  |¡j}t  |d¡ |  t j||¡ |  	¡  d S )Nrc   r   )
r   r   r   r   r   rj   rù   r   rõ   r÷   )r=   Úpycacherú   r    r    r!   Útest_non_writable_pycacheH  s    
z#TestCache.test_non_writable_pycachec              
      sä   t jddg‰ ˆ g d¢7 ‰ zt ˆ dg ¡}W n6 tjyb } zˆ d|j ¡ W Y d }~n
d }~0 0 | ¡  ¡ }t	j
 ˆjd¡‰tˆdƒ}| d¡ W d   ƒ n1 sª0    Y  ‡ ‡‡fd	d
„}|ƒ  |ƒ }ˆ d| ¡ ¡ d S )Nú-mÚIPython©z--quietz--quickz--no-bannerz--colors=NoColorú	--versionú%ipython not available: return code %dzipython_cache_usecase.txtrÒ   á}  
                import os
                import sys

                from numba import jit

                # IPython 5 does not support multiline input if stdin isn't
                # a tty (https://github.com/ipython/ipython/issues/9752)
                f = jit(cache=True)(lambda: 42)

                res = f()
                # IPython writes on stdout, so use stderr instead
                sys.stderr.write(u"cache hits = %d\n" % f.stats.cache_hits[()])

                # IPython hijacks sys.exit(), bypass it
                sys.stdout.flush()
                sys.stderr.flush()
                os._exit(res)
                c                     sv   t ˆdƒX} tjˆ | tjtjdd}| ¡ \}}|jdkrPˆ d|j||f ¡ |W  d   ƒ S 1 sh0    Y  d S ©NÚrbT)Ústdinr’   r“   Úuniversal_newlinesé*   z6unexpected return code %d
-- stdout:
%s
-- stderr:
%s
©r   r•   r–   r˜   r™   rš   rS   ©r  Úpr    r¡   ©Zbase_cmdZinputfnr=   r    r!   Úexecute_with_inputv  s    ý

ýz2TestCache.test_ipython.<locals>.execute_with_inputúcache hits = 1)rd   r—   r•   Úcheck_outputÚCalledProcessErrorÚskipTestrš   Ústriprœ   r   r   r   r   r   r   rÎ   )r=   ÚverÚer   r  r¡   r    r  r!   Útest_ipythonU  s     ÿ(zTestCache.test_ipythonNr   r®   zrequires ipykernel >= 6c              
      sè   t jddg‰ ˆ g d¢7 ‰ zt ˆ dg ¡}W n6 tjyb } zˆ d|j ¡ W Y d }~n
d }~0 0 | ¡  ¡ }ddl	m
} | ¡ ‰tˆdƒ}| d	¡ W d   ƒ n1 s®0    Y  ‡ ‡‡fd
d„}|ƒ  |ƒ }ˆ d| ¡ ¡ d S )Nrþ   rÿ   r   r  r  r   )ÚcompilerrÒ   r  c                     sv   t ˆdƒX} tjˆ | tjtjdd}| ¡ \}}|jdkrPˆ d|j||f ¡ |W  d   ƒ S 1 sh0    Y  d S r  r	  r
  r  r    r!   r  ¯  s    ý

ýz4TestCache.test_ipykernel.<locals>.execute_with_inputr  )rd   r—   r•   r  r  r  rš   r  rœ   Ú	ipykernelr  Zget_tmp_directoryr   r   rÎ   )r=   r  r  r  r   r  r¡   r    r  r!   Útest_ipykernelŠ  s"    ÿ(zTestCache.test_ipykernel)rK   r[   r\   r»   r¾   rÄ   rÅ   rÇ   rÓ   rÔ   rÖ   rÛ   rß   râ   rä   ræ   rç   ró   r÷   Úskip_bad_accessÚunittestZskipIfr   Únamerû   rý   r  r  Úversion_infor  r    r    r    r!   r­   !  s>   "ÿÿ
5ÿr­   c                       s0   e Zd Z‡ fdd„Z‡ fdd„Zdd„ Z‡  ZS )ÚTestSequentialParForsCachec                    s   t t| ƒ ¡  dt_d S )NT)Úsuperr  rm   r   Úsequential_parfor_loweringrB   ©rJ   r    r!   rm   Æ  s    z TestSequentialParForsCache.setUpc                    s   t t| ƒ ¡  dt_d S )NF)r  r  rq   r   r  rB   r   r    r!   rq   Ë  s    z#TestSequentialParForsCache.tearDownc                 C   sj   |   ¡ }|  d¡ |j}t d¡}|  ||ƒ|| | ¡ dd„ |j ¡ D ƒ}|  |dg¡ |  d¡ d S )Nr   rÙ   c                 S   s   g | ]}|j j‘qS r    )ZlibraryZhas_dynamic_globals)r   Zcresr    r    r!   r€   Ö  s   ÿz;TestSequentialParForsCache.test_caching.<locals>.<listcomp>Fr-   )	r|   rŠ   Zparfor_usecaser¼   Zonesr´   Z	overloadsr¦   r<   )r=   r{   r   ZaryZdynamic_globalsr    r    r!   r»   Ð  s    

ÿz'TestSequentialParForsCache.test_caching)rK   r[   r\   rm   rq   r»   Ú__classcell__r    r    r   r!   r  Ä  s   r  c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚTestCacheWithCpuSettingFc                 C   sN   d}|   ¡  ¡ D ](\}}||v r|  ||| ¡ |d7 }q| j|ddd d S )Nr   r,   znothing to compare©Úmsg)r‡   ÚitemsZassertGreaterEqualÚassertGreater)r=   Z
mtimes_oldÚmatch_countÚkr6   r    r    r!   Úcheck_later_mtimesà  s    
ÿz*TestCacheWithCpuSetting.check_later_mtimesc                 C   s6  |   d¡ |  ¡ }| ¡  t|  ¡ ƒ}|  ¡ }tddƒ |  ¡  W d   ƒ n1 sV0    Y  |  |¡ |  	t|  ¡ ƒ|¡ |j
j}|j}| ¡ }|  t|ƒd¡ | ¡ \}}|d d t ¡ krÐ|| }	}
n
|| }	}
|  |	d d t ¡ ¡ |  |	d d t ¡ ¡ |  |
d d d¡ |  |
d d d¡ d S )Nr   ZNUMBA_CPU_NAMEZgenericr-   r,   Ú )rŠ   r|   Ú	self_testrˆ   r‚   r‡   r   r¢   r)  r&  r³   Ú_cacheÚ_cache_fileÚ_load_indexr<   ÚkeysÚllÚget_host_cpu_namer   Úget_host_cpu_features)r=   r{   Ú
cache_sizerà   r(   Ú
cache_fileÚcache_indexÚkey_aÚkey_bÚkey_hostÚkey_genericr    r    r!   Útest_user_set_cpu_nameé  s*    
&

z.TestCacheWithCpuSetting.test_user_set_cpu_namec                 C   sJ  |   d¡ |  ¡ }| ¡  t|  ¡ ƒ}|  ¡ }d}t ¡ }|  ||¡ t	d|ƒ |  
¡  W d   ƒ n1 sn0    Y  |  |¡ |  t|  ¡ ƒ|¡ |jj}|j}| ¡ }|  t|ƒd¡ | ¡ \}	}
|	d d |krä|	|
 }}n
|
|	 }}|  |d d t ¡ ¡ |  |d d |¡ |  |d d t ¡ ¡ |  |d d |¡ d S )Nr   z	-sse;-avxZNUMBA_CPU_FEATURESr-   r,   )rŠ   r|   r+  rˆ   r‚   r‡   r   r2  ÚassertNotEqualr   r¢   r)  r&  r³   r,  r-  r.  r<   r/  r0  r1  )r=   r{   r3  rà   Zmy_cpu_featuresZsystem_featuresr(   r4  r5  r6  r7  r8  r9  r    r    r!   Útest_user_set_cpu_features  s0    
&

z2TestCacheWithCpuSetting.test_user_set_cpu_featuresN)rK   r[   r\   Ú_numba_parallel_test_r)  r:  r<  r    r    r    r!   r"  Ü  s   	r"  c                   @   s6   e Zd ZdZej e¡Zej 	ed¡Z
dZdd„ ZdS )ÚTestMultiprocessCacheFr   rŽ   c                 C   sŠ   |   ¡ }|j}d}zt d¡}W n ty6   t}Y n0 | |¡}z t| |t|ƒ¡ƒ}W | 	¡  n
| 	¡  0 |  
|||d  d ¡ d S )Nr5   Úspawnr,   r-   )r|   Zsimple_usecase_callerrL   rM   ÚAttributeErrorZPoolr¤   ÚimapÚrangeÚcloser<   )r=   r{   r   r‰   rU   Úpoolr?   r    r    r!   Útest_multiprocessing2  s    

z*TestMultiprocessCache.test_multiprocessingN)rK   r[   r\   r=  r   r   r«   rv   r¬   r   ri   rf   rE  r    r    r    r!   r>  (  s
   r>  c                   @   sx   e Zd ZdZej e¡Zej 	ed¡Z
dZdZdZdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Ze eedƒd¡dd„ ƒZdS )ÚTestCacheFileCollisionFr   Zcaching_file_loc_fodderzD
from numba import njit
@njit(cache=True)
def bar():
    return 123
zD
from numba import njit
@njit(cache=True)
def bar():
    return 321
c                 C   sî   t dƒ| _tj d| j¡ d| _|  | jtj¡ | j| _d 	| jdg¡| _
tj 	| j| j¡}t |¡ ttj 	|d¡dƒ}t| j|d W d   ƒ n1 sž0    Y  ttj 	|d	¡dƒ}t| j|d W d   ƒ n1 sà0    Y  d S )
NÚtest_cache_file_locr   Zmodule_name_that_is_unlikelyÚ.Zfooz__init__.pyrÒ   ©Úfilezfoo.py)r   r   rd   r   re   rf   ZassertNotInrn   Úmodname_bar1r   Úmodname_bar2r   r   r   ÚprintÚsource_text_1Úsource_text_2)r=   ZfoomodÚfoutr    r    r!   rm   Y  s    

,zTestCacheFileCollision.setUpc                 C   s2   t j | jd ¡ t j | jd ¡ t j | j¡ d S rA   )rd   rn   ro   rK  rL  r   rp   r   rB   r    r    r!   rq   g  s    zTestCacheFileCollision.tearDownc                 C   s   t | jƒjS rA   )r
   rK  ÚbarrB   r    r    r!   Úimport_bar1l  s    z"TestCacheFileCollision.import_bar1c                 C   s   t | jƒjS rA   )r
   rL  rQ  rB   r    r    r!   Úimport_bar2o  s    z"TestCacheFileCollision.import_bar2c                 C   sT   |   ¡ }|  ¡ }|jjj}|jjj}|  ||¡ |  | d¡¡ |  | d¡¡ d S )Nz__init__.bar-3.pyzfoo.bar-3.py)rR  rS  r,  r-  Z_index_namer;  Ú
assertTrueÚ
startswith)r=   Úbar1Úbar2Zidxname1Zidxname2r    r    r!   Útest_file_locationr  s    

z)TestCacheFileCollision.test_file_locationrM   z)Test requires multiprocessing.get_contextc                 C   s@  |   ¡ }|  ¡ }tƒ }|ƒ }W d   ƒ n1 s20    Y  | ¡ }|  | d¡d¡ |  | d¡d¡ |  | d¡d¡ |  | d¡d¡ tƒ }|ƒ }W d   ƒ n1 s®0    Y  | ¡ }|  | d¡d¡ |  | d¡d¡ |  | d¡d¡ |  | d¡d¡ |  ||¡ zt d¡}W n t	y>   t
dƒ Y n0 | ¡ }|jt|| j| j| jfd	}	|	 ¡  | ¡ }
| ¡ }| ¡ }| ¡ }|	 ¡  |  ||¡ |  ||¡ |  |
 d¡d¡ |  |
 d¡d¡ |  |
 d¡d¡ |  |
 d¡d¡ |  | d¡d¡ |  | d¡d¡ |  | d¡d¡ |  | d¡d¡ d S )
Nzindex savedr,   z
data savedzindex loadedr   zdata loadedr?  zmissing spawn contextrF   )rR  rS  r	   Úgetvaluer<   Úcountr;  rL   rM   Ú
ValueErrorrM  rN   rO   Úcache_file_collision_testerr   rK  rL  rQ   rR   r   )r=   rV  rW  ÚbufZres1ZcachelogZres2ÚmpÚqrW   Zlog1Zgot1Úlog2Zgot2r    r    r!   Útest_no_collision|  sX    $$þÿz(TestCacheFileCollision.test_no_collisionN)rK   r[   r\   r=  r   r   r«   rv   r¬   r   ri   rf   rN  rO  rm   rq   rR  rS  rX  r  Ú
skipUnlessÚhasattrrL   ra  r    r    r    r!   rF  E  s   
ÿrF  c           	      C   s®   t j d|¡ t|ƒj}t|ƒj}tƒ }|ƒ }W d   ƒ n1 sD0    Y  |  | ¡ ¡ |  |¡ tƒ }|ƒ }W d   ƒ n1 sˆ0    Y  |  | ¡ ¡ |  |¡ d S )Nr   )rd   r   re   r
   rQ  r	   ra   rY  )	r_  r   rK  rL  rV  rW  r]  Zr1Zr2r    r    r!   r\  º  s    

$
$r\  c                   @   s0   e Zd ZdZdZdZdd„ Zdd„ Zdd	„ Zd
S )Ú#TestCacheMultipleFilesWithSignatureFz
from file2 import function2
z¤
from numba import njit

@njit('float64(float64)', cache=True)
def function1(x):
    return x

@njit('float64(float64)', cache=True)
def function2(x):
    return x
c                 C   s¦   t dƒ| _tj | jd¡| _t| jdƒ}t| j|d W d   ƒ n1 sL0    Y  tj | jd¡| _	t| j	dƒ}t| j
|d W d   ƒ n1 s˜0    Y  d S )NrG  zfile1.pyrÒ   rI  zfile2.py)r   r   r   r   r   Úfile1r   rM  Úsource_text_file1Úfile2Úsource_text_file2)r=   rP  r    r    r!   rm   Ü  s    
,z)TestCacheMultipleFilesWithSignature.setUpc                 C   s   t  | j¡ d S rA   )r   r   r   rB   r    r    r!   rq   ç  s    z,TestCacheMultipleFilesWithSignature.tearDownc                 C   s¦   t jtj| jgt jt jd}| ¡ \}}d| ¡ › d| ¡ › }| j|j	d|d t jtj| j
gt jt jd}| ¡ \}}d| ¡ › d| ¡ › }|  |j	d|¡ d S )Nr‘   zstdout:
z


stderr:
r   r#  )r•   r–   rd   r—   re  r˜   r™   rœ   r<   rš   rg  )r=   rŸ   r    r¡   r$  r    r    r!   Ú*test_caching_mutliple_files_with_signatureê  s    þþzNTestCacheMultipleFilesWithSignature.test_caching_mutliple_files_with_signatureN)	rK   r[   r\   r=  rf  rh  rm   rq   ri  r    r    r    r!   rd  È  s   rd  c                   @   sB   e Zd Zej e¡Zej ed¡Z	dZ
dd„ Zdd„ Zdd„ Zd	S )
ÚTestCFuncCachezcfunc_cache_usecases.pyZcfunc_caching_test_fodderc                 C   sf   dt | j| jd }tjtjd|gtjtjd}| ¡ \}}|j	dkrbt
d|j	› d| ¡ › dƒ‚d S )	Naf  if 1:
            import sys

            sys.path.insert(0, %(tempdir)r)
            mod = __import__(%(modname)r)
            mod.self_test()

            f = mod.add_usecase
            assert f.cache_hits == 1
            f = mod.outer
            assert f.cache_hits == 1
            f = mod.div_usecase
            assert f.cache_hits == 1
            r   r   r‘   r   zprocess failed with code z:stderr follows
Ú
r”   r   r    r    r!   r¢     s    óÿ
ÿz&TestCFuncCache.run_in_separate_processc                 C   s   |  ¡  d S rA   )r+  ©r=   r{   r    r    r!   Úcheck_module  s    zTestCFuncCache.check_modulec                 C   sÎ   |   d¡ |  ¡ }|   d¡ |  |jjd¡ |  |jjd¡ |  |jjd¡ |  |jjd¡ |  |¡ |  ¡ }|   d¡ |  |jjd¡ |  |jjd¡ |  |jjd¡ |  |jjd¡ |  |¡ |  	¡  d S )Nr   r®   r,   )
rŠ   r|   r<   r³   r¥   rÃ   rÆ   Zdiv_usecaserm  r¢   rl  r    r    r!   r»     s     




zTestCFuncCache.test_cachingN)rK   r[   r\   r   r   r«   rv   r¬   r   ri   rf   r¢   rm  r»   r    r    r    r!   rj  ü  s   rj  Ú__main__)?rí   Zllvmlite.bindingZbindingr0  rL   Únumpyr¼   r   rj   r   r•   rd   r_   r  rË   Znumbar   Z
numba.corer   Znumba.core.cachingr   Znumba.core.errorsr   Znumba.parforsr   Znumba.tests.supportr   r   r	   r
   r   r   r   r   r   r   r  ÚImportErrorr"   Z_access_preventableZ_access_msgrb  r  r%   r+   r0   r4   r7   r9   r:   rP   rb   rŒ   r­   r  r"  r>  rF  r\  rd  rj  rK   Úmainr    r    r    r!   Ú<module>   sd   0
	:7$   &Lt49
