
    ܙdÒ                        d dl Z d dlmZ d dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZmZmZmZmZmZm Z m!Z!m"Z"m#Z# 	 d dl$Z$n# e%$ r dZ$Y nw xY wd Z& e&            Z'd	Z( ej)        e'e(          Z*d
 Z+d Z,d Z-d Z.d Z/d Z0 G d dee          Z1d Z2 G d de          Z3 G d de3          Z4 G d de4          Z5e" G d de4                      Z6 G d de4          Z7 G d de3          Z8e! G d d ej                              Z9d! Z: G d" d#ej                  Z; G d$ d%e3          Z<e=d&k    r ej>                     dS dS )'    N)njit)codegen)_UserWideCacheLocator)NumbaWarning)parfor)
TestCaseSerialMixincapture_cache_logimport_dynamicoverride_configoverride_env_configrun_in_new_process_cachingskip_if_typeguardskip_parfors_unsupportedtemp_directoryc                  H   t          d          } t          j                            | d          }t          j        |           t          t          j                            |d          d          5 }|                    d           d d d            n# 1 swxY w Y   t          j        |d           	 t          t          j                            |d          d          5 }|                    d           d d d            n# 1 swxY w Y   	 t          j        |d	           t          j	        |           d
S # t          $ r- Y t          j        |d	           t          j	        |           dS w xY w# t          j        |d	           t          j	        |           w xY w)N
test_cachewritable_testwrite_okwtcheck1@  write_forbiddencheck2i  FT)r   ospathjoinmkdiropenwritechmodshutilrmtreePermissionError)tempdirtest_dirfs      8lib/python3.11/site-packages/numba/tests/test_caching.pycheck_access_is_preventabler)   %   s    \**GWo66HHX	bgll8Z00$	7	7 1	               HXu "',,x):;;TBB 	aGGH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  	5!!!h     
5!!!h 	5!!!hsZ   2BBB5.D< #D9D< D		D< D	D< <
E3E6 2E33E6 6+F!z9Cannot create a directory to which writes are preventablec                  (    d} t          |           | fS )Nabcd)hash)cs    r(   constant_unicode_cacher.   I   s    A77A:    c                      t           }  t          d          |           } |             \  }} |            \  }}||k    sJ ||k    sJ d S NTcache)r.   r   )pyfunccfuncexp_hvexp_strgot_hvgot_strs         r(   check_constant_unicode_cacher:   N   sg    #FDtV$$EfhhOFGeggOFGVgr/   c                      dddS )N      )ab r@   r/   r(   
dict_cacherA   W   s    r/   c                  ~    t           }  t          d          |           } |             } |            }||k    sJ d S r1   )rA   r   r4   r5   expgots       r(   check_dict_cacherF   [   sH    FDtV$$E
&((C
%''C#::::::r/   c               #      K   dD ]} | V  d S )N)r<   r=      r@   )vs    r(   generator_cacherJ   c   s*         r/   c                      t           }  t          d          |           }t           |                       }t           |                      }||k    sJ d S r1   )rJ   r   listrC   s       r(   check_generator_cacherM   h   sT    FDtV$$E
vvxx..C
uuww--C#::::::r/   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )TestCachingc                 p     |             t          |          }|                     |d         d           d S )Nexitcoder   )r   assertEqual)selffuncress      r(   run_testzTestCaching.run_testq   s:    (..Z!,,,,,r/   c                 :    |                      t                     d S N)rV   r:   rS   s    r(   test_constant_unicode_cachez'TestCaching.test_constant_unicode_cachev   s    233333r/   c                 :    |                      t                     d S rX   )rV   rF   rY   s    r(   test_dict_cachezTestCaching.test_dict_cachey   s    &'''''r/   c                 :    |                      t                     d S rX   )rV   rM   rY   s    r(   test_generator_cachez TestCaching.test_generator_cache|   s    +,,,,,r/   c                    t          | j        j                  }t          j                    }|                                }|                    t          ||df          }|                                 |	                                 |
                                \  }}|s|                     |           |                     |dd           |                    t          ||df          }|                                 |	                                 |
                                \  }}|s|                     |           |                     |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failrR   )rS   	cache_dirctxresult_queueprocsuccessoutputs          r(   test_omittedzTestCaching.test_omitted   sc    #4>#:;;	)++yy{{{{-	51  
 
 	

		&**,,  	IIf;	
 	
 	
 {{-	40  
 
 	

		&**,,  	IIf;	
 	
 	
 	
 	
r/   N)re   
__module____qualname__rV   rZ   r\   r^   rt   r@   r/   r(   rO   rO   p   s_        - - -
4 4 4( ( (- - -(
 (
 (
 (
 (
r/   rO   c                 D   t          d|          5  t          d          dd            }	  |            }|rM|j        |j        d                  dk    s
J d            |j        |j        d                  dk    s
J d	            nL|j        |j        d                  dk    s
J d
            |j        |j        d                  dk    s
J d            d}n#  t          j                    }d}Y nxY w|                     ||f           d d d            d S # 1 swxY w Y   d S )N	CACHE_DIRTr2   rc   c                     | S rX   r@   )nums    r(   testz(omitted_child_test_wrapper.<locals>.test   s    Jr/   r   r<   zCache did not hit as expectedzCache has an unexpected misszCache did not miss as expectedzCache has an unexpected hitF)rc   )r   r   _cache_hits
signatures_cache_misses	traceback
format_excput)rp   rn   second_callr{   rs   rr   s         r(   rj   rj      s   	i	0	0 , ,	D				 	 	 
			TVVF  	2'(:;q@@@3 A@@)$/!*<=BBB2 CBBB )$/!*<=BBB4 CBB'(:;q@@@1 A@@GG	)++FGGG'6*+++1, , , , , , , , , , , , , , , , , ,s)   DB'CDC.,DDDc                   @    e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d ZdS )	BaseCacheTestNc                    t          d          | _        t          j                            d| j                   t
          j                            | j        | j        dz             | _        t
          j                            | j        d          | _	        t          j        | j        | j                   t          j        | j        t          j        t          j        z             d | _        d S )Nr   r   z.py__pycache__)r   r%   sysr   insertr   r   modnamemodfilern   r"   copyusecases_filer!   statS_IREADS_IWRITEmaxDiffrY   s    r(   setUpzBaseCacheTest.setUp   s    %l334<(((w||DL$,2FGGdlMBBD&555
t|dm;<<<r/   c                     t           j                            | j        d            t           j                            | j                   d S rX   )r   modulespopr   r   remover%   rY   s    r(   tearDownzBaseCacheTest.tearDown   s6    d+++%%%%%r/   c                 F   t           j                            | j        d           }|2|j        g}|D ]'}	 t          j        |           # t          $ r Y $w xY wt          | j                  }| 	                    |j
                            d          | j                   |S )Nco)r   r   r   r   
__cached__r   unlinkFileNotFoundErrorr   rR   __file__rstripr   )rS   oldcachedfnmods        r(   import_modulezBaseCacheTest.import_module   s     koodlD11?n%F  IbMMMM(   DT\**,,T22DLAAA
s   A


AAc                 n    	 d t          j        | j                  D             S # t          $ r g cY S w xY w)Nc                 <    g | ]}|                     d           |S ))z.pycz.pyo)endswith).0r   s     r(   
<listcomp>z0BaseCacheTest.cache_contents.<locals>.<listcomp>   s9     : : :2;;'788:B : : :r/   )r   listdirrn   r   rY   s    r(   cache_contentszBaseCacheTest.cache_contents   sW    	: :DN!;!; : : : :  	 	 	III	s   "% 44c                 x     t           fdt                                                     D                       S )Nc              3      K   | ]H}|t           j                            t           j                            j        |                    fV  Id S rX   )r   r   getmtimer   rn   )r   r   rS   s     r(   	<genexpr>z1BaseCacheTest.get_cache_mtimes.<locals>.<genexpr>   s^       = = ))"',,t~r*J*JKKL = = = = = =r/   )dictsortedr   rY   s   `r(   get_cache_mtimeszBaseCacheTest.get_cache_mtimes   sO     = = = =$T%8%8%:%:;;= = = = = 	=r/   c                 v    |                                  }|                     t          |          ||           d S rX   )r   rR   len)rS   nr-   s      r(   check_pycachezBaseCacheTest.check_pycache   s7    !!QA&&&&&r/   c                     d S rX   r@   rY   s    r(   
dummy_testzBaseCacheTest.dummy_test   s    r/   )re   ru   rv   r   r   r   r   r   r   r   r   r   r@   r/   r(   r   r      s        MG  & & &  "  = = =' ' '    r/   r   c                       e Zd Zej                            e          Zej                            ed          Z	dZ
d ZddZdS )DispatcherCacheUsecasesTestcache_usecases.pydispatcher_caching_test_fodderc           	      z   dt          | j        | j                  z  }t          j        t
          j        d|gt          j        t          j                  }|                                \  }}|j	        dk    rBt          d|j	        d|                                d|                                d	          d S )
Nzif 1:
            import sys

            sys.path.insert(0, %(tempdir)r)
            mod = __import__(%(modname)r)
            mod.self_test()
            r%   r   -cstdoutstderrr   process failed with code z: 
stdout follows
z
stderr follows

r   r%   r   
subprocessPopenr   
executablePIPEcommunicate
returncodeAssertionErrordecoderS   codepopenouterrs        r(   run_in_separate_processz3DispatcherCacheUsecasesTest.run_in_separate_process  s     t|T\BBBC  #.$!=(2
Q Q Q$$&&Sq   . ###SZZ\\\\3::<<<<A   ! r/   Nc                    |j         }|                     t          |j                                                  ||j                   |B|                     t          |j                                                  ||j                   d S d S rX   )statsrR   sum
cache_hitsvaluescache_misses)rS   rT   hitsmissessts        r(   
check_hitsz&DispatcherCacheUsecasesTest.check_hits  s    ZR]113344dBMJJJS!7!7!9!9::F_. . . . . r/   rX   )re   ru   rv   r   r   dirnamer   herer   r   r   r   r   r@   r/   r(   r   r      se        7??8$$DGLL':;;M.G  .. . . . . .r/   r   c                   z   e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Ze ej        ej        dk    d          d                         Ze ej        ej        dk    d          d                         Zd Z ej        edu pej        d         dk     d          d             ZdS )	TestCachec                    |                      d           |                                 }|                      d           |j        }|                      |dd          d           |                      d           |                      |dd          d           |                      d           |                     |dd           |j        }|                      |dd          d           |                      d           |                      |dd          d           |                      d           |                     |dd           |j        } ||j        d          }|                     t          |          d	            ||j	        d          }|                     t          |          d	           |                      d
           |                     |dd           |j
        }|                      |dd          d           |                      |dd          d           |                                  d S )Nr   r=   rH            @      @   r<   )r=   g     E@	   y              @y       @      @)r   r   add_usecaseassertPreciseEqualr   add_objmode_usecaserecord_returnaligned_arrtuple
packed_arrgenerated_usecaser   )rS   r   r'   recs       r(   test_cachingzTestCache.test_caching#  s8   1  ""1O!Q+++1#q		3///11a   #!Q+++1#q		3///11a   a##c

I666a""c

I66611a   !!Q+++"a&111 	$$&&&&&r/   c                    |                      d           |                                 }|                      d           |j        }|                      |dd          d           |                      d           |                      |dt	          j        d                    dt	          j        d          z   dz              |                      d           |                     |dd           d S )Nr   r=   rH   r   r<   )r   r   r   r   nparanger   rS   r   r'   s      r(   test_caching_nrt_prunedz!TestCache.test_caching_nrt_prunedE  s    1  ""1O!Q+++1!RYq\\ 2 2A	!4Dq4HIII11a     r/   c                    |                                  }|                     |                    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 )NrH   r=   r            @r   )r   r   innerr   outer_uncachedouterr   s      r(   test_inner_then_outerzTestCache.test_inner_then_outerR  sG     ""		!Q3331!Q+++1  ""!Q+++1I!Q+++1#q		3///1r/   c                 4   |                                  }|                     |                    dd          d           |                     d           |                     |                    dd          d           |                     d           |                                  }|j        }|                      |dd          d           |                     d           |                      |dd          d           |                     d           d S )NrH   r=   r   r   r   r   r   )r   r   r   r   r   r   r   s      r(   test_outer_then_innerzTestCache.test_outer_then_innerf  s	     ""		!Q3331 2 21a 8 8!<<<1  ""I!Q+++1#q		3///1r/   c                     |                                  }|j        }|                      |dd          d           |                     d           d S )Nr=   rH   r   r   )r   add_nocache_usecaser   r   r   s      r(   test_no_cachingzTestCache.test_no_cachingt  sU      ""#!Q+++1r/   c                    |                                  }t          j        d          5 }t          j        dt                     |j        }|                      |d          d           |                     d           d d d            n# 1 swxY 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   
loopliftedr   r   rR   r   assertInstrmessagerS   r   wr'   s       r(   test_loopliftedzTestCache.test_looplifted{  s     ""$D111 	"Q!(L999A##AAaDD!,,,q!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	Q### /03AaDL0A0A	C 	C 	C 	C 	Cs   ABBBc                    |                                  }t          j        d          5 }t          j        dt                     |j        }t          j                             |            |j	                   | 
                    d           d d d            n# 1 swxY 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   use_big_arrayr   testingassert_equalbiggier   rR   r   r	  r
  r  r  s       r(   test_big_arrayzTestCache.test_big_array  s!     ""$D111 	"Q!(L999!AJ##AACC444q!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	Q### 347!4E4E	G 	G 	G 	G 	Gs   A$BB!Bc                 $   |                                  }|j        |j        |j        fD ]}t	          j        d          5 }t	          j        dt                     |                      |d          d           | 	                    d           d d d            n# 1 swxY w Y   | 
                    t          |          d           |                     d                    |j                  t          |d         j                             d S )NTr  r  g        r   r<   z#Cannot cache compiled function "{}")r   	use_c_sinuse_c_sin_nest1use_c_sin_nest2r  r  r  r   r   r   rR   r   r	  formatre   r
  r  )rS   r   r'   r  s       r(   test_ctypeszTestCache.test_ctypes  sE      ""-!4c6IJ 	 	A(555 &%h===''#444""1%%%	& & & & & & & & & & & & & & & SVVQ'''MM5<<QZHHAaDL!!   	 	s    ABB	"B	c                    |                                  }t          j                    5  t          j        dt                     |j        }|                      |d          d           |j        }|                      |d          d           |j        }|                      |d          d           |j	        }|                      |d          d           | 
                    d           d d d            d S # 1 swxY w Y   d S )NerrorrH   r      
      r   )r   r  r  r  r   closure1r   closure2closure3closure4r   r   s      r(   test_closurezTestCache.test_closure  sA     ""$&& 	" 	"!'<888A##AAaDD!,,,A##AAaDD!,,,A##AAaDD"---A##AAaDD"---q!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s   CC==DDc                    |                                  }|j        }|                      ||j        d          d           |                      ||j        d          d           |                      ||j        d          d           |                      ||j        d          d           |                     d           d S )Nr<   r  d   r=         )r   first_class_function_usecaserR   first_class_function_mulfirst_class_function_addr   r   s      r(   test_first_class_functionz#TestCache.test_first_class_function  s      "",37;;Q???37<<cBBB37;;Q???37<<bAAA 	1r/   c                    |                                  }|                    dd           |                    dd           |                    dd           |                    dd           |                    dd           |                    |j        d           |                    |j        d           |                    dd           | 	                                }| 
                    |j        dd           |                                  }|                     ||           |j        } |dd           | 
                    |dd            |dd           | 
                    |dd           |j        } |dd           | 
                    |dd           |                     | 	                                |           |                                  |                     | 	                                |           d S )Nr=   rH   r   r   r   r<   )r   r   r   r   r   r   r   r   r   r   r   assertIsNotrR   r   )rS   r   mtimesmod2r'   s        r(   test_cache_reusezTestCache.test_cache_reuse  s     ""1S!!!1%%%1a   		!Q#.!,,,#/1---a###&&((A...!!##d###	!Q1a   	#s1a   $	!Q1a    	..00&999$$&&&..00&99999r/   c                    |                                  }|j        }|                      |dd          d           t          | j        d          5 }|                    d           d d d            n# 1 swxY w Y   |                                  }|j        }|                      |dd          d           |j        }|                      |dd          d           d S )Nr=   rH   r   r>   z
Z = 10
   )r   r   r   r   r   r    r   r   s      r(   test_cache_invalidatezTestCache.test_cache_invalidate  s     ""O!Q+++ $,$$ 	"GGL!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"   ""O!Q,,,#!Q,,,,,s   A33A7:A7c                    |                                  }|j        }|                      |dd          d           |                                  }|j        }d|_        |                      |dd          d           |                                 |                      |dd          d           |                                  }|j        }|                      |dd          d           d S )Nr=   rH   r   r  r3  )r   r   r   Z	recompiler   s      r(   test_recompilezTestCache.test_recompile  s      ""O!Q+++  ""O!Q+++	!Q,,,   ""O!Q,,,,,r/   c                     |                                  }|j        }|                      |d          d           |j        }|                      |d          d           d S )Nr=   r   r  )r   renamed_function1r   renamed_function2r   s      r(   test_same_nameszTestCache.test_same_names  se      ""!!a(((!!a(((((r/   c                    ddl m} |j        }t          ddd          }	 ||_        t	          j        |          }t          j        ||          }|                     |           dt          _
        t          j        ||          }|                     |t                     ||_        t          `
d S # ||_        t          `
w xY w)Nr<   )functionpassztests/dummy_module.pyexecT)dummy_moduler>  __code__compileinspectgetfiler   from_functionassertIsNoner   frozenassertIsInstance)rS   r>  old_codecode_objsourcelocators         r(   test_frozenzTestCache.test_frozen  s    ******$6#:FCC	 (H_X..F ,9(FKKGg&&&CJ ,9(FKKG!!'+@AAA !)H


 !)H
