<s> from __future__ import print_function , division , absolute_import <EOL> import errno <EOL> import imp <EOL> import os <EOL> import shutil <EOL> import stat <EOL> import subprocess <EOL> import sys <EOL> import tempfile <EOL> import threading <EOL> import warnings <EOL> import numpy as np <EOL> from numba import unittest_support as unittest <EOL> from numba import utils , vectorize , jit , generated_jit , types , appdirs <EOL> from numba . errors import NumbaWarning <EOL> from . support import TestCase , tag , temp_directory , import_dynamic <EOL> def dummy ( x ) : <EOL> return x <EOL> def add ( x , y ) : <EOL> return x + y <EOL> def addsub ( x , y , z ) : <EOL> return x - y + z <EOL> def addsub_defaults ( x , y = <NUM_LIT:2> , z = <NUM_LIT:3> ) : <EOL> return x - y + z <EOL> def star_defaults ( x , y = <NUM_LIT:2> , * z ) : <EOL> return x , y , z <EOL> def generated_usecase ( x , y = <NUM_LIT:5> ) : <EOL> if isinstance ( x , types . Complex ) : <EOL> def impl ( x , y ) : <EOL> return x + y <EOL> else : <EOL> def impl ( x , y ) : <EOL> return x - y <EOL> return impl <EOL> def bad_generated_usecase ( x , y = <NUM_LIT:5> ) : <EOL> if isinstance ( x , types . Complex ) : <EOL> def impl ( x ) : <EOL> return x <EOL> else : <EOL> def impl ( x , y = <NUM_LIT:6> ) : <EOL> return x - y <EOL> return impl <EOL> class BaseTest ( TestCase ) : <EOL> jit_args = dict ( nopython = True ) <EOL> def compile_func ( self , pyfunc ) : <EOL> def check ( * args , ** kwargs ) : <EOL> expected = pyfunc ( * args , ** kwargs ) <EOL> result = f ( * args , ** kwargs ) <EOL> self . assertPreciseEqual ( result , expected ) <EOL> f = jit ( ** self . jit_args ) ( pyfunc ) <EOL> return f , check <EOL> class TestDispatcher ( BaseTest ) : <EOL> def test_no_argument ( self ) : <EOL> @ jit <EOL> def foo ( ) : <EOL> return <NUM_LIT:1> <EOL> foo ( ) <EOL> def test_coerce_input_types ( self ) : <EOL> c_add = jit ( nopython = True ) ( add ) <EOL> self . assertPreciseEqual ( c_add ( <NUM_LIT> , <NUM_LIT> ) , add ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> self . assertPreciseEqual ( c_add ( <NUM_LIT> , <NUM_LIT> ) , add ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> self . assertPreciseEqual ( c_add ( <NUM_LIT> , <NUM_LIT> ) , add ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> self . assertPreciseEqual ( c_add ( <NUM_LIT> , <NUM_LIT> ) , add ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> c_add = jit ( '<STR_LIT>' , nopython = True ) ( add ) <EOL> self . assertPreciseEqual ( c_add ( <NUM_LIT> , <NUM_LIT> ) , add ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> self . assertPreciseEqual ( c_add ( <NUM_LIT> , <NUM_LIT> ) , add ( <NUM_LIT:12> , <NUM_LIT> ) ) <EOL> with self . assertRaises ( TypeError ) : <EOL> c_add ( <NUM_LIT> , <NUM_LIT> ) <EOL> def test_ambiguous_new_version ( self ) : <EOL> """<STR_LIT>""" <EOL> @ jit <EOL> def foo ( a , b ) : <EOL> return a + b <EOL> INT = <NUM_LIT:1> <EOL> FLT = <NUM_LIT> <EOL> self . assertAlmostEqual ( foo ( INT , FLT ) , INT + FLT ) <EOL> self . assertEqual ( len ( foo . overloads ) , <NUM_LIT:1> ) <EOL> self . assertAlmostEqual ( foo ( FLT , INT ) , FLT + INT ) <EOL> self . assertEqual ( len ( foo . overloads ) , <NUM_LIT:2> ) <EOL> self . assertAlmostEqual ( foo ( FLT , FLT ) , FLT + FLT ) <EOL> self . assertEqual ( len ( foo . overloads ) , <NUM_LIT:3> ) <EOL> self . assertAlmostEqual ( foo ( <NUM_LIT:1> , <NUM_LIT:1> ) , INT + INT ) <EOL> self . assertEqual ( len ( foo . overloads ) , <NUM_LIT:4> , "<STR_LIT>" <EOL> "<STR_LIT:version>" ) <EOL> def test_lock ( self ) : <EOL> """<STR_LIT>""" <EOL> errors = [ ] <EOL> @ jit <EOL> def foo ( x ) : <EOL> return x + <NUM_LIT:1> <EOL> def wrapper ( ) : <EOL> try : <EOL> self . assertEqual ( foo ( <NUM_LIT:1> ) , <NUM_LIT:2> ) <EOL> except BaseException as e : <EOL> errors . append ( e ) <EOL> threads = [ threading . Thread ( target = wrapper ) for i in range ( <NUM_LIT:16> ) ] <EOL> for t in threads : <EOL> t . start ( ) <EOL> for t in threads : <EOL> t . join ( ) <EOL> self . assertFalse ( errors ) <EOL> def test_explicit_signatures ( self ) : <EOL> f = jit ( "<STR_LIT>" ) ( add ) <EOL> self . assertPreciseEqual ( f ( <NUM_LIT> , <NUM_LIT> ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( len ( f . overloads ) , <NUM_LIT:1> , f . overloads ) <EOL> f = jit ( [ "<STR_LIT>" , "<STR_LIT>" ] ) ( add ) <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:1> , <NUM_LIT:2> ) , <NUM_LIT:3> ) <EOL> self . assertPreciseEqual ( f ( <NUM_LIT> , <NUM_LIT> ) , <NUM_LIT> ) <EOL> self . assertPreciseEqual ( f ( np . int32 ( <NUM_LIT:1> ) , <NUM_LIT> ) , <NUM_LIT> ) <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> f ( <NUM_LIT> , <NUM_LIT> ) <EOL> self . assertIn ( "<STR_LIT>" , str ( cm . exception ) ) <EOL> self . assertEqual ( len ( f . overloads ) , <NUM_LIT:2> , f . overloads ) <EOL> f = jit ( [ "<STR_LIT>" , "<STR_LIT>" ] ) ( add ) <EOL> self . assertPreciseEqual ( f ( np . float32 ( <NUM_LIT:1> ) , np . float32 ( <NUM_LIT:2> ** - <NUM_LIT> ) ) , <NUM_LIT:1.0> ) <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:1> , <NUM_LIT:2> ** - <NUM_LIT> ) , <NUM_LIT> ) <EOL> f = jit ( [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) ( add ) <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> f ( <NUM_LIT:1.0> , <NUM_LIT> ) <EOL> self . assertRegexpMatches ( <EOL> str ( cm . exception ) , <EOL> r"<STR_LIT>" <EOL> r"<STR_LIT>" <EOL> r"<STR_LIT>" <EOL> ) <EOL> self . assertNotIn ( "<STR_LIT>" , str ( cm . exception ) ) <EOL> def test_signature_mismatch ( self ) : <EOL> tmpl = "<STR_LIT>" <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> jit ( "<STR_LIT>" ) ( add ) <EOL> self . assertIn ( tmpl % <NUM_LIT:0> , str ( cm . exception ) ) <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> jit ( "<STR_LIT>" ) ( add ) <EOL> self . assertIn ( tmpl % <NUM_LIT:1> , str ( cm . exception ) ) <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> jit ( "<STR_LIT>" ) ( add ) <EOL> self . assertIn ( tmpl % <NUM_LIT:3> , str ( cm . exception ) ) <EOL> jit ( "<STR_LIT>" , forceobj = True ) ( add ) <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> jit ( "<STR_LIT>" , forceobj = True ) ( add ) <EOL> self . assertIn ( tmpl % <NUM_LIT:1> , str ( cm . exception ) ) <EOL> def test_matching_error_message ( self ) : <EOL> f = jit ( "<STR_LIT>" ) ( add ) <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> f ( <NUM_LIT> , <NUM_LIT> ) <EOL> self . assertEqual ( str ( cm . exception ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def test_disabled_compilation ( self ) : <EOL> @ jit <EOL> def foo ( a ) : <EOL> return a <EOL> foo . compile ( "<STR_LIT>" ) <EOL> foo . disable_compile ( ) <EOL> with self . assertRaises ( RuntimeError ) as raises : <EOL> foo . compile ( "<STR_LIT>" ) <EOL> self . assertEqual ( str ( raises . exception ) , "<STR_LIT>" ) <EOL> self . assertEqual ( len ( foo . signatures ) , <NUM_LIT:1> ) <EOL> def test_disabled_compilation_through_list ( self ) : <EOL> @ jit ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def foo ( a ) : <EOL> return a <EOL> with self . assertRaises ( RuntimeError ) as raises : <EOL> foo . compile ( "<STR_LIT>" ) <EOL> self . assertEqual ( str ( raises . exception ) , "<STR_LIT>" ) <EOL> self . assertEqual ( len ( foo . signatures ) , <NUM_LIT:2> ) <EOL> def test_disabled_compilation_nested_call ( self ) : <EOL> @ jit ( [ "<STR_LIT>" ] ) <EOL> def foo ( a ) : <EOL> return a <EOL> @ jit <EOL> def bar ( ) : <EOL> foo ( <NUM_LIT:1> ) <EOL> foo ( np . ones ( <NUM_LIT:1> ) ) <EOL> with self . assertRaises ( TypeError ) as raises : <EOL> bar ( ) <EOL> m = "<STR_LIT>" <EOL> self . assertEqual ( str ( raises . exception ) , m ) <EOL> class TestSignatureHandling ( BaseTest ) : <EOL> """<STR_LIT>""" <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_named_args ( self ) : <EOL> """<STR_LIT>""" <EOL> f , check = self . compile_func ( addsub ) <EOL> check ( <NUM_LIT:3> , z = <NUM_LIT:10> , y = <NUM_LIT:4> ) <EOL> check ( <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:10> ) <EOL> check ( x = <NUM_LIT:3> , y = <NUM_LIT:4> , z = <NUM_LIT:10> ) <EOL> self . assertEqual ( len ( f . overloads ) , <NUM_LIT:1> ) <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> f ( <NUM_LIT:3> , <NUM_LIT:4> , y = <NUM_LIT:6> , z = <NUM_LIT:7> ) <EOL> self . assertIn ( "<STR_LIT>" , <EOL> str ( cm . exception ) ) <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> f ( ) <EOL> self . assertIn ( "<STR_LIT>" , <EOL> str ( cm . exception ) ) <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> f ( <NUM_LIT:3> , <NUM_LIT:4> , y = <NUM_LIT:6> ) <EOL> self . assertIn ( "<STR_LIT>" , str ( cm . exception ) ) <EOL> def test_default_args ( self ) : <EOL> """<STR_LIT>""" <EOL> f , check = self . compile_func ( addsub_defaults ) <EOL> check ( <NUM_LIT:3> , z = <NUM_LIT:10> , y = <NUM_LIT:4> ) <EOL> check ( <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:10> ) <EOL> check ( x = <NUM_LIT:3> , y = <NUM_LIT:4> , z = <NUM_LIT:10> ) <EOL> check ( <NUM_LIT:3> , z = <NUM_LIT:10> ) <EOL> check ( <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> check ( x = <NUM_LIT:3> , y = <NUM_LIT:4> ) <EOL> check ( <NUM_LIT:3> ) <EOL> check ( x = <NUM_LIT:3> ) <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> f ( <NUM_LIT:3> , <NUM_LIT:4> , y = <NUM_LIT:6> , z = <NUM_LIT:7> ) <EOL> self . assertIn ( "<STR_LIT>" , <EOL> str ( cm . exception ) ) <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> f ( ) <EOL> self . assertIn ( "<STR_LIT>" , <EOL> str ( cm . exception ) ) <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> f ( y = <NUM_LIT:6> , z = <NUM_LIT:7> ) <EOL> self . assertIn ( "<STR_LIT>" , str ( cm . exception ) ) <EOL> def test_star_args ( self ) : <EOL> """<STR_LIT>""" <EOL> f , check = self . compile_func ( star_defaults ) <EOL> check ( <NUM_LIT:4> ) <EOL> check ( <NUM_LIT:4> , <NUM_LIT:5> ) <EOL> check ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) <EOL> check ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> ) <EOL> check ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ) <EOL> check ( x = <NUM_LIT:4> ) <EOL> check ( x = <NUM_LIT:4> , y = <NUM_LIT:5> ) <EOL> check ( <NUM_LIT:4> , y = <NUM_LIT:5> ) <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> f ( <NUM_LIT:4> , <NUM_LIT:5> , y = <NUM_LIT:6> ) <EOL> self . assertIn ( "<STR_LIT>" , str ( cm . exception ) ) <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> f ( <NUM_LIT:4> , <NUM_LIT:5> , z = <NUM_LIT:6> ) <EOL> self . assertIn ( "<STR_LIT>" , str ( cm . exception ) ) <EOL> with self . assertRaises ( TypeError ) as cm : <EOL> f ( <NUM_LIT:4> , x = <NUM_LIT:6> ) <EOL> self . assertIn ( "<STR_LIT>" , str ( cm . exception ) ) <EOL> class TestSignatureHandlingObjectMode ( TestSignatureHandling ) : <EOL> """<STR_LIT>""" <EOL> jit_args = dict ( forceobj = True ) <EOL> class TestGeneratedDispatcher ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_generated ( self ) : <EOL> f = generated_jit ( nopython = True ) ( generated_usecase ) <EOL> self . assertEqual ( f ( <NUM_LIT:8> ) , <NUM_LIT:8> - <NUM_LIT:5> ) <EOL> self . assertEqual ( f ( x = <NUM_LIT:8> ) , <NUM_LIT:8> - <NUM_LIT:5> ) <EOL> self . assertEqual ( f ( x = <NUM_LIT:8> , y = <NUM_LIT:4> ) , <NUM_LIT:8> - <NUM_LIT:4> ) <EOL> self . assertEqual ( f ( <NUM_LIT> ) , <NUM_LIT:5> + <NUM_LIT> ) <EOL> self . assertEqual ( f ( <NUM_LIT> , <NUM_LIT> ) , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertEqual ( f ( x = <NUM_LIT> , y = <NUM_LIT:7> ) , <NUM_LIT:7> + <NUM_LIT> ) <EOL> def test_signature_errors ( self ) : <EOL> """<STR_LIT>""" <EOL> f = generated_jit ( nopython = True ) ( bad_generated_usecase ) <EOL> with self . assertRaises ( TypeError ) as raises : <EOL> f ( <NUM_LIT> ) <EOL> self . assertIn ( "<STR_LIT>" , <EOL> str ( raises . exception ) ) <EOL> with self . assertRaises ( TypeError ) as raises : <EOL> f ( <NUM_LIT:1> ) <EOL> self . assertIn ( "<STR_LIT>" , <EOL> str ( raises . exception ) ) <EOL> class TestDispatcherMethods ( TestCase ) : <EOL> def test_recompile ( self ) : <EOL> closure = <NUM_LIT:1> <EOL> @ jit <EOL> def foo ( x ) : <EOL> return x + closure <EOL> self . assertPreciseEqual ( foo ( <NUM_LIT:1> ) , <NUM_LIT:2> ) <EOL> self . assertPreciseEqual ( foo ( <NUM_LIT> ) , <NUM_LIT> ) <EOL> self . assertEqual ( len ( foo . signatures ) , <NUM_LIT:2> ) <EOL> closure = <NUM_LIT:2> <EOL> self . assertPreciseEqual ( foo ( <NUM_LIT:1> ) , <NUM_LIT:2> ) <EOL> foo . recompile ( ) <EOL> self . assertEqual ( len ( foo . signatures ) , <NUM_LIT:2> ) <EOL> self . assertPreciseEqual ( foo ( <NUM_LIT:1> ) , <NUM_LIT:3> ) <EOL> self . assertPreciseEqual ( foo ( <NUM_LIT> ) , <NUM_LIT> ) <EOL> def test_recompile_signatures ( self ) : <EOL> closure = <NUM_LIT:1> <EOL> @ jit ( "<STR_LIT>" ) <EOL> def foo ( x ) : <EOL> return x + closure <EOL> self . assertPreciseEqual ( foo ( <NUM_LIT:1> ) , <NUM_LIT:2> ) <EOL> self . assertPreciseEqual ( foo ( <NUM_LIT> ) , <NUM_LIT:2> ) <EOL> closure = <NUM_LIT:2> <EOL> self . assertPreciseEqual ( foo ( <NUM_LIT:1> ) , <NUM_LIT:2> ) <EOL> foo . recompile ( ) <EOL> self . assertPreciseEqual ( foo ( <NUM_LIT:1> ) , <NUM_LIT:3> ) <EOL> self . assertPreciseEqual ( foo ( <NUM_LIT> ) , <NUM_LIT:3> ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_inspect_llvm ( self ) : <EOL> @ jit <EOL> def foo ( explicit_arg1 , explicit_arg2 ) : <EOL> return explicit_arg1 + explicit_arg2 <EOL> foo ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> foo ( <NUM_LIT:1.0> , <NUM_LIT:1> ) <EOL> foo ( <NUM_LIT:1.0> , <NUM_LIT:1.0> ) <EOL> llvms = foo . inspect_llvm ( ) <EOL> self . assertEqual ( len ( llvms ) , <NUM_LIT:3> ) <EOL> for llvm_bc in llvms . values ( ) : <EOL> self . assertIn ( "<STR_LIT:foo>" , llvm_bc ) <EOL> self . assertIn ( "<STR_LIT>" , llvm_bc ) <EOL> self . assertIn ( "<STR_LIT>" , llvm_bc ) <EOL> def test_inspect_asm ( self ) : <EOL> @ jit <EOL> def foo ( explicit_arg1 , explicit_arg2 ) : <EOL> return explicit_arg1 + explicit_arg2 <EOL> foo ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> foo ( <NUM_LIT:1.0> , <NUM_LIT:1> ) <EOL> foo ( <NUM_LIT:1.0> , <NUM_LIT:1.0> ) <EOL> asms = foo . inspect_asm ( ) <EOL> self . assertEqual ( len ( asms ) , <NUM_LIT:3> ) <EOL> for asm in asms . values ( ) : <EOL> self . assertTrue ( "<STR_LIT:foo>" in asm ) <EOL> def test_inspect_types ( self ) : <EOL> @ jit <EOL> def foo ( a , b ) : <EOL> return a + b <EOL> foo ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> foo . inspect_types ( utils . StringIO ( ) ) <EOL> def test_issue_with_array_layout_conflict ( self ) : <EOL> """<STR_LIT>""" <EOL> def pyfunc ( A , i , j ) : <EOL> return A [ i , j ] <EOL> cfunc = jit ( pyfunc ) <EOL> ary_c_and_f = np . array ( [ [ <NUM_LIT:1.> ] ] ) <EOL> ary_c = np . array ( [ [ <NUM_LIT:0.> , <NUM_LIT:1.> ] , [ <NUM_LIT> , <NUM_LIT> ] ] , order = '<STR_LIT:C>' ) <EOL> ary_f = np . array ( [ [ <NUM_LIT:0.> , <NUM_LIT:1.> ] , [ <NUM_LIT> , <NUM_LIT> ] ] , order = '<STR_LIT:F>' ) <EOL> exp_c = pyfunc ( ary_c , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> exp_f = pyfunc ( ary_f , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> self . assertEqual ( <NUM_LIT:1.> , cfunc ( ary_c_and_f , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> got_c = cfunc ( ary_c , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> got_f = cfunc ( ary_f , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> self . assertEqual ( exp_c , got_c ) <EOL> self . assertEqual ( exp_f , got_f ) <EOL> class TestCache ( TestCase ) : <EOL> here = os . path . dirname ( __file__ ) <EOL> usecases_file = os . path . join ( here , "<STR_LIT>" ) <EOL> modname = "<STR_LIT>" <EOL> def setUp ( self ) : <EOL> self . tempdir = temp_directory ( '<STR_LIT>' ) <EOL> sys . path . insert ( <NUM_LIT:0> , self . tempdir ) <EOL> self . modfile = os . path . join ( self . tempdir , self . modname + "<STR_LIT>" ) <EOL> self . cache_dir = os . path . join ( self . tempdir , "<STR_LIT>" ) <EOL> shutil . copy ( self . usecases_file , self . modfile ) <EOL> self . maxDiff = None <EOL> def tearDown ( self ) : <EOL> sys . modules . pop ( self . modname , None ) <EOL> sys . path . remove ( self . tempdir ) <EOL> def import_module ( self ) : <EOL> old = sys . modules . pop ( self . modname , None ) <EOL> if old is not None : <EOL> if sys . version_info >= ( <NUM_LIT:3> , ) : <EOL> cached = [ old . __cached__ ] <EOL> else : <EOL> if old . __file__ . endswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> cached = [ old . __file__ ] <EOL> else : <EOL> cached = [ old . __file__ + '<STR_LIT:c>' , old . __file__ + '<STR_LIT:o>' ] <EOL> for fn in cached : <EOL> try : <EOL> os . unlink ( fn ) <EOL> except OSError as e : <EOL> if e . errno != errno . ENOENT : <EOL> raise <EOL> mod = import_dynamic ( self . modname ) <EOL> self . assertEqual ( mod . __file__ . rstrip ( '<STR_LIT>' ) , self . modfile ) <EOL> return mod <EOL> def cache_contents ( self ) : <EOL> try : <EOL> return [ fn for fn in os . listdir ( self . cache_dir ) <EOL> if not fn . endswith ( ( '<STR_LIT>' , "<STR_LIT>" ) ) ] <EOL> except OSError as e : <EOL> if e . errno != errno . ENOENT : <EOL> raise <EOL> return [ ] <EOL> def get_cache_mtimes ( self ) : <EOL> return dict ( ( fn , os . path . getmtime ( os . path . join ( self . cache_dir , fn ) ) ) <EOL> for fn in sorted ( self . cache_contents ( ) ) ) <EOL> def check_pycache ( self , n ) : <EOL> c = self . cache_contents ( ) <EOL> self . assertEqual ( len ( c ) , n , c ) <EOL> def dummy_test ( self ) : <EOL> pass <EOL> def run_in_separate_process ( self ) : <EOL> code = """<STR_LIT>""" % dict ( tempdir = self . tempdir , modname = self . modname , <EOL> test_class = self . __class__ . __name__ ) <EOL> popen = subprocess . Popen ( [ sys . executable , "<STR_LIT:-c>" , code ] , <EOL> stdout = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> out , err = popen . communicate ( ) <EOL> if popen . returncode != <NUM_LIT:0> : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> % ( popen . returncode , err . decode ( ) ) ) <EOL> def check_module ( self , mod ) : <EOL> self . check_pycache ( <NUM_LIT:0> ) <EOL> f = mod . add_usecase <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:2> , <NUM_LIT:3> ) , <NUM_LIT:6> ) <EOL> self . check_pycache ( <NUM_LIT:2> ) <EOL> self . assertPreciseEqual ( f ( <NUM_LIT> , <NUM_LIT:3> ) , <NUM_LIT> ) <EOL> self . check_pycache ( <NUM_LIT:3> ) <EOL> f = mod . add_objmode_usecase <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:2> , <NUM_LIT:3> ) , <NUM_LIT:6> ) <EOL> self . check_pycache ( <NUM_LIT:5> ) <EOL> self . assertPreciseEqual ( f ( <NUM_LIT> , <NUM_LIT:3> ) , <NUM_LIT> ) <EOL> self . check_pycache ( <NUM_LIT:6> ) <EOL> def check_hits ( self , func , hits , misses = None ) : <EOL> st = func . stats <EOL> self . assertEqual ( sum ( st . cache_hits . values ( ) ) , hits , st . cache_hits ) <EOL> if misses is not None : <EOL> self . assertEqual ( sum ( st . cache_misses . values ( ) ) , misses , <EOL> st . cache_misses ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_caching ( self ) : <EOL> self . check_pycache ( <NUM_LIT:0> ) <EOL> mod = self . import_module ( ) <EOL> self . check_pycache ( <NUM_LIT:0> ) <EOL> f = mod . add_usecase <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:2> , <NUM_LIT:3> ) , <NUM_LIT:6> ) <EOL> self . check_pycache ( <NUM_LIT:2> ) <EOL> self . assertPreciseEqual ( f ( <NUM_LIT> , <NUM_LIT:3> ) , <NUM_LIT> ) <EOL> self . check_pycache ( <NUM_LIT:3> ) <EOL> self . check_hits ( f , <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> f = mod . add_objmode_usecase <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:2> , <NUM_LIT:3> ) , <NUM_LIT:6> ) <EOL> self . check_pycache ( <NUM_LIT:5> ) <EOL> self . assertPreciseEqual ( f ( <NUM_LIT> , <NUM_LIT:3> ) , <NUM_LIT> ) <EOL> self . check_pycache ( <NUM_LIT:6> ) <EOL> self . check_hits ( f , <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> f = mod . record_return <EOL> rec = f ( mod . aligned_arr , <NUM_LIT:1> ) <EOL> self . assertPreciseEqual ( tuple ( rec ) , ( <NUM_LIT:2> , <NUM_LIT> ) ) <EOL> rec = f ( mod . packed_arr , <NUM_LIT:1> ) <EOL> self . assertPreciseEqual ( tuple ( rec ) , ( <NUM_LIT:2> , <NUM_LIT> ) ) <EOL> self . check_pycache ( <NUM_LIT:9> ) <EOL> self . check_hits ( f , <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> f = mod . generated_usecase <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:3> , <NUM_LIT:2> ) , <NUM_LIT:1> ) <EOL> self . assertPreciseEqual ( f ( <NUM_LIT> , <NUM_LIT:2> ) , <NUM_LIT:2> + <NUM_LIT> ) <EOL> self . run_in_separate_process ( ) <EOL> def test_inner_then_outer ( self ) : <EOL> mod = self . import_module ( ) <EOL> self . assertPreciseEqual ( mod . inner ( <NUM_LIT:3> , <NUM_LIT:2> ) , <NUM_LIT:6> ) <EOL> self . check_pycache ( <NUM_LIT:2> ) <EOL> f = mod . outer_uncached <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:3> , <NUM_LIT:2> ) , <NUM_LIT:2> ) <EOL> self . check_pycache ( <NUM_LIT:2> ) <EOL> mod = self . import_module ( ) <EOL> f = mod . outer_uncached <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:3> , <NUM_LIT:2> ) , <NUM_LIT:2> ) <EOL> self . check_pycache ( <NUM_LIT:2> ) <EOL> f = mod . outer <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:3> , <NUM_LIT:2> ) , <NUM_LIT:2> ) <EOL> self . check_pycache ( <NUM_LIT:4> ) <EOL> self . assertPreciseEqual ( f ( <NUM_LIT> , <NUM_LIT:2> ) , <NUM_LIT> ) <EOL> self . check_pycache ( <NUM_LIT:6> ) <EOL> def test_outer_then_inner ( self ) : <EOL> mod = self . import_module ( ) <EOL> self . assertPreciseEqual ( mod . outer ( <NUM_LIT:3> , <NUM_LIT:2> ) , <NUM_LIT:2> ) <EOL> self . check_pycache ( <NUM_LIT:4> ) <EOL> self . assertPreciseEqual ( mod . outer_uncached ( <NUM_LIT:3> , <NUM_LIT:2> ) , <NUM_LIT:2> ) <EOL> self . check_pycache ( <NUM_LIT:4> ) <EOL> mod = self . import_module ( ) <EOL> f = mod . inner <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:3> , <NUM_LIT:2> ) , <NUM_LIT:6> ) <EOL> self . check_pycache ( <NUM_LIT:4> ) <EOL> self . assertPreciseEqual ( f ( <NUM_LIT> , <NUM_LIT:2> ) , <NUM_LIT> ) <EOL> self . check_pycache ( <NUM_LIT:5> ) <EOL> def test_no_caching ( self ) : <EOL> mod = self . import_module ( ) <EOL> f = mod . add_nocache_usecase <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:2> , <NUM_LIT:3> ) , <NUM_LIT:6> ) <EOL> self . check_pycache ( <NUM_LIT:0> ) <EOL> def test_looplifted ( self ) : <EOL> mod = self . import_module ( ) <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> warnings . simplefilter ( '<STR_LIT>' , NumbaWarning ) <EOL> f = mod . looplifted <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:4> ) , <NUM_LIT:6> ) <EOL> self . check_pycache ( <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( w ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( str ( w [ <NUM_LIT:0> ] . message ) , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def test_ctypes ( self ) : <EOL> mod = self . import_module ( ) <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> warnings . simplefilter ( '<STR_LIT>' , NumbaWarning ) <EOL> f = mod . use_c_sin <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:0.0> ) , <NUM_LIT:0.0> ) <EOL> self . check_pycache ( <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( w ) , <NUM_LIT:1> ) <EOL> self . assertIn ( '<STR_LIT>' , <EOL> str ( w [ <NUM_LIT:0> ] . message ) ) <EOL> def test_closure ( self ) : <EOL> mod = self . import_module ( ) <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> warnings . simplefilter ( '<STR_LIT>' , NumbaWarning ) <EOL> f = mod . closure1 <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:3> ) , <NUM_LIT:6> ) <EOL> f = mod . closure2 <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:3> ) , <NUM_LIT:8> ) <EOL> self . check_pycache ( <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( w ) , <NUM_LIT:2> ) <EOL> for item in w : <EOL> self . assertIn ( '<STR_LIT>' , <EOL> str ( item . message ) ) <EOL> def test_cache_reuse ( self ) : <EOL> mod = self . import_module ( ) <EOL> mod . add_usecase ( <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> mod . add_usecase ( <NUM_LIT> , <NUM_LIT> ) <EOL> mod . add_objmode_usecase ( <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> mod . outer_uncached ( <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> mod . outer ( <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> mod . record_return ( mod . packed_arr , <NUM_LIT:0> ) <EOL> mod . record_return ( mod . aligned_arr , <NUM_LIT:1> ) <EOL> mod . generated_usecase ( <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> mtimes = self . get_cache_mtimes ( ) <EOL> self . check_hits ( mod . add_usecase , <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> mod2 = self . import_module ( ) <EOL> self . assertIsNot ( mod , mod2 ) <EOL> f = mod2 . add_usecase <EOL> f ( <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> self . check_hits ( f , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> f ( <NUM_LIT> , <NUM_LIT> ) <EOL> self . check_hits ( f , <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> f = mod2 . add_objmode_usecase <EOL> f ( <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> self . check_hits ( f , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> self . assertEqual ( self . get_cache_mtimes ( ) , mtimes ) <EOL> self . run_in_separate_process ( ) <EOL> self . assertEqual ( self . get_cache_mtimes ( ) , mtimes ) <EOL> def test_cache_invalidate ( self ) : <EOL> mod = self . import_module ( ) <EOL> f = mod . add_usecase <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:2> , <NUM_LIT:3> ) , <NUM_LIT:6> ) <EOL> with open ( self . modfile , "<STR_LIT:a>" ) as f : <EOL> f . write ( "<STR_LIT>" ) <EOL> mod = self . import_module ( ) <EOL> f = mod . add_usecase <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:2> , <NUM_LIT:3> ) , <NUM_LIT:15> ) <EOL> f = mod . add_objmode_usecase <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:2> , <NUM_LIT:3> ) , <NUM_LIT:15> ) <EOL> def test_recompile ( self ) : <EOL> mod = self . import_module ( ) <EOL> f = mod . add_usecase <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:2> , <NUM_LIT:3> ) , <NUM_LIT:6> ) <EOL> mod = self . import_module ( ) <EOL> f = mod . add_usecase <EOL> mod . Z = <NUM_LIT:10> <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:2> , <NUM_LIT:3> ) , <NUM_LIT:6> ) <EOL> f . recompile ( ) <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:2> , <NUM_LIT:3> ) , <NUM_LIT:15> ) <EOL> mod = self . import_module ( ) <EOL> f = mod . add_usecase <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:2> , <NUM_LIT:3> ) , <NUM_LIT:15> ) <EOL> def test_same_names ( self ) : <EOL> mod = self . import_module ( ) <EOL> f = mod . renamed_function1 <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:2> ) , <NUM_LIT:4> ) <EOL> f = mod . renamed_function2 <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:2> ) , <NUM_LIT:8> ) <EOL> def _test_pycache_fallback ( self ) : <EOL> """<STR_LIT>""" <EOL> mod = self . import_module ( ) <EOL> f = mod . add_usecase <EOL> self . addCleanup ( shutil . rmtree , f . stats . cache_path , ignore_errors = True ) <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:2> , <NUM_LIT:3> ) , <NUM_LIT:6> ) <EOL> self . check_hits ( f , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> mod2 = self . import_module ( ) <EOL> f = mod2 . add_usecase <EOL> self . assertPreciseEqual ( f ( <NUM_LIT:2> , <NUM_LIT:3> ) , <NUM_LIT:6> ) <EOL> self . check_hits ( f , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> self . check_pycache ( <NUM_LIT:0> ) <EOL> @ unittest . skipIf ( os . name == "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> def test_non_creatable_pycache ( self ) : <EOL> old_perms = os . stat ( self . tempdir ) . st_mode <EOL> os . chmod ( self . tempdir , <NUM_LIT> ) <EOL> self . addCleanup ( os . chmod , self . tempdir , old_perms ) <EOL> self . _test_pycache_fallback ( ) <EOL> @ unittest . skipIf ( os . name == "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> def test_non_writable_pycache ( self ) : <EOL> pycache = os . path . join ( self . tempdir , '<STR_LIT>' ) <EOL> os . mkdir ( pycache ) <EOL> old_perms = os . stat ( pycache ) . st_mode <EOL> os . chmod ( pycache , <NUM_LIT> ) <EOL> self . addCleanup ( os . chmod , pycache , old_perms ) <EOL> self . _test_pycache_fallback ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
