
    
fd                        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" 	 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 Z/ G d dee          Z0d Z1 G d de          Z2 G d de2          Z3 G d de3          Z4e! G d de3                      Z5 G d de3          Z6 G d de2          Z7e  G d d ej                              Z8d! Z9 G d" d#ej                  Z: G d$ d%e2          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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-   H   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_cacher9   M   sg    #FDtV$$EfhhOFGeggOFGVgr.   c                      dddS )N      )ab r?   r.   r'   
dict_cacher@   V   s    r.   c                  ~    t           }  t          d          |           } |             } |            }||k    sJ d S r0   )r@   r   r3   r4   expgots       r'   check_dict_cacherE   Z   sH    FDtV$$E
&((C
%''C#::::::r.   c               #      K   dD ]} | V  d S )N)r;   r<      r?   )vs    r'   generator_cacherI   b   s*         r.   c                      t           }  t          d          |           }t           |                       }t           |                      }||k    sJ d S r0   )rI   r   listrB   s       r'   check_generator_cacherL   g   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_testp   s:    (..Z!,,,,,r.   c                 :    |                      t                     d S N)rU   r9   rR   s    r'   test_constant_unicode_cachez'TestCaching.test_constant_unicode_cacheu   s    233333r.   c                 :    |                      t                     d S rW   )rU   rE   rX   s    r'   test_dict_cachezTestCaching.test_dict_cachex   s    &'''''r.   c                 :    |                      t                     d S rW   )rU   rL   rX   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failrQ   )rR   	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)rd   
__module____qualname__rU   rY   r[   r]   rs   r?   r.   r'   rN   rN   o   s_        - - -
4 4 4( ( (- - -(
 (
 (
 (
 (
r.   rN   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_DIRTr1   rb   c                     | S rW   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)rb   )r   r   _cache_hits
signatures_cache_misses	traceback
format_excput)ro   rm   second_callrz   rr   rq   s         r'   ri   ri      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modfilerm   r!   copyusecases_filer    statS_IREADS_IWRITEmaxDiffrX   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 rW   )r   modulespopr   r   remover$   rX   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   rQ   __file__rstripr   )rR   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   listdirrm   r   rX   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 rW   )r   r   getmtimer   rm   )r   r   rR   s     r'   	<genexpr>z1BaseCacheTest.get_cache_mtimes.<locals>.<genexpr>   s^       = = ))"',,t~r*J*JKKL = = = = = =r.   )dictsortedr   rX   s   `r'   get_cache_mtimeszBaseCacheTest.get_cache_mtimes   sO     = = = =$T%8%8%:%:;;= = = = = 	=r.   c                 v    |                                  }|                     t          |          ||           d S rW   )r   rQ   len)rR   nr,   s      r'   check_pycachezBaseCacheTest.check_pycache   s7    !!QA&&&&&r.   c                     d S rW   r?   rX   s    r'   
dummy_testzBaseCacheTest.dummy_test   s    r.   )rd   rt   ru   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
i ddZddZdS )DispatcherCacheUsecasesTestcache_usecases.pydispatcher_caching_test_fodderenvvarsc          	         dt          | j        | j                  z  }t          j                                        }|                    |           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stderrenvr   process failed with code z: 
stdout follows
z
stderr follows

)r   r$   r   r   environr   update
subprocessPopenr   
executablePIPEcommunicate
returncodeAssertionErrordecode)rR   r   codesubp_envpopenouterrs          r'   run_in_separate_processz3DispatcherCacheUsecasesTest.run_in_separate_process  s     t|T\BBBC :??$$    #.$!=(2
%-/ / / $$&&Sq   . ###SZZ\\\\3::<<<<A   ! r.   Nc                    |j         }|                     t          |j                                                  ||j                   |B|                     t          |j                                                  ||j                   d S d S rW   )statsrQ   sum
cache_hitsvaluescache_misses)rR   rS   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.   rW   )rd   rt   ru   r   r   dirnamer   herer   r   r   r   r   r?   r.   r'   r   r      sr        7??8$$DGLL':;;M.G13     4. . . . . .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           | 
                                 d S )Nr   r<   rG            @      @   r;   )r<   g     E@	   )r   r   add_usecaseassertPreciseEqualr   add_objmode_usecaserecord_returnaligned_arrtuple
packed_arrr   )rR   r   r&   recs       r'   test_cachingzTestCache.test_caching%  s   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    	$$&&&&&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<   rG   r   r;   )r   r   r   r   nparanger   rR   r   r&   s      r'   test_caching_nrt_prunedz!TestCache.test_caching_nrt_prunedC  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 )NrG   r<   r            @r   )r   r   innerr   outer_uncachedouterr   s      r'   test_inner_then_outerzTestCache.test_inner_then_outerP  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 )NrG   r<   r   r   r   r   r   )r   r   r   r   r   r   r   s      r'   test_outer_then_innerzTestCache.test_outer_then_innerd  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<   rG   r   r   )r   add_nocache_usecaser   r   r   s      r'   test_no_cachingzTestCache.test_no_cachingr  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   rQ   r   assertInstrmessagerR   r   wr&   s       r'   test_loopliftedzTestCache.test_loopliftedy  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   rQ   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   rQ   r   r
  formatrd   r  r  )rR   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errorrG   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_usecaserQ   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           |                                 }| 	                    |j        dd           |                                  }| 
                    ||           |j        } |dd           | 	                    |dd            |dd           | 	                    |dd           |j        } |dd           | 	                    |dd           |                     |                                 |           |                                  |                     |                                 |           d S )Nr<   rG   r   r   r   r;   )r   r   r   r   r   r   r   r   r   r   assertIsNotrQ   r   )rR   r   mtimesmod2r&   s        r'   test_cache_reusezTestCache.test_cache_reuse  s     ""1S!!!1%%%1a   		!Q#.!,,,#/1---&&((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<   rG   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<   rG   r   r  r4  )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)rR   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<   rG   r   r   r;   N)
