o
    1Æ&iŸµ  ã                   @   s®  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 d dlmZmZmZ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,m-Z- d d
l.m/Z/ zd dl0Z0W n e1y›   dZ0Y nw dd„ Z2e2ƒ Z3dZ4e	 5e3e4¡Z6dd„ Z7dd„ Z8dd„ Z9dd„ Z:dd„ Z;dd„ Z<G dd„ de$e%ƒZ=dd„ Z>G dd„ de%ƒZ?G d d!„ d!e?ƒZ@G d"d#„ d#e@ƒZAG d$d%„ d%e@ƒZBG d&d'„ d'e@ƒZCe+G d(d)„ d)e@ƒƒZDG d*d+„ d+e@ƒZEG d,d-„ d-e?ƒZFe*G d.d/„ d/e	j%ƒƒZGd0d1„ ZHG d2d3„ d3e	j%ƒZIG d4d5„ d5e?ƒZJG d6d7„ d7eƒZKG d8d9„ d9e%ƒZLG d:d;„ d;e%ƒZMeNd<krUe	 O¡  dS dS )=é    N)ÚPath)Úfloor)Únjit)Úcodegen)ÚUserWideCacheLocatorÚZipCacheLocatorÚFunctionCacheÚInTreeCacheLocatorÚInTreeCacheLocatorFsAgnostic)ÚNumbaWarning)Úparfor)
ÚSerialMixinÚTestCaseÚcapture_cache_logÚimport_dynamicÚoverride_configÚrun_in_new_process_cachingÚskip_if_typeguardÚskip_parfors_unsupportedÚtemp_directoryÚoverride_env_config)Ú
cpu_targetc               
   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'   úCC:\wamp64\www\opt\env\Lib\site-packages\numba/tests/test_caching.pyÚcheck_access_is_preventable3   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_cacheW   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_cache\   s   

r3   c                   C   s
   dddœS )Né   é   )ÚaÚbr'   r'   r'   r'   r(   Ú
dict_cachee   s   
r8   c                  C   s.   t } tdd| ƒ}| ƒ }|ƒ }||ksJ ‚d S r.   )r8   r   ©r1   r2   ÚexpÚgotr'   r'   r(   Úcheck_dict_cachei   s
   r<   c                  c   s    dD ]} | V  qd S )N)r4   r5   é   r'   )Úvr'   r'   r(   Úgenerator_cacheq   s   €ÿr?   c                  C   s6   t } tdd| ƒ}t| ƒ ƒ}t|ƒ ƒ}||ksJ ‚d S r.   )r?   r   Úlistr9   r'   r'   r(   Úcheck_generator_cachev   s
   

