o
    8ήcz                     @   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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$yu   dZ#Y nw 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 Z/G dd deeZ0dd Z1G dd deZ2G dd de2Z3G dd  d e3Z4e!G d!d" d"e3Z5G d#d$ d$e3Z6G d%d& d&e2Z7e G d'd( d(ejZ8d)d* Z9G d+d, d,ejZ:G d-d. d.e2Z;e<d/kre=  dS 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 s*w   Y  t|d zEz.ttj|dd}|d W d    n1 sQw   Y  W W t|d	 t	| d
S  t
yz   Y W t|d	 t	| dS w t|d	 t	| w )N
test_cachewritable_testwrite_okwtcheck1@  write_forbiddencheck2i  FT)r   ospathjoinmkdiropenwritechmodshutilrmtreePermissionError)tempdirtest_dirf r&   ?/tmp/pip-target-vg8gfxp4/lib/python/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 )Nabcd)hash)cr&   r&   r'   constant_unicode_cacheH   s   r,   c                  C   sB   t } tdd| }|  \}}| \}}||ksJ ||ksJ d S NTcache)r,   r   )pyfunccfuncexp_hvexp_strgot_hvgot_strr&   r&   r'   check_constant_unicode_cacheM   s   

r6   c                   C   s
   dddS )N      )abr&   r&   r&   r&   r'   
dict_cacheV   s   
r;   c                  C   s.   t } tdd| }|  }| }||ksJ d S r-   )r;   r   r0   r1   expgotr&   r&   r'   check_dict_cacheZ   s
   r?   c                  c   s    dD ]} | V  qd S )N)r7   r8      r&   )vr&   r&   r'   generator_cacheb   s   rB   c                  C   s6   t } tdd| }t|  }t| }||ksJ d S r-   )rB   r   listr<   r&   r&   r'   check_generator_cacheg   s
   

rD   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_testp   s   zTestCaching.run_testc                 C      |  t d S N)rK   r6   rH   r&   r&   r'   test_constant_unicode_cacheu      z'TestCaching.test_constant_unicode_cachec                 C   rL   rM   )rK   r?   rN   r&   r&   r'   test_dict_cachex   rP   zTestCaching.test_dict_cachec                 C   rL   rM   )rK   rD   rN   r&   r&   r'   test_generator_cache{   rP   z TestCaching.test_generator_cachec                 C   s   t | jj}t }| }|jt||dfd}|  |	  |
 \}}|s-| | | |dd |jt||dfd}|  |	  |
 \}}|sS| | | |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failrG   )rH   	cache_dirctxresult_queueprocsuccessoutputr&   r&   r'   test_omitted~   s>   

zTestCaching.test_omittedN)rX   
__module____qualname__rK   rO   rQ   rR   rg   r&   r&   r&   r'   rE   o   s    rE   c                 C   s   t d|g tddddd}zB| }|r3|j|jd  dks$J d	|j|jd  dks2J d
n|j|jd  dksAJ d|j|jd  dksOJ dd}W n
   t }d}Y | ||f W d    d S 1 sow   Y  d S )N	CACHE_DIRTr.   rV   c                 S   s   | S rM   r&   )numr&   r&   r'   test   s   z(omitted_child_test_wrapper.<locals>.testr   r7   zCache did not hit as expectedzCache has an unexpected misszCache did not miss as expectedzCache has an unexpected hitF)rV   )r   r   _cache_hits
signatures_cache_misses	traceback
format_excput)rc   ra   second_callrl   rf   re   r&   r&   r'   r]      s0   "r]   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   s^   t d| _tjd| j tj| j| jd | _tj| jd| _	t
| j| j d | _d S )Nr   r   z.py__pycache__)r   r#   sysr   insertr   r   modnamemodfilera   r    copyusecases_filemaxDiffrN   r&   r&   r'   setUp   s   

zBaseCacheTest.setUpc                 C   s"   t j| jd  t j| j d S rM   )rv   modulespoprx   r   remover#   rN   r&   r&   r'   tearDown   s   zBaseCacheTest.tearDownc              	   C   sn   t j| jd }|d ur%|jg}|D ]}zt| W q ty$   Y qw t| j}| 	|j
d| j |S )Nco)rv   r~   r   rx   
__cached__r   unlinkFileNotFoundErrorr
   rG   __file__rstripry   )rH   oldcachedfnmodr&   r&   r'   import_module   s   
