o
    'ѹgl                     @   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_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!   \/Users/admin/.pyenv/versions/3.10.0/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 )NZ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cfuncZexp_hvZexp_strZgot_hvZgot_strr!   r!   r"   check_constant_unicode_cacheR   s   

r,   c                   C   s
   dddS )N      )abr!   r!   r!   r!   r"   
dict_cache[   s   
r1   c                  C   s.   t } tdd| }|  }| }||ksJ d S r'   )r1   r   r*   r+   expgotr!   r!   r"   check_dict_cache_   s
   r5   c                  c   s    dD ]} | V  qd S )N)r-   r.      r!   )vr!   r!   r"   generator_cacheg   s   r8   c                  C   s6   t } tdd| }t|  }t| }||ksJ d S r'   )r8   r   listr2   r!   r!   r"   check_generator_cachel   s
   

r:   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)rA   r,   r>   r!   r!   r"   test_constant_unicode_cachez      z'TestCaching.test_constant_unicode_cachec                 C   rB   rC   )rA   r5   rD   r!   r!   r"   test_dict_cache}   rF   zTestCaching.test_dict_cachec                 C   rB   rC   )rA   r:   rD   r!   r!   r"   test_generator_cache   rF   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failr=   )r>   	cache_dirctxresult_queueprocsuccessoutputr!   r!   r"   test_omitted   s>   

zTestCaching.test_omittedN)rN   
__module____qualname__rA   rE   rG   rH   r]   r!   r!   r!   r"   r;   t   s    r;   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 )NZ	CACHE_DIRTr(   rL   c                 S   s   | S rC   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)rL   )r   r   Z_cache_hitsZ
signaturesZ_cache_misses	traceback
format_excput)rY   rW   Zsecond_callra   r\   r[   r!   r!   r"   rS      s0   "rS   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modfilerW   r   copyusecases_filer   statS_IREADS_IWRITEZmaxDiffrD   r!   r!   r"   setUp   s   

zBaseCacheTest.setUpc                 C   s"   t j| jd  t j| j d S rC   )rg   modulespopri   r   remover   rD   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)rg   rq   rr   ri   
__cached__r   unlinkFileNotFoundErrorr   r=   __file__rstriprj   )r>   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   listdirrW   rx   rD   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 rC   )r   r   getmtimer   rW   r   rD   r!   r"   	<genexpr>   s    $z1BaseCacheTest.get_cache_mtimes.<locals>.<genexpr>)dictsortedr   rD   r!   rD   r"   get_cache_mtimes   s   
zBaseCacheTest.get_cache_mtimesc                 C   s   |   }| t||| d S rC   )r   r=   len)r>   nr%   r!   r!   r"   check_pycache   s   zBaseCacheTest.check_pycachec                 C      d S rC   r!   rD   r!   r!   r"   
dummy_test      zBaseCacheTest.dummy_test)rN   r^   r_   rl   ri   rp   rt   r   r   r   r   r   r!   r!   r!   r"   re      s    	re   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   ri   -c)stdoutstderrenvr   zBprocess failed with code %s: 
stdout follows
%s
stderr follows
%s
)r   r   ri   r   environrk   update
subprocessPopenrg   
executablePIPEcommunicate
returncodeAssertionErrordecode)r>   r   codeZ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 rC   )statsr=   sum
cache_hitsvaluesZcache_misses)r>   r?   hitsmissesstr!   r!   r"   
check_hits   s   z&DispatcherCacheUsecasesTest.check_hitsrC   )rN   r^   r_   r   r   dirnamery   herer   rl   ri   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   r.   r6            @      @   r-   )r.   g     E@	   )r   r   add_usecaseassertPreciseEqualr   add_objmode_usecaserecord_returnaligned_arrtuple
packed_arrr   )r>   r~   r    Z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   r.   r6   r   r-   )r   r   r   r   npZaranger   r>   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 )Nr6   r.   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 )Nr6   r.   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 )Nr.   r6   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   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 s,w   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 ]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   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   formatrN   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    d S 1 sKw   Y  d S )Nerrorr6   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 |  }| 	|jdd |   }| 
|| |j}|dd | 	|dd |dd | 	|dd |j}|dd | 	|dd | |  | |   | |  | d S )Nr.   r6   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  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 )Nr.   r6   r   r/   z
Z = 10
   )r   r   r   r   rj   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.   r6   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}z*||_t|}t||}| | dt	_