rA   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_test   s   zTestCaching.run_testc                 C   ó   |   t¡ d S ©N)rH   r3   ©rE   r'   r'   r(   Útest_constant_unicode_cache„   ó   z'TestCaching.test_constant_unicode_cachec                 C   rI   rJ   )rH   r<   rK   r'   r'   r(   Útest_dict_cache‡   rM   zTestCaching.test_dict_cachec                 C   rI   rJ   )rH   rA   rK   r'   r'   r(   Útest_generator_cacheŠ   rM   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ÚfailrD   )rE   Ú	cache_dirÚctxÚresult_queueÚprocÚsuccessÚoutputr'   r'   r(   Útest_omitted   s>   þ
ýþ
ýzTestCaching.test_omittedN)rU   Ú
__module__Ú__qualname__rH   rL   rN   rO   rd   r'   r'   r'   r(   rB   ~   s    rB   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/   rS   c                 S   s   | S rJ   r'   )Únumr'   r'   r(   Útestº   s   z(omitted_child_test_wrapper.<locals>.testr   r4   zCache did not hit as expectedzCache has an unexpected misszCache did not miss as expectedzCache has an unexpected hitF)rS   )r   r   Z_cache_hitsZ
signaturesZ_cache_missesÚ	tracebackÚ
format_excÚput)r`   r^   Zsecond_callrh   rc   rb   r'   r'   r(   rZ   ¸   s0   ÿÿÿÿ"èrZ   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Úmodfiler^   r"   ÚcopyÚusecases_filer!   ÚstatÚS_IREADÚS_IWRITEZmaxDiffrK   r'   r'   r(   ÚsetUpÚ   s   

zBaseCacheTest.setUpc                 C   s"   t j | jd ¡ t j | j¡ d S rJ   )rn   ÚmodulesÚpoprp   r   Úremover%   rK   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)rn   rx   ry   rp   Ú
__cached__r   ÚunlinkÚFileNotFoundErrorr   rD   Ú__file__Úrstriprq   )rE   Ú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   Úlistdirr^   r   rK   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 rJ   )r   r   Úgetmtimer   r^   rˆ   rK   r'   r(   Ú	<genexpr>   s   € $ÿz1BaseCacheTest.get_cache_mtimes.<locals>.<genexpr>)ÚdictÚsortedrŒ   rK   r'   rK   r(   Úget_cache_mtimesÿ   s   
ÿzBaseCacheTest.get_cache_mtimesc                 C   s   |   ¡ }|  t|ƒ||¡ d S rJ   )rŒ   rD   Úlen)rE   Únr,   r'   r'   r(   Úcheck_pycache  s   zBaseCacheTest.check_pycachec                 C   ó   d S rJ   r'   rK   r'   r'   r(   Ú
dummy_test  ó   zBaseCacheTest.dummy_test)rU   re   rf   rs   rp   rw   r{   r†   rŒ   r‘   r”   r–   r'   r'   r'   r(   rl   Ô   s    	rl   c                   @   sB   e Zd Zej e¡Zej ed¡Z	dZ
ddœdd„Zd	dd„ZdS )
ÚDispatcherCacheUsecasesTestúcache_usecases.pyÚdispatcher_caching_test_fodderN©Úenvvarsc                C   sˆ   |d u ri }dt | j| jd }tj ¡ }| |¡ 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%   rp   ú-c)ÚstdoutÚstderrÚenvr   zBprocess failed with code %s: 
stdout follows
%s
stderr follows
%s
)r   r%   rp   r   Úenvironrr   ÚupdateÚ
subprocessÚPopenrn   Ú
executableÚPIPEÚcommunicateÚ
returncodeÚAssertionErrorÚdecode)rE   rœ   ÚcodeZsubp_envÚpopenÚoutÚerrr'   r'   r(   Úrun_in_separate_process  s&   ú

þ
ýÿÿz3DispatcherCacheUsecasesTest.run_in_separate_processc                 C   sJ   |j }|  t|j ¡ ƒ||j¡ |d ur#|  t|j ¡ ƒ||j¡ d S d S rJ   )ÚstatsrD   ÚsumÚ
cache_hitsÚvaluesZcache_misses)rE   rF   ÚhitsÚmissesÚstr'   r'   r(   Ú
check_hits,  s   ÿÿz&DispatcherCacheUsecasesTest.check_hitsrJ   )rU   re   rf   r   r   Údirnamer€   Úherer   rs   rp   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   r5   r=   é   ç      @ç      @é   r4   )r5   g     ÀE@é	   )r”   r†   Úadd_usecaseÚassertPreciseEqualr¸   Úadd_objmode_usecaseÚrecord_returnÚaligned_arrÚtupleÚ
packed_arrr°   )rE   r…   r&   Zrecr'   r'   r(   Útest_caching6  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   r5   r=   r¼   r4   )r”   r†   rÁ   rÂ   ÚnpZaranger¸   ©rE   r…   r&   r'   r'   r(   Útest_caching_nrt_prunedT  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=   r5   r¼   é   ç      @r½   )r†   rÂ   Úinnerr”   Úouter_uncachedÚouterrÊ   r'   r'   r(   Útest_inner_then_outera  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=   r5   rÌ   r¼   rÍ   r¾   r¿   )r†   rÂ   rÐ   r”   rÏ   rÎ   rÊ   r'   r'   r(   Útest_outer_then_inneru  s   


zTestCache.test_outer_then_innerc                 C   s.   |   ¡ }|j}|  |ddƒd¡ |  d¡ d S )Nr5   r=   r¼   r   )r†   Úadd_nocache_usecaserÂ   r”   rÊ   r'   r'   r(   Útest_no_cachingƒ  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   r4   zBCannot cache compiled function "looplifted" as it uses lifted code)r†   ÚwarningsÚcatch_warningsÚsimplefilterr   Z
loopliftedrÂ   r”   rD   r’   ÚassertInÚstrÚmessage©rE   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   r4   zICannot cache compiled function "use_big_array" as it uses dynamic globals)r†   rØ   rÙ   rÚ   r   Zuse_big_arrayrÉ   ÚtestingZassert_equalZbiggier”   rD   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   r4   z#Cannot cache compiled function "{}")r†   Z	use_c_sinZuse_c_sin_nest1Zuse_c_sin_nest2rØ   rÙ   rÚ   r   rÂ   r”   rD   r’   rÛ   ÚformatrU   rÜ   rÝ   )rE   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   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 )Nr4   rç   éd   r5   é   é   )r†   Zfirst_class_function_usecaserD   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 )Nr5   r=   r½   rÍ   r   r4   )r†   rÁ   rÃ   rÏ   rÐ   rÄ   rÇ   rÅ   r‘   r¸   ZassertIsNotrD   r°   )rE   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 )Nr5   r=   r¼   r6   z
Z = 10
é   )r†   rÁ   rÂ   r   rq   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 )Nr5   r=   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 )Nr5   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 )Nr4   ©ÚfunctionÚpassztests/dummy_module.pyÚexecT)Údummy_moduler÷   Ú__code__ÚcompileÚinspectÚgetfiler   Úfrom_functionÚassertIsNonern   ÚfrozenÚassertIsInstance)rE   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_errorsr5   r=   r¼   r   r4   N)
r†   rÁ   Ú
addCleanupr"   r#   r±   Ú
cache_pathrÂ   r¸   r”   )rE   r…   r&   rï   r'   r'   r(   Ú_test_pycache_fallback2  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   rt   r%   Úst_moder!   r  r	  )rE   Ú	old_permsr'   r'   r(   Útest_non_creatable_pycacheK  s   z$TestCache.test_non_creatable_pycachec                 C   sN   t j | jd¡}t  |¡ t  |¡j}t  |d¡ |  t j||¡ |  	¡  d S )Nrm   r   )
r   r   r   r%   r   rt   r  r!   r  r	  )rE   Úpycacher  r'   r'   r(   Útest_non_writable_pycacheV  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]   ©r  Úpr®   r¯   ©Zbase_cmdZinputfnrE   r'   r(   Úexecute_with_input„  ó   ý

ý$õz2TestCache.test_ipython.<locals>.execute_with_inputúcache hits = 1)rn   r¦   r¤   Úcheck_outputÚCalledProcessErrorÚskipTestr©   Ústripr«   r   r   r   r%   r   r    rÛ   )rE   ÚverÚer&   r   r¯   r'   r  r(   Útest_ipythonc  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"  )rn   r¦   r¤   r#  r$  r%  r©   r&  r«   Ú	ipykernelr*  Zget_tmp_directoryr   r    rÛ   )rE   r'  r(  r*  r&   r   r¯   r'   r  r(   Útest_ipykernel˜  s(   ÿ€ÿÿzTestCache.test_ipykernel)rU   re   rf   rÈ   rË   rÑ   rÒ   rÔ   rà   râ   rä   ré   rí   rð   rò   rô   rõ   r  r	  Úskip_bad_accessÚunittestÚskipIfr   Únamer  r  r)  r+  Úversion_infor,  r'   r'   r'   r(   r»   4  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)Úsuperrw   r   r   r   r%   ÚzipfileÚZipFileÚwritestrrn   ro   rp   )rE   Zmod_contentZmod_filenameÚzip_filenameÚzip_pathÚzf©rT   r'   r(   rw   Ô  s   
ÿ
zTestCacheZip.setUpc                 C   s   t j d¡ t j dd ¡ d S )Nr   r3  )rn   r   ry   rx   rK   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   r5   r=   r¿   r4   r3  )r3  ÚaddrD   r¸   rn   rx   Ú	importlibÚinvalidate_caches)rE   r3  Zresult1Zresult2r'   r'   r(   Útest_zip_cachingñ  s   zTestCacheZip.test_zip_caching)rU   re   rf   rw   r{   r?  Ú__classcell__r'   r'   r;  r(   r2  Ò  s    r2  c                   @   ó    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•   rJ   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ÿ   ÚassertIsNotNonerD   Z	_zip_pathrÜ   r   Z_internal_path)rE   rC  r9  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•   rJ   r'   r'   r'   r'   r(   rC    r—   z@TestCacheZipLib.test_zip_locator_non_zip_path.<locals>.mock_funcz/path/to/module.py)r   rÿ   r   )rE   rC  Znon_zip_pathr  r'   r'   r(   Útest_zip_locator_non_zip_path  s   z-TestCacheZipLib.test_zip_locator_non_zip_pathN)rU   re   rf   Ú__doc__rE  rF  r'   r'   r'   r(   rB    s    rB  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)r4  rH  rw   r   Úsequential_parfor_loweringrK   r;  r'   r(   rw   *  ó   
z TestSequentialParForsCache.setUpc                    rI  )NF)r4  rH  r{   r   rJ  rK   r;  r'   r(   r{   /  rK  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>Fr5   )	r†   r”   Zparfor_usecaserÉ   ZonesrÂ   Z	overloadsr´   rD   )rE   r…   r&   ZaryZdynamic_globalsr'   r'   r(   rÈ   4  s   

ÿz'TestSequentialParForsCache.test_caching)rU   re   rf   rw   r{   rÈ   r@  r'   r'   r;  r(   rH  (  s    rH  c                   @   s<   e Zd ZdZdd„ Ze e ¡ dkd¡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   r4   znothing to compare©Úmsg)r‘   ÚitemsZassertGreaterEqualÚassertGreater)rE   Z
mtimes_oldÚmatch_countÚkr>   r'   r'   r(   Úcheck_later_mtimesD  s   €
ÿz*TestCacheWithCpuSetting.check_later_mtimesÚgenericzLLVM detected 'generic' CPUc                 C   s  |   d¡ | jt ¡ ddœd |  ¡ }t|  ¡ ƒ}| jdddœd |  |¡ |  t|  ¡ ƒ|¡ |  	¡ }|j
j}|j}| ¡ }|  t|ƒd¡ | ¡ \}}|d d dkr]||}	}
n||}
}	|  |
d d t ¡ ¡ |  |
d d d¡ |  |	d d d¡ |  |	d d d¡ d S )Nr   Ú )ZNUMBA_CPU_NAMEÚNUMBA_CPU_FEATURESr›   rT  r5   r4   )r”   r°   ÚllÚget_host_cpu_namer‘   r’   rŒ   rS  rP  r†   rÁ   Ú_cacheÚ_cache_fileÚ_load_indexrD   Úkeys)rE   rî   Ú
cache_sizer…   r0   Ú
cache_fileÚcache_indexÚkey_aÚkey_bZkey_genericÚkey_hostr'   r'   r(   Útest_user_set_cpu_nameM  s6   
ÿÿ
þ

z.TestCacheWithCpuSetting.test_user_set_cpu_namec                 C   s:  |   d¡ tj ¡ }t ¡ }| jd|id t|  ¡ ƒ}|  ¡ }d}|  	||¡ | jd|id |  
|¡ |  t|  ¡ ƒ|¡ |  ¡ }|jj}|j}| ¡ }	|  t|	ƒd¡ |	 ¡ \}
}|
d d |krj|
|}}n|
|}}|  |d d | ¡ ¡ |  |d d |¡ |  |d d | ¡ ¡ |  |d d |¡ d S )Nr   rV  r›   z	-sse;-avxr5   r4   )r”   r   Ztarget_contextr   Zget_host_cpu_featuresr°   r’   rŒ   r‘   ÚassertNotEqualrS  rP  r†   rÁ   rY  rZ  r[  rD   r\  Z_get_host_cpu_name)rE   Zcpu_codegenZsystem_featuresr]  rî   Zmy_cpu_featuresr…   r0   r^  r_  r`  ra  Zkey_modifiedrb  r'   r'   r(   Útest_user_set_cpu_featuresr  s8   

ÿÿ

z2TestCacheWithCpuSetting.test_user_set_cpu_featuresN)rU   re   rf   Ú_numba_parallel_test_rS  r.  r/  rW  rX  rc  re  r'   r'   r'   r(   rL  @  s    	ÿ
#rL  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=   Úspawnr4   r5   )r†   Zsimple_usecase_callerrV   rW   ÚAttributeErrorZPoolr²   ÚimapÚrangeÚcloserD   )rE   r…   r&   r“   r_   ÚpoolrG   r'   r'   r(   Útest_multiprocessing¦  s   ÿ
z*TestMultiprocessCache.test_multiprocessingN)rU   re   rf   rf  r   r   r¹   r€   rº   r   rs   rp   rn  r'   r'   r'   r(   rg  œ  s    rg  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%   rn   r   ro   rp   ZassertNotInrx   Úmodname_bar1r   Úmodname_bar2r   r   r   ÚprintÚsource_text_1Úsource_text_2)rE   ZfoomodÚfoutr'   r'   r(   rw   Í  s   