zBaseCacheTest.import_modulec                 C   s0   zdd t | jD W S  ty   g  Y S w )Nc                 S   s   g | ]	}| d s|qS ))z.pycz.pyo)endswith.0r   r&   r&   r'   
<listcomp>   s    
z0BaseCacheTest.cache_contents.<locals>.<listcomp>)r   listdirra   r   rN   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 rM   )r   r   getmtimer   ra   r   rN   r&   r'   	<genexpr>   s    $z1BaseCacheTest.get_cache_mtimes.<locals>.<genexpr>)dictsortedr   rN   r&   rN   r'   get_cache_mtimes   s   
zBaseCacheTest.get_cache_mtimesc                 C   s   |   }| t||| d S rM   )r   rG   len)rH   nr+   r&   r&   r'   check_pycache   s   zBaseCacheTest.check_pycachec                 C   s   d S rM   r&   rN   r&   r&   r'   
dummy_test   s   zBaseCacheTest.dummy_test)rX   rh   ri   r{   rx   r}   r   r   r   r   r   r   r&   r&   r&   r'   rt      s    rt   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r1t
d|j	| | f d S )Nzif 1:
            import sys

            sys.path.insert(0, %(tempdir)r)
            mod = __import__(%(modname)r)
            mod.self_test()
            r#   rx   -cstdoutstderrr   zBprocess failed with code %s: 
stdout follows
%s
stderr follows
%s
r   r#   rx   
subprocessPopenrv   
executablePIPEcommunicate
returncodeAssertionErrordecoderH   codepopenouterrr&   r&   r'   run_in_separate_process   s   
z3DispatcherCacheUsecasesTest.run_in_separate_processNc                 C   sJ   |j }| t|j ||j |d ur#| t|j ||j d S d S rM   )statsrG   sum
cache_hitsvaluescache_misses)rH   rI   hitsmissesstr&   r&   r'   
check_hits  s   z&DispatcherCacheUsecasesTest.check_hitsrM   )rX   rh   ri   r   r   dirnamer   herer   r{   rx   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 pre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   r8   r@            @      @   r7   )r8   g     E@	   y              @y       @      @)r   r   add_usecaseassertPreciseEqualr   add_objmode_usecaserecord_returnaligned_arrtuple
packed_arrgenerated_usecaser   )rH   r   r%   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   r8   r@   r   r7   )r   r   r   r   nparanger   rH   r   r%   r&   r&   r'   test_caching_nrt_prunedC  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 )Nr@   r8   r            @r   )r   r   innerr   outer_uncachedouterr   r&   r&   r'   test_inner_then_outerP  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 )Nr@   r8   r   r   r   r   r   )r   r   r   r   r   r   r   r&   r&   r'   test_outer_then_innerd  s   


zTestCache.test_outer_then_innerc                 C   s.   |   }|j}| |ddd | d d S )Nr8   r@   r   r   )r   add_nocache_usecaser   r   r   r&   r&   r'   test_no_cachingr  s   zTestCache.test_no_cachingc                 C   s   |   }tjdd}tdt |j}| |dd | d W d    n1 s+w   Y  | t	|d | 
dt|d j d S )	NTrecordalwaysr   r   r   r7   zBCannot cache compiled function "looplifted" as it uses lifted code)r   warningscatch_warningssimplefilterr   
loopliftedr   r   rG   r   assertInstrmessagerH   r   wr%   r&   r&   r'   test_loopliftedy  s   zTestCache.test_loopliftedc                 C   s   |   }tjdd}tdt |j}tj| |j	 | 
d W d    n1 s,w   Y  | t|d | dt|d j d S )NTr   r   r   r7   zICannot cache compiled function "use_big_array" as it uses dynamic globals)r   r   r   r   r   use_big_arrayr   testingassert_equalbiggier   rG   r   r   r   r   r   r&   r&   r'   test_big_array  s   zTestCache.test_big_arrayc              	   C   s   |   }|j|j|jfD ]B}tjdd}tdt | |dd | 	d W d    n1 s2w   Y  | 
t|d | d|jt|d j qd S )NTr   r   g        r   r7   z#Cannot cache compiled function "{}")r   	use_c_sinuse_c_sin_nest1use_c_sin_nest2r   r   r   r   r   r   rG   r   r   formatrX   r   r   )rH   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    d S 1 sKw   Y  d S )Nerrorr@   r      
      r   )r   r   r   r   r   closure1r   closure2closure3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 )Nr7   r   d   r8         )r   first_class_function_usecaserG   first_class_function_mul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 )Nr8   r@   r   r   r   r7   )r   r   r   r   r   r   r   r   r   r   r   assertIsNotrG   r   )rH   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 s&w   Y  |   }|j}| |ddd |j}| |ddd d S )Nr8   r@   r   r9   z
Z = 10
   )r   r   r   r   ry   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 )Nr8   r@   r   r   r  )r   r   r   Z	recompiler   r&   r&   r'   test_recompile  s   zTestCache.test_recompilec                 C   s8   |   }|j}| |dd |j}| |dd d S )Nr8   r   r   )r   renamed_function1r   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}z*||_t|}t||}| | dt	_