r   r   
addCleanupr!   r"   r   
cache_pathr   r   r   )rR   r   r&   r1  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    rR  rT  )rR   	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   rW  r    rR  rT  )rR   pycacherX  s      r'   test_non_writable_pycachez#TestCache.test_non_writable_pycacheE  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   rl   rg  pr   r   base_cmdinputfnrR   s       r'   execute_with_inputz2TestCache.test_ipython.<locals>.execute_with_inputs      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
  )rR   verer&   ro  r   rm  rn  s   `     @@r'   test_ipythonzTestCache.test_ipythonR  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`  ra  rb  r   )compilerr  rc  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 re  rj  rk  s       r'   ro  z4TestCache.test_ipykernel.<locals>.execute_with_input  rp  rq  rr  )r   r   r   rs  rt  ru  r   rv  r   	ipykernelr{  get_tmp_directoryr   r   r
  )	rR   rw  rx  r{  r&   ro  r   rm  rn  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#)rd   rt   ru   r   r   r   r   r  r  r  r  r%  r-  r2  r5  r9  r=  rO  rT  skip_bad_accessunittestskipIfr   namerY  r\  ry  r}  version_infor  r?   r.   r'   r   r   #  s       ' ' '<! ! !  (    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rR   rc   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   rQ   )rR   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.   )rd   rt   ru   r   r   r   __classcell__)rc   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)rR   
mtimes_oldmatch_countkrH   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           |                                 }|                                 t          |                                           }|                                 }|                     ddi           |                     |           |                     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   r   	self_testr   r   r   r   r  r  r   _cache_cache_file_load_indexrQ   keysllget_host_cpu_namer   get_host_cpu_features)rR   r   
cache_sizer0  r2   
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  "",,..//
&&(($$.>	-J$KKK'''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/////r.   c                 P   |                      d           |                                 }|                                 t          |                                           }|                                 }d}t          j                    }|                     ||           | 	                    d|i           | 
                    |           |                     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   r  assertNotEqualr   r  r  r   r  r  r  rQ   r  r  r  )rR   r   r  r0  my_cpu_featuressystem_featuresr2   r  r  r  r  r  r  s                r'   test_user_set_cpu_featuresz2TestCacheWithCpuSetting.test_user_set_cpu_features  s   1  "",,..//
&&((%!799O_===$$)?; 	% 	
 	
 	
 	'''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<<<<<r.   N)rd   rt   ru   _numba_parallel_test_r  r  r  r?   r.   r'   r  r    sG        !5 5 50 0 06!= != != != !=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 )NrG   spawnr;   r<   )r   simple_usecase_callerre   rf   AttributeErrorPoolr   imaprangecloserQ   )rR   r   r&   r   rn   poolrT   s          r'   test_multiprocessingz*TestMultiprocessCache.test_multiprocessing/  s      "" %	"!-g66CC 	" 	" 	"!CCC	"xx{{	dii588,,--CJJLLLLDJJLLLLa1q5kQ./////s   4 AA!0B& &B<N)rd   rt   ru   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)rR   foomodfouts      r'   r   zTestCacheFileCollision.setUpV  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 rW   )r   r   r   r  r  r   r   r$   rX   s    r'   r   zTestCacheFileCollision.tearDownd  sP    )4000)4000%%%%%r.   c                 4    t          | j                  j        S rW   )r   r  barrX   s    r'   import_bar1z"TestCacheFileCollision.import_bar1i      d/0044r.   c                 4    t          | j                  j        S rW   )r   r  r  rX   s    r'   import_bar2z"TestCacheFileCollision.import_bar2l  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)rR   bar1bar2idxname1idxname2s        r'   test_file_locationz)TestCacheFileCollision.test_file_locationo  s    !!!!;*6;*6Hh///++,?@@AAA++N;;<<<<<r.   rf   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
   getvaluerQ   countr  re   rf   
ValueErrorr  rg   rh   cache_file_collision_testerr$   r  r  rj   rk   r   )rR   r  r  bufres1cachelogres2mpqrp   log1got1log2got2s                 r'   test_no_collisionz(TestCacheFileCollision.test_no_collisiony  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)rd   rt   ru   r  r   r   r   r   r   r   r   r   r  r  r   r   r  r  r  r  
skipUnlesshasattrre   r  r?   r.   r'   r  r  B  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)rR   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 rW   )r!   r"   r$   rX   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 )Nr   r   zstdout:
z


stderr:
r   r  )r   r   r   r   r  r   r   r   rQ   r   r  )rR   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)	rd   rt   ru   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$   r   r   r   r   r   r   r   r   r   r   )rR   r   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 rW   )r  rR   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   rQ   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)rd   rt   ru   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__)>rE  llvmlite.bindingbindingr  re   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}  ImportErrorr(   _access_preventable_access_msgr  r  r-   r9   r@   rE   rI   rL   rN   ri   r   r   r   r  r  r  r  r  r  r  rd   mainr?   r.   r'   <module>r#     sG                  				       



                   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$. $. $. $. $.- $. $. $.N[5 [5 [5 [5 [5+ [5 [5 [5|     !<   .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- -A76A7