ÿ"ÿzTestCacheFileCollision.setUpc                 C   s2   t j | jd ¡ t j | jd ¡ t j | j¡ d S rJ   )rn   rx   ry   rt  ru  r   rz   r%   rK   r'   r'   r(   r{   Û  s   zTestCacheFileCollision.tearDownc                 C   ó   t | jƒjS rJ   )r   rt  ÚbarrK   r'   r'   r(   Úimport_bar1à  ó   z"TestCacheFileCollision.import_bar1c                 C   rz  rJ   )r   ru  r{  rK   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~  rY  rZ  Z_index_namerd  Ú
assertTrueÚ
startswith)rE   Úbar1Úbar2Zidxname1Zidxname2r'   r'   r(   Útest_file_locationæ  s   

z)TestCacheFileCollision.test_file_locationrW   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 savedr4   z
data savedzindex loadedr   zdata loadedrh  zmissing spawn contextrP   )r|  r~  r   ÚgetvaluerD   Úcountrd  rV   rW   Ú
ValueErrorrv  rX   rY   Úcache_file_collision_testerr%   rt  ru  r[   r\   r   )rE   r  r‚  ÚbufZres1ZcachelogZres2ÚmpÚqra   Zlog1Zgot1Úlog2Zgot2r'   r'   r(   Útest_no_collisionð  s^   ÿÿÿþÿz(TestCacheFileCollision.test_no_collisionN)rU   re   rf   rf  r   r   r¹   r€   rº   r   rs   rp   rw  rx  rw   r{   r|  r~  rƒ  r.  Ú
skipUnlessÚhasattrrV   rŒ  r'   r'   r'   r(   ro  ¹  s     
ÿro  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   )rn   r   ro   r   r{  r   rk   r„  )	rŠ  r%   rt  ru  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 )Nrp  zfile1.pyrß   rr  zfile2.py)r   r%   r   r   r   Úfile1r   rv  Úsource_text_file1Úfile2Úsource_text_file2)rE   ry  r'   r'   r(   rw   P  s   
ÿ"ÿz)TestCacheMultipleFilesWithSignature.setUpc                 C   s   t  | j¡ d S rJ   )r"   r#   r%   rK   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   rM  )r¤   r¥   rn   r¦   r’  r§   r¨   r«   rD   r©   r”  )rE   r­   r®   r¯   rN  r'   r'   r(   Ú*test_caching_mutliple_files_with_signature^  s   þþzNTestCacheMultipleFilesWithSignature.test_caching_mutliple_files_with_signatureN)	rU   re   rf   rf  r“  r•  rw   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.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%   rp   r¤   r¥   rn   r¦   r§   r¨   r©   rª   r«   )rE   r¬   r­   r®   r¯   r'   r'   r(   r°   v  s   óÿ

ÿÿz&TestCFuncCache.run_in_separate_processc                 C   s   |  ¡  d S rJ   )Z	self_test©rE   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¼   r4   )
r”   r†   rD   rÁ   r³   rÐ   rÓ   Zdiv_usecaser›  r°   rš  r'   r'   r(   rÈ   ‘  s    




zTestCFuncCache.test_cachingN)rU   re   rf   r   r   r¹   r€   rº   r   rs   rp   r°   r›  rÈ   r'   r'   r'   r(   r˜  p  s    r˜  c                   @   s   e Zd ZdS )ÚTestLocatorN)rU   re   rf   r'   r'   r'   r(   rœ  ©  s    rœ  c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )Ú&TestCacheLocatorEnvironmentIntegrationz9Integration tests for environment variable functionality.c              	   C   sz   dd„ }t ddƒ* |  t¡ t|ƒ W d   ƒ n1 sw   Y  W d   ƒ d S W d   ƒ d S 1 s6w   Y  d S )Nc                   S   ó   dS ©Nr  r'   r'   r'   r'   r(   rC  ±  r—   z\TestCacheLocatorEnvironmentIntegration.test_locators_env_override_unknown.<locals>.mock_funcÚNUMBA_CACHE_LOCATOR_CLASSESzfoo,bar)r   ÚassertRaisesÚRuntimeErrorr   )rE   rC  r'   r'   r(   Ú"test_locators_env_override_unknown°  s   þ
ÿü"üzITestCacheLocatorEnvironmentIntegration.test_locators_env_override_unknownc                 C   sT   dd„ }t ddƒ t|ƒ}t}|  |jj|¡ W d   ƒ d S 1 s#w   Y  d S )Nc                   S   rž  rŸ  r'   r'   r'   r'   r(   rC  ¼  r—   z[TestCacheLocatorEnvironmentIntegration.test_locators_env_override_single.<locals>.mock_funcr   r
   )r   r   r
   r  Ú_implr  ©rE   rC  r0   ÚexpectedLocatorr'   r'   r(   Ú!test_locators_env_override_single»  s   þ
ÿ"úzHTestCacheLocatorEnvironmentIntegration.test_locators_env_override_singlec                 C   sZ   dd„ }t dt› dƒ t|ƒ}t}|  |jj|¡ W d   ƒ d S 1 s&w   Y  d S )Nc                   S   rž  rŸ  r'   r'   r'   r'   r(   rC  É  r—   z[TestCacheLocatorEnvironmentIntegration.test_locators_env_override_custom.<locals>.mock_funcr   z.TestLocator)r   rU   r   rœ  r  r¤  r  r¥  r'   r'   r(   Ú!test_locators_env_override_customÈ  s   þ
ÿ"úzHTestCacheLocatorEnvironmentIntegration.test_locators_env_override_customc                 C   sX   dd„ }d}t }td|ƒ t|ƒ}|  |jj|¡ W d   ƒ d S 1 s%w   Y  d S )Nc                   S   rž  rŸ  r'   r'   r'   r'   r(   rC  Ö  r—   zYTestCacheLocatorEnvironmentIntegration.test_locators_env_override_list.<locals>.mock_funczXInTreeCacheLocatorFsAgnostic,InTreeCacheLocator,IPythonCacheLocator,UserWideCacheLocatorr   )r
   r   r   r  r¤  r  )rE   rC  ZlocatorClassesr¦  r0   r'   r'   r(   Útest_locators_env_override_listÕ  s   þ"ûzFTestCacheLocatorEnvironmentIntegration.test_locators_env_override_listc                 C   s(   dd„ }t |ƒ}t}|  |jj|¡ d S )Nc                   S   rž  rŸ  r'   r'   r'   r'   r(   rC  å  r—   zOTestCacheLocatorEnvironmentIntegration.test_default_locators.<locals>.mock_func)r   r	   r  r¤  r  r¥  r'   r'   r(   Útest_default_locatorsä  s   z<TestCacheLocatorEnvironmentIntegration.test_default_locatorsN)	rU   re   rf   rG  r£  r§  r¨  r©  rª  r'   r'   r'   r(   r  ­  s    r  c                   @   rA  )Ú TestInTreeCacheLocatorFsAgnosticz7Test _InTreeCacheLocatorFsAgnostic class functionality.c                 C   s¶   ddl m} t |¡}t ||¡}t ||¡}|  |¡ |  |¡ | ¡ }| ¡ }|  	t
|ƒd¡ |  	t
|ƒd¡ |  	|d |d ¡ |  	|d t|d ƒ¡ |  |d |d ¡ dS )z9Test that FsAgnostic locator floors timestamp to seconds.r4   rö   r5   r   N)rú   r÷   rý   rþ   r	   rÿ   r
   rD  Úget_source_stamprD   r’   r   ZassertLessEqual)rE   r÷   r  Zregular_locatorÚfs_agnostic_locatorZregular_stampZfs_agnostic_stampr'   r'   r(   Útest_source_stamp_precisionð  s   
ÿ

z<TestInTreeCacheLocatorFsAgnostic.test_source_stamp_precisionc                 C   sh   ddl m} t |¡}t ||¡}t |¡}|j}| 	¡ }t
|ƒ}|  |d |¡ |  |d |j¡ dS )z:Test FsAgnostic timestamp handling using filesystem mtime.r4   rö   r   N)rú   r÷   rý   rþ   r
   rÿ   r   rt   Úst_mtimer¬  r   rD   Úst_size)rE   r÷   r  r­  Ústat_resultZoriginal_mtimeZstampZexpected_timestampr'   r'   r(   Útest_timestamp_precision_on_fs  s   
ÿ
z?TestInTreeCacheLocatorFsAgnostic.test_timestamp_precision_on_fsN)rU   re   rf   rG  r®  r²  r'   r'   r'   r(   r«  í  s    !r«  Ú__main__)Pr=  rý   rV   r   r"   rt   r¤   rn   ri   r.  rØ   r5  Úpathlibr   Zllvmlite.bindingZbindingrW  ÚnumpyrÉ   Úmathr   Znumbar   Z
numba.corer   Znumba.core.cachingr   r   r   r	   r
   Znumba.core.errorsr   Znumba.parforsr   Znumba.tests.supportr   r   r   r   r   r   r   r   r   r   Znumba.core.registryr   r+  ÚImportErrorr)   Z_access_preventableZ_access_msgr  r-  r-   r3   r8   r<   r?   rA   rB   rZ   rl   r˜   r»   r2  rB  rH  rL  rg  ro  r‡  r‘  r˜  rœ  r  r«  rU   Úmainr'   r'   r'   r(   Ú<module>   s~    0ÿ	:7)   !;\t49@
?ÿ