o
    ۾il                     @   sV  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
Z
d dlZd dlmZ d dlmZ d dlZd dlmZ d dlmZ d dlmZ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' zd dl(Z(W n e)y   dZ(Y nw d	d
 Z*e* Z+dZ,e	-e+e,Z.dd Z/dd Z0dd Z1dd Z2dd Z3dd Z4G dd dee Z5dd Z6G dd de Z7G dd de7Z8G d d! d!e8Z9G d"d# d#e8Z:G d$d% d%e8Z;e&G d&d' d'e8Z<G d(d) d)e8Z=G d*d+ d+e7Z>e%G d,d- d-e	j Z?d.d/ Z@G d0d1 d1e	j ZAG d2d3 d3e7ZBeCd4kr)e	D  dS dS )5    N)Path)njit)codegen)_UserWideCacheLocator_ZipCacheLocator)NumbaWarning)parfor)	SerialMixinTestCasecapture_cache_logimport_dynamicoverride_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'   L/home/ubuntu/.local/lib/python3.10/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 )Nabcd)hash)cr'   r'   r(   constant_unicode_cacheM   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_cacheR   s   

r7   c                   C   s
   dddS )N      )abr'   r'   r'   r'   r(   
dict_cache[   s   
r<   c                  C   s.   t } tdd| }|  }| }||ksJ d S r.   )r<   r   r1   r2   expgotr'   r'   r(   check_dict_cache_   s
   r@   c                  c   s    dD ]} | V  qd S )N)r8   r9      r'   )vr'   r'   r(   generator_cacheg   s   rC   c                  C   s6   t } tdd| }t|  }t| }||ksJ d S r.   )rC   r   listr=   r'   r'   r(   check_generator_cachel   s
   

rE   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_testu   s   zTestCaching.run_testc                 C      |  t d S N)rL   r7   rI   r'   r'   r(   test_constant_unicode_cachez      z'TestCaching.test_constant_unicode_cachec                 C   rM   rN   )rL   r@   rO   r'   r'   r(   test_dict_cache}   rQ   zTestCaching.test_dict_cachec                 C   rM   rN   )rL   rE   rO   r'   r'   r(   test_generator_cache   rQ   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failrH   )rI   	cache_dirctxresult_queueprocsuccessoutputr'   r'   r(   test_omitted   s>   

zTestCaching.test_omittedN)rY   
__module____qualname__rL   rP   rR   rS   rh   r'   r'   r'   r(   rF   t   s    rF   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/   rW   c                 S   s   | S rN   r'   )numr'   r'   r(   test   s   z(omitted_child_test_wrapper.<locals>.testr   r8   zCache did not hit as expectedzCache has an unexpected misszCache did not miss as expectedzCache has an unexpected hitF)rW   )r   r   _cache_hits
signatures_cache_misses	traceback
format_excput)rd   rb   second_callrm   rg   rf   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   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modfilerb   r!   copyusecases_filer    statS_IREADS_IWRITEmaxDiffrO   r'   r'   r(   setUp   s   

zBaseCacheTest.setUpc                 C   s"   t j| jd  t j| j d S rN   )rw   modulespopry   r   remover$   rO   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)rw   r   r   ry   
__cached__r   unlinkFileNotFoundErrorr   rH   __file__rstriprz   )rI   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   listdirrb   r   rO   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 rN   )r   r   getmtimer   rb   r   rO   r'   r(   	<genexpr>   s    $z1BaseCacheTest.get_cache_mtimes.<locals>.<genexpr>)dictsortedr   rO   r'   rO   r(   get_cache_mtimes   s   
zBaseCacheTest.get_cache_mtimesc                 C   s   |   }| t||| d S rN   )r   rH   len)rI   nr,   r'   r'   r(   check_pycache   s   zBaseCacheTest.check_pycachec                 C      d S rN   r'   rO   r'   r'   r(   
dummy_test      zBaseCacheTest.dummy_test)rY   ri   rj   r|   ry   r   r   r   r   r   r   r   r'   r'   r'   r(   ru      s    	ru   c                   @   sB   e Zd ZejeZejedZ	dZ
i dddZd	ddZdS )
DispatcherCacheUsecasesTestcache_usecases.pydispatcher_caching_test_fodderenvvarsc                C   s|   dt | j| jd }tj }|| tjt	j
d|gtjtj|d}| \}}|jdkr<td|j| | f d S )Nzif 1:
            import sys

            sys.path.insert(0, %(tempdir)r)
            mod = __import__(%(modname)r)
            mod.self_test()
            r$   ry   -c)stdoutstderrenvr   zBprocess failed with code %s: 
