o
    ç0 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_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!   úd/home/app/PaddleOCR-VL-test/.venv_paddleocr/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Ú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   
r2   c                  C   s.   t } tdd| ƒ}| ƒ }|ƒ }||ksJ ‚d S r(   )r2   r   ©r+   r,   ÚexpÚgotr!   r!   r"   Úcheck_dict_cache_   s
   r6   c                  c   s    dD ]} | V  qd S )N)r.   r/   é   r!   )Úvr!   r!   r"   Úgenerator_cacheg   s   €ÿr9   c                  C   s6   t } tdd| ƒ}t| ƒ ƒ}t|ƒ ƒ}||ksJ ‚d S r(   )r9   r   Úlistr3   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)rB   r-   ©r?   r!   r!   r"   Útest_constant_unicode_cachez   ó   z'TestCaching.test_constant_unicode_cachec                 C   rC   rD   )rB   r6   rE   r!   r!   r"   Útest_dict_cache}   rG   zTestCaching.test_dict_cachec                 C   rC   rD   )rB   r;   rE   r!   r!   r"   Útest_generator_cache€   rG   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)rO   Ú
__module__Ú__qualname__rB   rF   rH   rI   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)   rM   c                 S   s   | S rD   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)rM   )r   r   Z_cache_hitsZ
signaturesZ_cache_missesÚ	tracebackÚ
format_excÚput)rZ   rX   Zsecond_callrb   r]   r\   r!   r!   r"   rT   ®   s0   ÿÿÿÿ"èrT   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ÚmodfilerX   r   ÚcopyÚusecases_filer   ÚstatÚS_IREADÚS_IWRITEZmaxDiffrE   r!   r!   r"   ÚsetUpÐ   s   

zBaseCacheTest.setUpc                 C   s"   t j | jd ¡ t j | j¡ d S rD   )rh   ÚmodulesÚpoprj   r   Úremover   rE   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)rh   rr   rs   rj   Ú
__cached__r   ÚunlinkÚFileNotFoundErrorr   r>   Ú__file__Úrstriprk   )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   ÚlistdirrX   ry   rE   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 rD   )r   r   Úgetmtimer   rX   r‚   rE   r!   r"   Ú	<genexpr>ö   s   € $ÿz1BaseCacheTest.get_cache_mtimes.<locals>.<genexpr>)ÚdictÚsortedr†   rE   r!   rE   r"   Úget_cache_mtimesõ   s   
ÿzBaseCacheTest.get_cache_mtimesc                 C   s   |   ¡ }|  t|ƒ||¡ d S rD   )r†   r>   Úlen)r?   Únr&   r!   r!   r"   Úcheck_pycacheù   s   zBaseCacheTest.check_pycachec                 C   ó   d S rD   r!   rE   r!   r!   r"   Ú
dummy_testý   ó   zBaseCacheTest.dummy_test)rO   r_   r`   rm   rj   rq   ru   r€   r†   r‹   rŽ   r   r!   r!   r!   r"   rf   Ê   s    	rf   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 )Nzœif 1:
            import sys

            sys.path.insert(0, %(tempdir)r)
            mod = __import__(%(modname)r)
            mod.self_test()
            ©r   rj   ú-c)ÚstdoutÚstderrÚenvr   zBprocess failed with code %s: 
stdout follows
%s
stderr follows
%s
)r‰   r   rj   r   Úenvironrl   ÚupdateÚ
subprocessÚPopenrh   Ú
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 rD   )Ústatsr>   ÚsumÚ
cache_hitsÚvaluesZcache_misses)r?   r@   ÚhitsÚmissesÚstr!   r!   r"   Ú
check_hits   s   ÿÿz&DispatcherCacheUsecasesTest.check_hitsrD   )rO   r_   r`   r   r   Údirnamerz   Úherer   rm   rj   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/   r7   é   ç      @ç      @é   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/   r7   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 )Nr7   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 )Nr7   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/   r7   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Ã   Ú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Õ   ÚformatrO   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Úerrorr7   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/   r7   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/   r7   r¶   r0   z
Z = 10
é   )r€   r»   r¼   r   rk   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/   r7   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ÚassertIsNonerh   Ú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/   r7   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   rn   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 )Nrg   r   )
r   r   r   r   r   rn   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£   rW   ©r  Úpr¨   r©   ©Zbase_cmdZinputfnr?   r!   r"   Úexecute_with_inputx  ó   ý

ý$õz2TestCache.test_ipython.<locals>.execute_with_inputúcache hits = 1)rh   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  )rh   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)rO   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)Úsuperrq   r   r   r   r   ÚzipfileÚZipFileÚwritestrrh   ri   rj   )r?   Zmod_contentZmod_filenameÚzip_filenameÚzip_pathÚzf©rN   r!   r"   rq   È  s   
ÿ
zTestCacheZip.setUpc                 C   s   t j d¡ t j dd ¡ d S )Nr   r)  )rh   r   rs   rr   rE   r!   r!   r"   ru   ß  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/   r7   r¹   r.   r)  )r)  Úaddr>   r²   rh   rr   Ú	importlibÚinvalidate_caches)r?   r)  Zresult1Zresult2r!   r!   r"   Útest_zip_cachingå  s   zTestCacheZip.test_zip_caching)rO   r_   r`   rq   ru   r5  Ú__classcell__r!   r!   r1  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   rD   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?   r8  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   rD   r!   r!   r!   r!   r"   r8    r‘   z@TestCacheZipLib.test_zip_locator_non_zip_path.<locals>.mock_funcz/path/to/module.py)r   r÷   rø   )r?   r8  Znon_zip_pathrû   r!   r!   r"   Útest_zip_locator_non_zip_path  s   z-TestCacheZipLib.test_zip_locator_non_zip_pathN)rO   r_   r`   Ú__doc__r9  r:  r!   r!   r!   r"   r7    s    r7  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<  rq   r   Úsequential_parfor_loweringrE   r1  r!   r"   rq     ó   
z TestSequentialParForsCache.setUpc                    r=  )NF)r*  r<  ru   r   r>  rE   r1  r!   r"   ru   #  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)rO   r_   r`   rq   ru   rÂ   r6  r!   r!   r1  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Úkr8   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ª   rG  rD  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€   rI  rŒ   r†   r‹   r   rP  ÚassertNotEqualrª   rG  rD  r»   rJ  rK  rL  r>   rM  rN  rO  )r?   r   rQ  rè   Zmy_cpu_featuresZsystem_featuresr*   rR  rS  rT  rU  rV  rW  r!   r!   r"   Útest_user_set_cpu_features\  s2   
ÿ