NNNNs   BB1 1Cc                    |                                  }|j        }|                     t          j        |j        j        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=   rH   r   r   r<   N)
r   r   
addCleanupr"   r#   r   
cache_pathr   r   r   )rS   r   r'   r0  s       r(   _test_pycache_fallbackz TestCache._test_pycache_fallback$  s    
   ""O 	qw'9NNN!Q+++1a    !!##!Q+++1a    	1r/   ntz3cannot easily make a directory read-only on Windowsc                     t          j        | j                  j        }t          j        | j        d           |                     t           j        | j        |           |                                  d S )Nr   )r   r   r%   st_moder!   rQ  rS  )rS   	old_permss     r(   test_non_creatable_pycachez$TestCache.test_non_creatable_pycache=  s^    
 GDL))1	
u%%%$,	:::##%%%%%r/   c                 >   t           j                            | j        d          }t          j        |           t          j        |          j        }t          j        |d           |                     t           j        ||           | 	                                 d S )Nr   r   )
r   r   r   r%   r   r   rV  r!   rQ  rS  )rS   pycacherW  s      r(   test_non_writable_pycachez#TestCache.test_non_writable_pycacheH  s    
 ',,t|];;
GG$$,	
%   '9555##%%%%%r/   c                 t    t           j        ddgg dz  	 t          j        dgz             }n9# t          j        $ r'}                     d|j        z             Y d }~nd }~ww xY w|                                                                }t          j
                             j        d          t          d          5 }|                    d           d d d            n# 1 swxY w Y    f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                  :   t          d          5 } t          j        | t          j        t          j        d          }|                                \  }}|j        dk    r                     d|j        ||fz             |cd d d            S # 1 swxY w Y   d S NrbT)stdinr   r   universal_newlines*   z6unexpected return code %d