stdout follows
%s
stderr follows
%s
)r   r$   ry   r   environr{   update
subprocessPopenrw   
executablePIPEcommunicate
returncodeAssertionErrordecode)rI   r   codesubp_env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 rN   )statsrH   sum
cache_hitsvaluescache_misses)rI   rJ   hitsmissesstr'   r'   r(   
check_hits   s   z&DispatcherCacheUsecasesTest.check_hitsrN   )rY   ri   rj   r   r   dirnamer   herer   r|   ry   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 | 
  d S )Nr   r9   rA            @      @   r8   )r9   g     E@	   )r   r   add_usecaseassertPreciseEqualr   add_objmode_usecaserecord_returnaligned_arrtuple
packed_arrr   )rI   r   r&   recr'   r'   r(   test_caching*  s.   






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   r9   rA   r   r8   )r   r   r   r   nparanger   rI   r   r&   r'   r'   r(   test_caching_nrt_prunedH  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 )NrA   r9   r            @r   )r   r   innerr   outer_uncachedouterr   r'   r'   r(   test_inner_then_outerU  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 )NrA   r9   r   r   r   r   r   )r   r   r   r   r   r   r   r'   r'   r(   test_outer_then_inneri  s   


zTestCache.test_outer_then_innerc                 C   s.   |   }|j}| |ddd | d d S )Nr9   rA   r   r   )r   add_nocache_usecaser   r   r   r'   r'   r(   test_no_cachingw  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   r8   zBCannot cache compiled function "looplifted" as it uses lifted code)r   warningscatch_warningssimplefilterr   
loopliftedr   r   rH   r   assertInstrmessagerI   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 s,w   Y  | t|d | dt|d j d S )NTr   r   r   r8   zICannot cache compiled function "use_big_array" as it uses dynamic globals)r   r   r   r   r   use_big_arrayr   testingassert_equalbiggier   rH   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   r8   z#Cannot cache compiled function "{}")r   	use_c_sinuse_c_sin_nest1use_c_sin_nest2r   r   r   r   r   r   rH   r   r   formatrY   r   r   )rI   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errorrA   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 )Nr8   r   d   r9         )r   first_class_function_usecaserH   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 |  }| 	|jdd |   }| 
|| |j}|dd | 	|dd |dd | 	|dd |j}|dd | 	|dd | |  | |   | |  | d S )Nr9   rA   r   r   r   r8   )r   r   r   r   r   r   r   r   r   r   assertIsNotrH   r   )rI   r   mtimesmod2r&   r'   r'   r(   test_cache_reuse  s.   


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 )Nr9   rA   r   r:   z
Z = 10
   )r   r   r   r   rz   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 )Nr9   rA   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 )Nr9   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 )Nr8   )functionpassztests/dummy_module.pyexecT)dummy_moduler  __code__compileinspectgetfiler   from_functionassertIsNonerw   frozenassertIsInstance)rI   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_errorsr9   rA   r   r   r8   N)
r   r   
addCleanupr!   r"   r   
cache_pathr   r   r   )rI   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   r}   r$   st_moder    r(  r*  )rI   	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 )Nrv   r   )
r   r   r   r$   r   r}   r,  r    r(  r*  )rI   pycacher-  r'   r'   r(   test_non_writable_pycacheJ  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   ra   r:  pr   r   base_cmdinputfnrI   r'   r(   execute_with_inputx     

$z2TestCache.test_ipython.<locals>.execute_with_inputcache hits = 1)rw   r   r   check_outputCalledProcessErrorskipTestr   stripr   r   r   r   r$   r   r   r   )rI   verer&   rC  r   r'   r@  r(   test_ipythonW  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 )Nr1  r2  r3  r4  r5  r   )compilerr   r6  c                     r7  r8  r=  r>  r@  r'   r(   rC    rD  z4TestCache.test_ipykernel.<locals>.execute_with_inputrE  )rw   r   r   rF  rG  rH  r   rI  r   	ipykernelrM  get_tmp_directoryr   r   r   )rI   rJ  rK  rM  r&   rC  r   r'   r@  r(   test_ipykernel  s(   zTestCache.test_ipykernel)rY   ri   rj   r   r   r   r   r   r   r   r   r  r	  r  r  r  r  r&  r*  skip_bad_accessunittestskipIfr   namer.  r0  rL  rN  version_inforP  r'   r'   r'   r(   r   (  s@    
5r   c                       s,   e Zd Z fddZdd Zdd Z  ZS )TestCacheZipc                    sv   t    d}d}d}tj| j|}t|d}||| W d    n1 s*w   Y  t	j
d| d| _d S )NzI
from numba import jit