t||}| |t W ||_t	`
d S ||_t	`
w )Nr-   )functionpassztests/dummy_module.pyexecT)Zdummy_moduler   __code__compileinspectgetfiler   from_functionassertIsNonerg   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.   r6   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   rm   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 )Nrf   r   )
r   r   r   r   r   rm   r   r   r   r   )r>   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   rV   r  pr   r   Zbase_cmdZinputfnr>   r!   r"   execute_with_inputx     

$z2TestCache.test_ipython.<locals>.execute_with_inputcache hits = 1)rg   r   r   check_outputCalledProcessErrorskipTestr   stripr   r   r   r   r   r   r   r   )r>   verer    r  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 )Nr  r  r  r  r	  r   )compilerr   r
  c                     r  r  r  r  r  r!   r"   r    r  z4TestCache.test_ipykernel.<locals>.execute_with_inputr  )rg   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)rN   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                       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)superrp   r   r   r   r   zipfileZipFilewritestrrg   rh   ri   )r>   Zmod_contentZmod_filenamezip_filenamezip_pathzfrM   r!   r"   rp     s   

zTestCacheZip.setUpc                 C   s   t jd t jdd  d S )Nr   r'  )rg   r   rr   rq   rD   r!   r!   r"   rt     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   r.   r6   r   r-   r'  )r'  addr=   r   rg   rq   	importlibinvalidate_caches)r>   r'  Zresult1Zresult2r!   r!   r"   test_zip_caching  s   zTestCacheZip.test_zip_caching)rN   r^   r_   rp   rt   r3  __classcell__r!   r!   r/  r"   r&    s    r&  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   rC   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   ZassertIsNotNoner=   Z	_zip_pathr   r   Z_internal_path)r>   r6  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   rC   r!   r!   r!   r!   r"   r6    r   z@TestCacheZipLib.test_zip_locator_non_zip_path.<locals>.mock_funcz/path/to/module.py)r   r   r   )r>   r6  Znon_zip_pathr   r!   r!   r"   test_zip_locator_non_zip_path  s   z-TestCacheZipLib.test_zip_locator_non_zip_pathN)rN   r^   r_   __doc__r7  r8  r!   r!   r!   r"   r5    s    r5  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)r(  r:  rp   r   sequential_parfor_loweringrD   r/  r!   r"   rp        
z TestSequentialParForsCache.setUpc                    r;  )NF)r(  r:  rt   r   r<  rD   r/  r!   r"   rt   #  r=  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)rN   r^   r_   rp   rt   r   r4  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kr7   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   ZNUMBA_CPU_NAMEZgenericr   r.   r-    )r   r   	self_testr   r   r   r   rE  rB  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_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;-avxZNUMBA_CPU_FEATURESr   r.   r-   )r   r   rG  r   r   r   r   rN  assertNotEqualr   rE  rB  r   rH  rI  rJ  r=   rK  rL  rM  )r>   r~   rO  r   Zmy_cpu_featuresZsystem_featuresr)   rP  rQ  rR  rS  rT  rU  r!   r!   r"   test_user_set_cpu_features\  s2   


z2TestCacheWithCpuSetting.test_user_set_cpu_featuresN)rN   r^   r_   _numba_parallel_test_rE  rV  rX  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 )Nr6   spawnr-   r.   )r   Zsimple_usecase_callerrO   rP   AttributeErrorZPoolr   imaprangecloser=   )r>   r~   r    r   rX   poolr@   r!   r!   r"   test_multiprocessing  s   
z*TestMultiprocessCache.test_multiprocessingN)rN   r^   r_   rY  r   r   r   ry   r   r   rl   ri   ra  r!   r!   r!   r"   rZ    s    rZ  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 sOw   Y  ttj	|d	d}t| j|d W d    d S 1 sqw   Y  d S )
Ntest_cache_file_locr   Zmodule_name_that_is_unlikely.Zfooz__init__.pyr   filezfoo.py)r   r   rg   r   rh   ri   ZassertNotInrq   modname_bar1r   modname_bar2r   r   r   printsource_text_1source_text_2)r>   Zfoomodfoutr!   r!   r"   rp     s   