-- stdout:
%s
-- stderr:
%s
r   r   r   r   r   r   rm   rf  pr   r   base_cmdinputfnrS   s       r(   execute_with_inputz2TestCache.test_ipython.<locals>.execute_with_inputv      gt$$ $XU,6O,6O8<> > > ==??S<2%%II 1 "#sC 89 : : :                     A1BBBcache hits = 1)r   r   r   check_outputCalledProcessErrorskipTestr   stripr   r   r   r   r%   r   r    r	  )rS   verer'   rn  r   rl  rm  s   `     @@r(   test_ipythonzTestCache.test_ipythonU  s   ND)4MMMM	*)(k]*BCCCC, 	* 	* 	*MMAL) * * * * * * * *	* iikk  ""',,t|-HII'3 	1GG    	 	 	 	 	 	 	 	 	 	 	 	 	 	 	*	 	 	 	 	 	 	 	  ""&		44444s'   4 A*A%%A*C**C.1C.Nr   r   zrequires ipykernel >= 6c                 ^    t           j        ddgg dz  	 t          j        dgz             }n9# t          j        $ r'}                     d|j        z             Y d }~nd }~ww xY w|                                                                }ddl	m
} |                                t          d          5 }|                    d	           d d d            n# 1 swxY w Y    fd
} |              |            }                     d|                                           d S )Nr]  r^  r_  r`  ra  r   )compilerr  rb  c                  :   t          d          5 } t          j        | t          j        t          j        d          }|                                \  }}|j        dk    r                     d|j        ||fz             |cd d d            S # 1 swxY w Y   d S rd  ri  rj  s       r(   rn  z4TestCache.test_ipykernel.<locals>.execute_with_input  ro  rp  rq  )r   r   r   rr  rs  rt  r   ru  r   	ipykernelrz  get_tmp_directoryr   r    r	  )	rS   rv  rw  rz  r'   rn  r   rl  rm  s	   `      @@r(   test_ipykernelzTestCache.test_ipykernel  s   
 ND)4MMMM	*)(k]*BCCCC, 	* 	* 	*MMAL) * * * * * * * *	* iikk  ""&&&&&&,,..'3 	1GG    	 	 	 	 	 	 	 	 	 	 	 	 	 	 	*	 	 	 	 	 	 	 	  ""&		44444s'   4 A*A%%A*=CC#&C#)re   ru   rv   r   r   r   r   r   r  r  r  r$  r,  r1  r4  r8  r<  rN  rS  skip_bad_accessunittestskipIfr   namerX  r[  rx  r|  version_infor~  r@   r/   r(   r   r   !  s        '  '  'D! ! !  (    C C CG G G  $" " " 	 	 	: : :>- - -- - -$) ) )  .  2 X_RW_JL L& &L L _& X_RW_JL L& &L L _&35 35 35j X_i4'KY-CA-F-J.0 055 550 055 55 55r/   r   c                   .     e Zd Z fdZ fdZd Z xZS )TestSequentialParForsCachec                 n    t          t          |                                            dt          _        d S )NT)superr  r   r   sequential_parfor_loweringrS   rd   s    r(   r   z TestSequentialParForsCache.setUp  s.    ($//55777,0)))r/   c                 n    t          t          |                                            dt          _        d S )NF)r  r  r   r   r  r  s    r(   r   z#TestSequentialParForsCache.tearDown  s.    ($//88:::,1)))r/   c                 v   |                                  }|                     d           |j        }t          j        d          }|                      ||          ||z  |z              d |j                                        D             }|                     |dg           |                     d           d S )Nr   r  c                 &    g | ]}|j         j        S r@   )libraryhas_dynamic_globals)r   cress     r(   r   z;TestSequentialParForsCache.test_caching.<locals>.<listcomp>  s-     = = =#  <; = = =r/   Fr=   )	r   r   parfor_usecaser   onesr   	overloadsr   rR   )rS   r   r'   arydynamic_globalss        r(   r   z'TestSequentialParForsCache.test_caching  s      ""1gbkk#c	C888= ='({'9'9';';= = =5'2221r/   )re   ru   rv   r   r   r   __classcell__)rd   s   @r(   r  r    s`        1 1 1 1 1