@jit(cache=True)
def add(x, y):
    return x + y
ztest_module.pyztest_archive.zipr   r   test_module)superr   r   r   r   r$   zipfileZipFilewritestrrw   rx   ry   )rI   mod_contentmod_filenamezip_filenamezip_pathzfrX   r'   r(   r     s   

zTestCacheZip.setUpc                 C   s   t jd t jdd  d S )Nr   rW  )rw   r   r   r   rO   r'   r'   r(   r     s   zTestCacheZip.tearDownc                 C   s   dd l }|dd}| |d | |jdd | |jd tjd= t  dd l }|dd}| |d | |jd d S )Nr   r9   rA   r   r8   rW  )rW  addrH   r   rw   r   	importlibinvalidate_caches)rI   rW  result1result2r'   r'   r(   test_zip_caching  s   zTestCacheZip.test_zip_caching)rY   ri   rj   r   r   rg  __classcell__r'   r'   ra  r(   rV    s    rV  c                   @   s    e Zd ZdZdd Zdd ZdS )TestCacheZipLibzR
    ZipCache tests that don't require the setup/teardown from `TestCacheZip`
    c                 C   sJ   dd }d}t ||}| | | |jttd | |jd d S )Nc                   S   r   rN   r'   r'   r'   r'   r(   	mock_func  r   z<TestCacheZipLib.test_zip_locator_creation.<locals>.mock_funcz/path/to/archive.zip/module.pyz/path/to/archive.zipz	module.py)r   r  assertIsNotNonerH   	_zip_pathr   r   _internal_path)rI   rj  r_  r%  r'   r'   r(   test_zip_locator_creation  s   
z)TestCacheZipLib.test_zip_locator_creationc                 C   s&   dd }d}t ||}| | d S )Nc                   S   r   rN   r'   r'   r'   r'   r(   rj    r   z@TestCacheZipLib.test_zip_locator_non_zip_path.<locals>.mock_funcz/path/to/module.py)r   r  r  )rI   rj  non_zip_pathr%  r'   r'   r(   test_zip_locator_non_zip_path  s   z-TestCacheZipLib.test_zip_locator_non_zip_pathN)rY   ri   rj   __doc__rn  rp  r'   r'   r'   r(   ri    s    ri  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)rX  rr  r   r   sequential_parfor_loweringrO   ra  r'   r(   r        
z TestSequentialParForsCache.setUpc                    rs  )NF)rX  rr  r   r   rt  rO   ra  r'   r(   r   #  ru  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>Fr9   )	r   r   parfor_usecaser   onesr   	overloadsr   rH   )rI   r   r&   arydynamic_globalsr'   r'   r(   r   (  s   