t||}| |t W ||_t	`
d S ||_t	`
w )Nr7   )functionpassztests/dummy_module.pyexecT)dummy_moduler  __code__compileinspectgetfiler   from_functionassertIsNonerv   frozenassertIsInstance)rH   r  old_code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_errorsr8   r@   r   r   r7   N)
r   r   
addCleanupr    r!   r   
cache_pathr   r   r   )rH   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   statr#   st_moder   r   r"  )rH   	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 )Nru   r   )
r   r   r   r#   r   r$  r%  r   r   r"  )rH   pycacher&  r&   r&   r'   test_non_writable_pycacheF  s   
z#TestCache.test_non_writable_pycachec              
      s   t jddg  g d7  z
t dg }W n tjy1 } zd|j  W Y d }~nd }~ww |  }t	j
jdtd}|d W d    n1 sUw   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                     v   t d,} tj | tjtjdd}| \}}|jdkr(d|j||f  |W  d    S 1 s4w   Y  d S NrbT)stdinr   r   universal_newlines*   z6unexpected return code %d
-- stdout:
%s
-- stderr:
%s
r   r   r   r   r   r   r`   r3  pr   r   base_cmdinputfnrH   r&   r'   execute_with_inputt     

$z2TestCache.test_ipython.<locals>.execute_with_inputcache hits = 1)rv   r   r   check_outputCalledProcessErrorskipTestr   stripr   r   r   r   r#   r   r   r   )rH   verer%   r<  r   r&   r9  r'   test_ipythonS  s&   zTestCache.test_ipythonNr   r   zrequires ipykernel >= 6c              
      s   t jddg  g d7  z
t dg }W n tjy1 } zd|j  W Y d }~nd }~ww |  }ddl	m
} | td}|d	 W d    n1 sWw   Y   fd
d}|  | }d|  d S )Nr*  r+  r,  r-  r.  r   )compilerr   r/  c                     r0  r1  r6  r7  r9  r&   r'   r<    r=  z4TestCache.test_ipykernel.<locals>.execute_with_inputr>  )rv   r   r   r?  r@  rA  r   rB  r   	ipykernelrF  get_tmp_directoryr   r   r   )rH   rC  rD  rF  r%   r<  r   r&   r9  r'   test_ipykernel  s(   zTestCache.test_ipykernel)rX   rh   ri   r   r   r   r   r   r   r   r   r   r  r  r  r
  r  r  r"  skip_bad_accessunittestskipIfr   namer'  r)  rE  rG  version_inforI  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                       t t|   dt_d S )NT)superrO  r}   r   sequential_parfor_loweringrN   rW   r&   r'   r}        
z TestSequentialParForsCache.setUpc                    rP  )NF)rQ  rO  r   r   rR  rN   rS  r&   r'   r     rT  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&   )libraryhas_dynamic_globals)r   cresr&   r&   r'   r     s    z;TestSequentialParForsCache.test_caching.<locals>.<listcomp>Fr8   )	r   r   parfor_usecaser   onesr   	overloadsr   rG   )rH   r   r%   arydynamic_globalsr&   r&   r'   r     s   

z'TestSequentialParForsCache.test_caching)rX   rh   ri   r}   r   r   __classcell__r&   r&   rS  r'   rO    s    rO  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   r7   znothing to comparemsg)r   itemsassertGreaterEqualassertGreater)rH   
mtimes_oldmatch_countkrA   r&   r&   r'   check_later_mtimes  s   
z*TestCacheWithCpuSetting.check_later_mtimesc                 C   s6  |  d |  }|  t|  }|  }tdd |   W d    n1 s+w   Y  | | | 	t|  | |j
j}|j}| }| t|d | \}}|d d t krh||}	}
n||}	}
| |	d d t  | |	d d t  | |
d d d | |
d d d d S )Nr   NUMBA_CPU_NAMEgenericr8   r7    )r   r   	self_testr   r   r   r   r   rg  rc  r   _cache_cache_file_load_indexrG   keysllget_host_cpu_namer   get_host_cpu_features)rH   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 s7w   Y  | | | t|  | |jj}|j}| }| t|d | \}	}
|	d d |krr|	|
}}n|
|	}}| |d d t  | |d d | | |d d t  | |d d | d S )Nr   z	-sse;-avxNUMBA_CPU_FEATURESr8   r7   )r   r   rk  r   r   r   r   rr  assertNotEqualr   r   rg  rc  r   rl  rm  rn  rG   ro  rp  rq  )rH   r   rs  r  my_cpu_featuressystem_featuresr/   rt  ru  rv  rw  rx  ry  r&   r&   r'   test_user_set_cpu_features  s2   



z2TestCacheWithCpuSetting.test_user_set_cpu_featuresN)rX   rh   ri   _numba_parallel_test_rg  rz  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y   t}Y nw ||}zt||t|}W |	  n|	  w | 
|||d  d  d S )Nr@   spawnr7   r8   )r   simple_usecase_callerrY   rZ   AttributeErrorPoolr   imaprangecloserG   )rH   r   r%   r   rb   poolrJ   r&   r&   r'   test_multiprocessing0  s   
z*TestMultiprocessCache.test_multiprocessingN)rX   rh   ri   r  r   r   r   r   r   r   r{   rx   r  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   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 sOw   Y  ttj	|d	d}t| j|d W d    d S 1 sqw   Y  d S )
Ntest_cache_file_locr   module_name_that_is_unlikely.fooz__init__.pyr   filezfoo.py)r   r#   rv   r   rw   rx   assertNotInr~   modname_bar1r   modname_bar2r   r   r   printsource_text_1source_text_2)rH   foomodfoutr&   r&   r'   r}   W  s   

"zTestCacheFileCollision.setUpc                 C   s2   t j| jd  t j| jd  t j| j d S rM   )rv   r~   r   r  r  r   r   r#   rN   r&   r&   r'   r   e  s   zTestCacheFileCollision.tearDownc                 C      t | jjS rM   )r
   r  barrN   r&   r&   r'   import_bar1j     z"TestCacheFileCollision.import_bar1c                 C   r  rM   )r
   r  r  rN   r&   r&   r'   import_bar2m  r  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)r  r  rl  rm  _index_namer|  
assertTrue
startswith)rH   bar1bar2idxname1idxname2r&   r&   r'   test_file_locationp  s   

z)TestCacheFileCollision.test_file_locationrZ   z)Test requires multiprocessing.get_contextc                 C   s>  |   }|  }t }| }W d    n1 sw   Y  | }| |dd | |dd | |dd | |dd t }| }W d    n1 sWw   Y  | }| |dd | |dd | |dd | |dd | || ztd}W n t	y   t
d Y nw | }|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 savedr7   z
data savedzindex loadedr   zdata loadedr  zmissing spawn contextrS   )r  r  r	   getvaluerG   countr|  rY   rZ   
ValueErrorr  r[   r\   cache_file_collision_testerr#   r  r  r^   r_   r   )rH   r  r  bufres1cachelogres2mpqrd   log1got1log2got2r&   r&   r'   test_no_collisionz  s^   z(TestCacheFileCollision.test_no_collisionN)rX   rh   ri   r  r   r   r   r   r   r   r{   rx   r  r  r}   r   r  r  r  rK  
skipUnlesshasattrrY   r  r&   r&   r&   r'   r  C  s     
r  c           	      C   s   t jd| t|j}t|j}t }| }W d    n1 s"w   Y  | |  | | t }| }W d    n1 sDw   Y  | |  | | d S )Nr   )rv   r   rw   r
   r  r	   rr   r  )	r  r#   r  r  r  r  r  r1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 s&w   Y  tj| jd| _	t| j	d}t| j
|d W d    d S 1 sMw   Y  d S )Nr  zfile1.pyr   r  zfile2.py)r   r#   r   r   r   file1r   r  source_text_file1file2source_text_file2)rH   r  r&   r&   r'   r}     s   
"z)TestCacheMultipleFilesWithSignature.setUpc                 C   s   t | j d S rM   )r    r!   r#   rN   r&   r&   r'   r     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   rv   r   r  r   r   r   rG   r   r  )rH   r   r   r   r`  r&   r&   r'   *test_caching_mutliple_files_with_signature  s   zNTestCacheMultipleFilesWithSignature.test_caching_mutliple_files_with_signatureN)	rX   rh   ri   r  r  r  r}   r   r  r&   r&   r&   r'   r    s    r  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.py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r1t
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 rM   )rk  rH   r   r&   r&   r'   check_module  r  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   r7   )
r   r   rG   r   r   r   r   div_usecaser  r   r  r&   r&   r'   r     s    




zTestCFuncCache.test_cachingN)rX   rh   ri   r   r   r   r   r   r   r{   rx   r   r  r   r&   r&   r&   r'   r    s    r  __main__)>r  llvmlite.bindingbindingrp  rY   numpyr   r   r    r   rv   rp   rK  r   numbar   
numba.corer   numba.core.cachingr   numba.core.errorsr   numba.parforsr   numba.tests.supportr   r   r	   r
   r   r   r   r   r   r   rG  ImportErrorr(   _access_preventable_access_msgr  rJ  r,   r6   r;   r?   rB   rD   rE   r]   rt   r   r   rO  r^  r  r  r  r  r  rX   mainr&   r&   r&   r'   <module>   sh    0	:6$   &Lt4
9