2 2 2 2 2
	 	 	 	 	 	 	r/   r  c                   $    e Zd ZdZd Zd Zd ZdS )TestCacheWithCpuSettingFc                     d}|                                                                  D ]*\  }}||v r!|                     |||                    |dz  }+|                     |dd           d S )Nr   r<   znothing to comparemsg)r   itemsassertGreaterEqualassertGreater)rS   
mtimes_oldmatch_countkrI   s        r(   check_later_mtimesz*TestCacheWithCpuSetting.check_later_mtimes  s    ))++1133 	! 	!DAqJ'':a=999q ;3 	 	5 	5 	5 	5 	5r/   c                 d   |                      d           |                                 }|                                 t          |                                           }|                                 }t          dd          5  |                                  d d d            n# 1 swxY w Y   |                     |           | 	                    t          |                                           |           |j
        j        }|j        }|                                }|                     t          |          d           |                                \  }}|d         d         t!          j                    k    r||}
}	n||}
}	|                     |	d         d         t!          j                               |                     |	d         d         t%          j                               |                     |
d         d         d           |                     |
d         d         d           d S )Nr   NUMBA_CPU_NAMEgenericr=   r<    )r   r   	self_testr   r   r   r   r   r  r  r   _cache_cache_file_load_indexrR   keysllget_host_cpu_namer   get_host_cpu_features)rS   r   
cache_sizer/  r3   
cache_filecache_indexkey_akey_bkey_hostkey_generics              r(   test_user_set_cpu_namez.TestCacheWithCpuSetting.test_user_set_cpu_name  s.   1  "",,..//
&&(( !19== 	+ 	+((***	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	'''3t224455zBBB&&
 ,,..[))1---$))++8A;".0000$)5kHH$)5kH!Q)=)?)?@@@!Q)F)H)HIIIQ*I666Q*B/////s   B$$B(+B(c                    |                      d           |                                 }|                                 t          |                                           }|                                 }d}t          j                    }|                     ||           t          d|          5  | 
                                 d d d            n# 1 swxY w Y   |                     |           |                     t          |                                           |           |j        j        }|j        }|                                }|                     t          |          d           |                                \  }	}
|	d         d         |k    r|	|
}}n|
|	}}|                     |d         d         t'          j                               |                     |d         d         |           |                     |d         d         t'          j                               |                     |d         d         |           d S )Nr   z	-sse;-avxNUMBA_CPU_FEATURESr=   r<   )r   r   r  r   r   r   r   r  assertNotEqualr   r   r  r  r   r  r  r  rR   r  r  r  )rS   r   r  r/  my_cpu_featuressystem_featuresr3   r  r  r  r  r  r  s                r(   test_user_set_cpu_featuresz2TestCacheWithCpuSetting.test_user_set_cpu_features  sL   1  "",,..//
&&((%!799O_=== !5GG 	+ 	+((***	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+'''3t224455zBBB&&
 ,,..[))1---$))++8A;/))$)5kHH$)5kH!Q)=)?)?@@@!Q999Q*B,@,B,BCCCQ*O<<<<<s   .CCCN)re   ru   rv   _numba_parallel_test_r  r  r  r@   r/   r(   r  r    sG        !5 5 50 0 08 =  =  =  =  =r/   r  c                       e Zd ZdZej                            e          Zej        	                    ed          Z
dZd ZdS )TestMultiprocessCacheFr   r   c                    |                                  }|j        }d}	 t          j        d          }n# t          $ r
 t          }Y nw xY w|                    |          }	 t          |                    |t          |                              }|	                                 n# |	                                 w xY w| 
                    |||dz
  z  dz             d S )NrH   spawnr<   r=   )r   simple_usecase_callerrf   rg   AttributeErrorPoolr   imaprangecloserR   )rS   r   r'   r   ro   poolrU   s          r(   test_multiprocessingz*TestMultiprocessCache.test_multiprocessing2  s      "" %	"!-g66CC 	" 	" 	"!CCC	"xx{{	dii588,,--CJJLLLLDJJLLLLa1q5kQ./////s   4 AA!0B& &B<N)re   ru   rv   r  r   r   r   r   r   r   r   r   r  r@   r/   r(   r  r  (  sY         "7??8$$DGLL':;;M.G0 0 0 0 0r/   r  c                       e Zd ZdZej                            e          Zej        	                    ed          Z
