11import os
22import sys
3- import shutil
43import inspect
5- import importlib
64import contextlib
7- import uuid
85import numpy as np
96import logging
107from io import StringIO
118
129import unittest
13- from numba .tests .support import temp_directory , SerialMixin
10+ from numba .tests .support import SerialMixin , create_temp_module
1411from numba .core import dispatcher
1512
1613
@@ -49,45 +46,10 @@ class Foo(object):
4946jit_module({jit_options})
5047"""
5148
52- def _format_jit_options (self , ** jit_options ):
53- if not jit_options :
54- return ''
55- out = []
56- for key , value in jit_options .items ():
57- if isinstance (value , str ):
58- value = '"{}"' .format (value )
59- out .append ('{}={}' .format (key , value ))
60- return ', ' .join (out )
61-
62- @contextlib .contextmanager
63- def create_temp_jitted_module (self , source_lines = None , ** jit_options ):
64- # Use try/finally so cleanup happens even when an exception is raised
65- try :
66- if source_lines is None :
67- source_lines = self .source_lines
68- tempdir = temp_directory ('test_jit_module' )
69- # Generate random module name
70- temp_module_name = 'test_module_{}' .format (
71- str (uuid .uuid4 ()).replace ('-' , '_' ))
72- temp_module_path = os .path .join (tempdir , temp_module_name + '.py' )
73-
74- jit_options = self ._format_jit_options (** jit_options )
75- with open (temp_module_path , 'w' ) as f :
76- lines = source_lines .format (jit_options = jit_options )
77- f .write (lines )
78- # Add test_module to sys.path so it can be imported
79- sys .path .insert (0 , tempdir )
80- test_module = importlib .import_module (temp_module_name )
81- yield test_module
82- finally :
83- sys .modules .pop (temp_module_name , None )
84- sys .path .remove (tempdir )
85- shutil .rmtree (tempdir )
86-
8749 def test_create_temp_jitted_module (self ):
8850 sys_path_original = list (sys .path )
8951 sys_modules_original = dict (sys .modules )
90- with self .create_temp_jitted_module ( ) as test_module :
52+ with create_temp_module ( self .source_lines ) as test_module :
9153 temp_module_dir = os .path .dirname (test_module .__file__ )
9254 self .assertEqual (temp_module_dir , sys .path [0 ])
9355 self .assertEqual (sys .path [1 :], sys_path_original )
@@ -100,15 +62,15 @@ def test_create_temp_jitted_module_with_exception(self):
10062 try :
10163 sys_path_original = list (sys .path )
10264 sys_modules_original = dict (sys .modules )
103- with self .create_temp_jitted_module ( ):
65+ with create_temp_module ( self .source_lines ):
10466 raise ValueError ("Something went wrong!" )
10567 except ValueError :
10668 # Test that modifications to sys.path / sys.modules are reverted
10769 self .assertEqual (sys .path , sys_path_original )
10870 self .assertEqual (sys .modules , sys_modules_original )
10971
11072 def test_jit_module (self ):
111- with self .create_temp_jitted_module ( ) as test_module :
73+ with create_temp_module ( self .source_lines ) as test_module :
11274 self .assertIsInstance (test_module .inc , dispatcher .Dispatcher )
11375 self .assertIsInstance (test_module .add , dispatcher .Dispatcher )
11476 self .assertIsInstance (test_module .inc_add , dispatcher .Dispatcher )
@@ -130,7 +92,8 @@ def test_jit_module_jit_options(self):
13092 "error_model" : "numpy" ,
13193 "boundscheck" : False ,
13294 }
133- with self .create_temp_jitted_module (** jit_options ) as test_module :
95+ with create_temp_module (self .source_lines ,
96+ ** jit_options ) as test_module :
13497 self .assertEqual (test_module .inc .targetoptions , jit_options )
13598
13699 def test_jit_module_jit_options_override (self ):
@@ -150,8 +113,8 @@ def add(x, y):
150113 "error_model" : "numpy" ,
151114 "boundscheck" : False ,
152115 }
153- with self . create_temp_jitted_module (source_lines = source_lines ,
154- ** jit_options ) as test_module :
116+ with create_temp_module (source_lines = source_lines ,
117+ ** jit_options ) as test_module :
155118 self .assertEqual (test_module .add .targetoptions , jit_options )
156119 # Test that manual jit-wrapping overrides jit_module options
157120 self .assertEqual (test_module .inc .targetoptions ,
@@ -165,7 +128,8 @@ def test_jit_module_logging_output(self):
165128 "error_model" : "numpy" ,
166129 }
167130 with captured_logs (logger ) as logs :
168- with self .create_temp_jitted_module (** jit_options ) as test_module :
131+ with create_temp_module (self .source_lines ,
132+ ** jit_options ) as test_module :
169133 logs = logs .getvalue ()
170134 expected = ["Auto decorating function" ,
171135 "from module {}" .format (test_module .__name__ ),
@@ -177,5 +141,5 @@ def test_jit_module_logging_level(self):
177141 # Test there's no logging for INFO level
178142 logger .setLevel (logging .INFO )
179143 with captured_logs (logger ) as logs :
180- with self .create_temp_jitted_module ( ):
144+ with create_temp_module ( self .source_lines ):
181145 self .assertEqual (logs .getvalue (), '' )
0 commit comments