z2TestCacheWithCpuSetting.test_user_set_cpu_featuresN)rO   r_   r`   Ú_numba_parallel_test_rG  rX  rZ  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 )Nr7   Úspawnr.   r/   )r€   Zsimple_usecase_callerrP   rQ   ÚAttributeErrorZPoolr¬   ÚimapÚrangeÚcloser>   )r?   r   r    r   rY   ÚpoolrA   r!   r!   r"   Útest_multiprocessingŠ  s   ÿ
z*TestMultiprocessCache.test_multiprocessingN)rO   r_   r`   r[  r   r   r³   rz   r´   r   rm   rj   rc  r!   r!   r!   r"   r\  €  s    r\  c                   @   sx   e Zd ZdZej e¡Zej 	ed¡Z
dZdZdZdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Ze eedƒd¡dd„ ƒZdS )ÚTestCacheFileCollisionFr“   Zcaching_file_loc_fodderzD
from numba import njit
@njit(cache=True)
def bar():
    return 123
zD
from numba import njit
@njit(cache=True)
def bar():
    return 321
c                 C   sð   t dƒ| _tj d| j¡ d| _|  | jtj¡ | j| _d 	| jdg¡| _
tj 	| j| j¡}t |¡ ttj 	|d¡dƒ}t| j|d W d   ƒ n1 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   rh   r   ri   rj   ZassertNotInrr   Úmodname_bar1r   Úmodname_bar2r   r   r   ÚprintÚsource_text_1Úsource_text_2)r?   ZfoomodÚfoutr!   r!   r"   rq   ±  s   

ÿ"ÿzTestCacheFileCollision.setUpc                 C   s2   t j | jd ¡ t j | jd ¡ t j | j¡ d S rD   )rh   rr   rs   ri  rj  r   rt   r   rE   r!   r!   r"   ru   ¿  s   zTestCacheFileCollision.tearDownc                 C   ó   t | jƒjS rD   )r   ri  ÚbarrE   r!   r!   r"   Úimport_bar1Ä  ó   z"TestCacheFileCollision.import_bar1c                 C   ro  rD   )r   rj  rp  rE   r!   r!   r"   Úimport_bar2Ç  rr  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)rq  rs  rJ  rK  Z_index_namerY  Ú
assertTrueÚ
startswith)r?   Úbar1Úbar2Zidxname1Zidxname2r!   r!   r"   Útest_file_locationÊ  s   

z)TestCacheFileCollision.test_file_locationrQ   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 contextrJ   )rq  rs  r   Úgetvaluer>   ÚcountrY  rP   rQ   Ú
ValueErrorrk  rR   rS   Úcache_file_collision_testerr   ri  rj  rU   rV   r   )r?   rv  rw  ÚbufZres1ZcachelogZres2ÚmpÚqr[   Zlog1Zgot1Úlog2Zgot2r!   r!   r"   Útest_no_collisionÔ  s^   ÿÿÿþÿz(TestCacheFileCollision.test_no_collisionN)rO   r_   r`   r[  r   r   r³   rz   r´   r   rm   rj   rl  rm  rq   ru   rq  rs  rx  r%  Ú
skipUnlessÚhasattrrP   r  r!   r!   r!   r"   rd    s     
ÿrd  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   )rh   r   ri   r   rp  r   re   ry  )	r  r   ri  rj  rv  rw  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 )Nre  zfile1.pyrÙ   rg  zfile2.py)r   r   r   r   r   Úfile1r   rk  Úsource_text_file1Úfile2Úsource_text_file2)r?   rn  r!   r!   r"   rq   4  s   
ÿ"ÿz)TestCacheMultipleFilesWithSignature.setUpc                 C   s   t  | j¡ d S rD   )r   r   r   rE   r!   r!   r"   ru   ?  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   rA  )rž   rŸ   rh   r    r‡  r¡   r¢   r¥   r>   r£   r‰  )r?   r§   r¨   r©   rB  r!   r!   r"   Ú*test_caching_mutliple_files_with_signatureB  s   þþzNTestCacheMultipleFilesWithSignature.test_caching_mutliple_files_with_signatureN)	rO   r_   r`   r[  rˆ  rŠ  rq   ru   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   rj   rž   rŸ   rh   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 rD   )rI  ©r?   r   r!   r!   r"   Úcheck_moduler  rr  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)rO   r_   r`   r   r   r³   rz   r´   r   rm   rj   rª   r  rÂ   r!   r!   r!   r"   r  T  s    r  Ú__main__)Er3  rõ   rP   r   r   rn   rž   rh   rc   r%  rÒ   r+  Úpathlibr   Zllvmlite.bindingZbindingrN  Ú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-   r2   r6   r9   r;   r<   rT   rf   r’   rµ   r(  r7  r<  r@  r\  rd  r|  r†  r  rO   Úmainr!   r!   r!   r"   Ú<module>   st    ,ÿ	:7'   !;Lt4
9ÿ