"zTestCacheFileCollision.setUpc                 C   s2   t j| jd  t j| jd  t j| j d S rC   )rg   rq   rr   rg  rh  r   rs   r   rD   r!   r!   r"   rt     s   zTestCacheFileCollision.tearDownc                 C      t | jjS rC   )r   rg  barrD   r!   r!   r"   import_bar1     z"TestCacheFileCollision.import_bar1c                 C   rm  rC   )r   rh  rn  rD   r!   r!   r"   import_bar2  rp  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)ro  rq  rH  rI  Z_index_namerW  
assertTrue
startswith)r>   bar1bar2Zidxname1Zidxname2r!   r!   r"   test_file_location  s   

z)TestCacheFileCollision.test_file_locationrP   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 savedr-   z
data savedzindex loadedr   zdata loadedr[  zmissing spawn contextrI   )ro  rq  r   getvaluer=   countrW  rO   rP   
ValueErrorri  rQ   rR   cache_file_collision_testerr   rg  rh  rT   rU   r   )r>   rt  ru  bufZres1ZcachelogZres2mpqrZ   Zlog1Zgot1log2Zgot2r!   r!   r"   test_no_collision  s^   z(TestCacheFileCollision.test_no_collisionN)rN   r^   r_   rY  r   r   r   ry   r   r   rl   ri   rj  rk  rp   rt   ro  rq  rv  r#  
skipUnlesshasattrrO   r  r!   r!   r!   r"   rb    s     
rb  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   )rg   r   rh   r   rn  r   rd   rw  )	r}  r   rg  rh  rt  ru  r{  Zr1Zr2r!   r!   r"   rz    s   


rz  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 )Nrc  zfile1.pyr   re  zfile2.py)r   r   r   r   r   file1r   ri  source_text_file1file2source_text_file2)r>   rl  r!   r!   r"   rp   4  s   
"z)TestCacheMultipleFilesWithSignature.setUpc                 C   s   t | j d S rC   )r   r   r   rD   r!   r!   r"   rt   ?  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   rg   r   r  r   r   r   r=   r   r  )r>   r   r   r   r@  r!   r!   r"   *test_caching_mutliple_files_with_signatureB  s   zNTestCacheMultipleFilesWithSignature.test_caching_mutliple_files_with_signatureN)	rN   r^   r_   rY  r  r  rp   rt   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.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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   ri   r   r   rg   r   r   r   r   r   r   )r>   r   r   r   r   r!   r!   r"   r   Z  s   

z&TestCFuncCache.run_in_separate_processc                 C   s   |   d S rC   )rG  r>   r~   r!   r!   r"   check_moduler  rp  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_usecaser  r   r  r!   r!   r"   r   u  s    




zTestCFuncCache.test_cachingN)rN   r^   r_   r   r   r   ry   r   r   rl   ri   r   r  r   r!   r!   r!   r"   r  T  s    r  __main__)Er1  r   rO   r   r   rm   r   rg   rb   r#  r   r)  pathlibr   Zllvmlite.bindingZbindingrL  Znumpyr   Znumbar   Z
numba.corer   Znumba.core.cachingr   r   Znumba.core.errorsr   Znumba.parforsr   Znumba.tests.supportr	   r
   r   r   r   r   r   r   r   r   ImportErrorr#   Z_access_preventableZ_access_msgr  r"  r&   r,   r1   r5   r8   r:   r;   rS   re   r   r   r&  r5  r:  r>  rZ  rb  rz  r  r  rN   mainr!   r!   r!   r"   <module>   st    ,	:7'   !;Lt4
9