dZdZdZd Zd Zd Zd	 Zd
 Z ej         eed          d          d             ZdS )TestCacheFileCollisionFr   caching_file_loc_fodderzD
from numba import njit
@njit(cache=True)
def bar():
    return 123
zD
from numba import njit
@njit(cache=True)
def bar():
    return 321
c                    t          d          | _        t          j                            d| j                   d| _        |                     | j        t          j                   | j        | _        d	                    | j        dg          | _
        t          j        	                    | j        | j                  }t          j        |           t          t          j        	                    |d          d          5 }t          | j        |           d d d            n# 1 swxY w Y   t          t          j        	                    |d	          d          5 }t          | j        |           d d d            d S # 1 swxY w Y   d S )
Ntest_cache_file_locr   module_name_that_is_unlikely.fooz__init__.pyr  filezfoo.py)r   r%   r   r   r   r   assertNotInr   modname_bar1r   modname_bar2r   r   r   printsource_text_1source_text_2)rS   foomodfouts      r(   r   zTestCacheFileCollision.setUpY  s   %&;<<4<(((5s{333 LHHdlE%:;;dlDL99
"',,v}55s;; 	1t$$40000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1"',,vx00#66 	1$$$40000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s$   ?D""D&)D&E??FFc                     t           j                            | j        d            t           j                            | j        d            t           j                            | j                   d S rX   )r   r   r   r  r  r   r   r%   rY   s    r(   r   zTestCacheFileCollision.tearDowng  sP    )4000)4000%%%%%r/   c                 4    t          | j                  j        S rX   )r   r  barrY   s    r(   import_bar1z"TestCacheFileCollision.import_bar1l      d/0044r/   c                 4    t          | j                  j        S rX   )r   r  r  rY   s    r(   import_bar2z"TestCacheFileCollision.import_bar2o  r  r/   c                 f   |                                  }|                                 }|j        j        j        }|j        j        j        }|                     ||           |                     |                    d                     |                     |                    d                     d S )Nz__init__.bar-3.pyzfoo.bar-3.py)r  r  r  r  _index_namer  
assertTrue
startswith)rS   bar1bar2idxname1idxname2s        r(   test_file_locationz)TestCacheFileCollision.test_file_locationr  s    !!!!;*6;*6Hh///++,?@@AAA++N;;<<<<<r/   rg   z)Test requires multiprocessing.get_contextc                    |                                  }|                                 }t                      5 } |            }d d d            n# 1 swxY w Y   |                                }|                     |                    d          d           |                     |                    d          d           |                     |                    d          d           |                     |                    d          d           t                      5 } |            }d d d            n# 1 swxY w Y   |                                }|                     |                    d          d           |                     |                    d          d           |                     |                    d          d           |                     |                    d          d           |                     ||           	 t          j        d          }n# t          $ r t          d           Y nw xY w|                                }|                    t          || j        | j        | j        f	          }	|	                                 |                                }
|                                }|                                }|                                }|	                                 |                     ||           |                     ||           |                     |
                    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 contextr`   )r  r  r
   getvaluerR   countr  rf   rg   
ValueErrorr  rh   ri   cache_file_collision_testerr%   r  r  rk   rl   r   )rS   r  r  bufres1cachelogres2mpqrq   log1got1log2got2s                 r(   test_no_collisionz(TestCacheFileCollision.test_no_collision|  s[    !!!!   	C466D	 	 	 	 	 	 	 	 	 	 	 	 	 	 	<<>>66:::55q99977;;;66:::   	C466D	 	 	 	 	 	 	 	 	 	 	 	 	 	 	<<>>66:::55q99977;;;66:::D$'''	+ !,W55BB 	+ 	+ 	+)*****	+ HHJJzz!< !4< $ 1 $ 1 3  4 4 	

uuwwuuwwuuwwuuww		 	t$$$t$$$ 	M22A666L111555N33Q777M22A666 	M22A666L111555N33Q777M22A66666s5   AAAD66D:=D:H% %I IN)re   ru   rv   r  r   r   r   r   r   r   r   r   r  r  r   r   r  r  r  r  
skipUnlesshasattrrf   r  r@   r/   r(   r  r  E  s        !7??8$$DGLL':;;M'GMM1 1 1& & &
5 5 55 5 5= = = X-@@DF F97 97F F97 97 97r/   r  c                 F   t           j                            d|           t          |          j        }t          |          j        }t                      5 } |            }d d d            n# 1 swxY w Y   |                     |                                           |                     |           t                      5 } |            }d d d            n# 1 swxY w Y   |                     |                                           |                     |           d S )Nr   )r   r   r   r   r  r
   r   r  )	r  r%   r  r  r  r  r  r1r2s	            r(   r  r    s`   HOOAw,''+D,''+D			 TVV              EE#,,..EE"III			 TVV              EE#,,..EE"IIIIIs$   A..A25A2CC!Cc                   ,    e Zd ZdZdZdZd Zd Z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                    t          d          | _        t          j                            | j        d          | _        t          | j        d          5 }t          | j        |           d d d            n# 1 swxY w Y   t          j                            | j        d          | _	        t          | j	        d          5 }t          | j
        |           d d d            d S # 1 swxY w Y   d S )Nr  zfile1.pyr  r  zfile2.py)r   r%   r   r   r   file1r   r  source_text_file1file2source_text_file2)rS   r  s     r(   r   z)TestCacheMultipleFilesWithSignature.setUp  sI   %&;<<W\\$,
;;
$*c"" 	5d$(t4444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 W\\$,
;;
$*c"" 	5d$(t4444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5s$   A77A;>A;C%%C),C)c                 8    t          j        | j                   d S rX   )r"   r#   r%   rY   s    r(   r   z,TestCacheMultipleFilesWithSignature.tearDown  s    dl#####r/   c                 t   t          j        t          j        | j        gt           j        t           j                  }|                                \  }}d|                                 d|                                 }|                     |j	        d|           t          j        t          j        | j
        gt           j        t           j                  }|                                \  }}d|                                 d|                                 }|                     |j	        d|           d S )Nr   zstdout:
z


stderr:
r   r  )r   r   r   r   r  r   r   r   rR   r   r  )rS   r   r   r   r  s        r(   *test_caching_mutliple_files_with_signaturezNTestCacheMultipleFilesWithSignature.test_caching_mutliple_files_with_signature  s    #.$*!=(2(29 9 9 $$&&SC#**,,CCSZZ\\CC)1#666  #.$*!=(2(29 9 9 $$&&SC#**,,CCSZZ\\CC)1c22222r/   N)	re   ru   rv   r  r  r  r   r   r	  r@   r/   r(   r  r    sY         "
	5 	5 	5$ $ $3 3 3 3 3r/   r  c                       e Zd Zej                            e          Zej                            ed          Z	dZ
d Zd Zd ZdS )TestCFuncCachezcfunc_cache_usecases.pycfunc_caching_test_fodderc                 P   dt          | j        | j                  z  }t          j        t
          j        d|gt          j        t          j                  }|                                \  }}|j	        dk    r-t          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   r   z:stderr follows
r   r   r   s        r(   r   z&TestCFuncCache.run_in_separate_process  s     t|T\BBBC  #.$!=(2
Q Q Q$$&&Sq    "EU=M "E "E47JJLL"E "E "E F F F ! r/   c                 .    |                                  d S rX   )r  rS   r   s     r(   check_modulezTestCFuncCache.check_module  s    r/   c                 P   |                      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   rR   r   r   r   r   div_usecaser  r   r  s     r(   r   zTestCFuncCache.test_caching  s|   1  ""13Q777-q1110;Q???3Q777#   ""13Q777-q1110;Q???3Q777#$$&&&&&r/   N)re   ru   rv   r   r   r   r   r   r   r   r   r   r  r   r@   r/   r(   r  r    sr        7??8$$DGLL'@AAM)GF F F0  ' ' ' ' 'r/   r  __main__)?rD  llvmlite.bindingbindingr  rf   numpyr   r   r   r"   r   r   r   r  r  numbar   
numba.corer   numba.core.cachingr   numba.core.errorsr   numba.parforsr   numba.tests.supportr   r	   r
   r   r   r   r   r   r   r   r|  ImportErrorr)   _access_preventable_access_msgr  r  r.   r:   rA   rF   rJ   rM   rO   rj   r   r   r   r  r  r  r  r  r  r  re   mainr@   r/   r(   <module>r!     sQ                  				       



                   4 4 4 4 4 4 * * * * * *                                      III     > 2133 I%(%&9;GG  
        
  7
 7
 7
 7
 7
+x 7
 7
 7
t, , ,84 4 4 4 4H 4 4 4n!. !. !. !. !.- !. !. !.H`5 `5 `5 `5 `5+ `5 `5 `5F     !<   .I= I= I= I= I=9 I= I= I=X0 0 0 0 0M 0 0 0: q7 q7 q7 q7 q7X. q7 q7 q7h  13 13 13 13 13(*; 13 13 13h6' 6' 6' 6' 6'] 6' 6' 6'r zHMOOOOO s   *A/ /A98A9