z'TestSequentialParForsCache.test_caching)rY   ri   rj   r   r   r   rh  r'   r'   ra  r(   rr    s    rr  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   r8   znothing to comparemsg)r   itemsassertGreaterEqualassertGreater)rI   
mtimes_oldmatch_countkrB   r'   r'   r(   check_later_mtimes8  s   
z*TestCacheWithCpuSetting.check_later_mtimesc                 C   s  |  d |  }|  t|  }|  }| jddid | | | t|  | |j	j
}|j}| }| t|d | \}}|d d t krW||}	}
n||}	}
| |	d d t  | |	d d t  | |
d d d | |
d d d d S )Nr   NUMBA_CPU_NAMEgenericr   r9   r8    )r   r   	self_testr   r   r   r   r  r  r   _cache_cache_file_load_indexrH   keysllget_host_cpu_namer   get_host_cpu_features)rI   r   
cache_sizer  r0   
cache_filecache_indexkey_akey_bkey_hostkey_genericr'   r'   r(   test_user_set_cpu_nameA  s(   


z.TestCacheWithCpuSetting.test_user_set_cpu_namec                 C   s(  |  d |  }|  t|  }|  }d}t }| || | j	d|id | 
| | t|  | |jj}|j}| }| t|d | \}	}
|	d d |kra|	|
}}n|
|	}}| |d d t  | |d d | | |d d t  | |d d | d S )Nr   z	-sse;-avxNUMBA_CPU_FEATURESr   r9   r8   )r   r   r  r   r   r   r   r  assertNotEqualr   r  r  r   r  r  r  rH   r  r  r  )rI   r   r  r  my_cpu_featuressystem_featuresr0   r  r  r  r  r  r  r'   r'   r(   test_user_set_cpu_features\  s2   


z2TestCacheWithCpuSetting.test_user_set_cpu_featuresN)rY   ri   rj   _numba_parallel_test_r  r  r  r'   r'   r'   r(   r~  4  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 )NrA   spawnr8   r9   )r   simple_usecase_callerrZ   r[   AttributeErrorPoolr   imaprangecloserH   )rI   r   r&   r   rc   poolrK   r'   r'   r(   test_multiprocessing  s   
z*TestMultiprocessCache.test_multiprocessingN)rY   ri   rj   r  r   r   r   r   r   r   r|   ry   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$   rw   r   rx   ry   assertNotInr   modname_bar1r   modname_bar2r   r   r   printsource_text_1source_text_2)rI   foomodfoutr'   r'   r(   r     s   

"zTestCacheFileCollision.setUpc                 C   s2   t j| jd  t j| jd  t j| j d S rN   )rw   r   r   r  r  r   r   r$   rO   r'   r'   r(   r     s   zTestCacheFileCollision.tearDownc                 C      t | jjS rN   )r   r  barrO   r'   r'   r(   import_bar1     z"TestCacheFileCollision.import_bar1c                 C   r  rN   )r   r  r  rO   r'   r'   r(   import_bar2  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  r  r  _index_namer  
assertTrue
startswith)rI   bar1bar2idxname1idxname2r'   r'   r(   test_file_location  s   

z)TestCacheFileCollision.test_file_locationr[   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 savedr8   z
data savedzindex loadedr   zdata loadedr  zmissing spawn contextrT   )r  r  r   getvaluerH   countr  rZ   r[   
ValueErrorr  r\   r]   cache_file_collision_testerr$   r  r  r_   r`   r   )rI   r  r  bufres1cachelogres2mpqre   log1got1log2got2r'   r'   r(   test_no_collision  s^   z(TestCacheFileCollision.test_no_collisionN)rY   ri   rj   r  r   r   r   r   r   r   r|   ry   r  r  r   r   r  r  r  rR  
skipUnlesshasattrrZ   r  r'   r'   r'   r(   r    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   )rw   r   rx   r   r  r   rs   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)rI   r  r'   r'   r(   r   4  s   
"z)TestCacheMultipleFilesWithSignature.setUpc                 C   s   t | j d S rN   )r!   r"   r$   rO   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 )Nr   r   zstdout:
z


stderr:
r   r  )r   r   rw   r   r  r   r   r   rH   r   r  )rI   r   r   r   r  r'   r'   r(   *test_caching_mutliple_files_with_signatureB  s   zNTestCacheMultipleFilesWithSignature.test_caching_mutliple_files_with_signatureN)	rY   ri   rj   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$   ry   r   r   rw   r   r   r   r   r   r   )rI   r   r   r   r   r'   r'   r(   r   Z  s   

z&TestCFuncCache.run_in_separate_processc                 C   s   |   d S rN   )r  rI   r   r'   r'   r(   check_moduler  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   r8   )
r   r   rH   r   r   r   r   div_usecaser  r   r  r'   r'   r(   r   u  s    




zTestCFuncCache.test_cachingN)rY   ri   rj   r   r   r   r   r   r   r|   ry   r   r  r   r'   r'   r'   r(   r  T  s    r  __main__)Erc  r  rZ   r   r!   r}   r   rw   rq   rR  r   rY  pathlibr   llvmlite.bindingbindingr  numpyr   numbar   
numba.corer   numba.core.cachingr   r   numba.core.errorsr   numba.parforsr   numba.tests.supportr	   r
   r   r   r   r   r   r   r   rN  ImportErrorr)   _access_preventable_access_msgr  rQ  r-   r7   r<   r@   rC   rE   rF   r^   ru   r   r   rV  ri  rr  r~  r  r  r  r  r  rY   mainr'   r'   r'   r(   <module>   st    ,	:7'   !;Lt4
9