def test_jit_module_logging_level(self): logger = logging.getLogger('numba.core.decorators') # Test there's no logging for INFO level logger.setLevel(logging.INFO) with captured_logs(logger) as logs: with create_temp_module(self.source_lines): self.assertEqual(logs.getvalue(), '')
def test_jit_module_jit_options(self): jit_options = {"nopython": True, "nogil": False, "error_model": "numpy", "boundscheck": False, } with create_temp_module(self.source_lines, **jit_options) as test_module: self.assertEqual(test_module.inc.targetoptions, jit_options)
def test_create_temp_jitted_module_with_exception(self): try: sys_path_original = list(sys.path) sys_modules_original = dict(sys.modules) with create_temp_module(self.source_lines): raise ValueError("Something went wrong!") except ValueError: # Test that modifications to sys.path / sys.modules are reverted self.assertEqual(sys.path, sys_path_original) self.assertEqual(sys.modules, sys_modules_original)
def test_create_temp_jitted_module(self): sys_path_original = list(sys.path) sys_modules_original = dict(sys.modules) with create_temp_module(self.source_lines) as test_module: temp_module_dir = os.path.dirname(test_module.__file__) self.assertEqual(temp_module_dir, sys.path[0]) self.assertEqual(sys.path[1:], sys_path_original) self.assertTrue(test_module.__name__ in sys.modules) # Test that modifications to sys.path / sys.modules are reverted self.assertEqual(sys.path, sys_path_original) self.assertEqual(sys.modules, sys_modules_original)
def test_external_type(self): with create_temp_module(self.source_lines) as test_module: Foo, FooType = self.make_foo_type(test_module.FooType) # sum of foo class instance and array return an array # binary operation with foo class instance as one of args @overload(operator.add) def overload_foo_add(lhs, rhs): if isinstance(lhs, FooType) and isinstance(rhs, types.Array): def imp(lhs, rhs): return np.array([lhs.value, rhs[0]]) return imp # sum of 2 foo class instances return an array # binary operation with 2 foo class instances as args @overload(operator.add) def overload_foo_add(lhs, rhs): if isinstance(lhs, FooType) and isinstance(rhs, FooType): def imp(lhs, rhs): return np.array([lhs.value, rhs.value]) return imp # neg of foo class instance return an array # unary operation with foo class instance arg @overload(operator.neg) def overload_foo_neg(x): if isinstance(x, FooType): def imp(x): return np.array([-x.value]) return imp @njit def arr_expr_sum1(x, y): return Foo(x) + np.array([y]) @njit def arr_expr_sum2(x, y): return Foo(x) + Foo(y) @njit def arr_expr_neg(x): return -Foo(x) np.testing.assert_array_equal(arr_expr_sum1(0, 1), np.array([0, 1])) np.testing.assert_array_equal(arr_expr_sum2(2, 3), np.array([2, 3])) np.testing.assert_array_equal(arr_expr_neg(4), np.array([-4]))
def test_jit_module_logging_output(self): logger = logging.getLogger('numba.core.decorators') logger.setLevel(logging.DEBUG) jit_options = {"nopython": True, "error_model": "numpy", } with captured_logs(logger) as logs: with create_temp_module(self.source_lines, **jit_options) as test_module: logs = logs.getvalue() expected = ["Auto decorating function", "from module {}".format(test_module.__name__), "with jit and options: {}".format(jit_options)] self.assertTrue(all(i in logs for i in expected))
def test_jit_module(self): with create_temp_module(self.source_lines) as test_module: self.assertIsInstance(test_module.inc, dispatcher.Dispatcher) self.assertIsInstance(test_module.add, dispatcher.Dispatcher) self.assertIsInstance(test_module.inc_add, dispatcher.Dispatcher) self.assertTrue(test_module.mean is np.mean) self.assertTrue(inspect.isclass(test_module.Foo)) # Test output of jitted functions is as expected x, y = 1.7, 2.3 self.assertEqual(test_module.inc(x), test_module.inc.py_func(x)) self.assertEqual(test_module.add(x, y), test_module.add.py_func(x, y)) self.assertEqual(test_module.inc_add(x), test_module.inc_add.py_func(x))
def test_mixin_against_real_example(self): # See issue #4970, this checks that unicode eq/ne now ignores extension # types. with create_temp_module(self.source_lines) as test_module: FooType = test_module.FooType self.assertFalse(FooType().is_internal) # set up an extension type class Foo(object): pass register_model(FooType)(models.OpaqueModel) @typeof_impl.register(Foo) def _typ_foo(val, c): return FooType() @unbox(FooType) def unbox_foo(typ, obj, c): return NativeValue(c.context.get_dummy_value()) @overload(operator.eq) def foo_eq(a, b): if a == FooType(): return lambda a, b: "RAN CUSTOM EQ OVERLOAD" @overload(operator.ne) def foo_ne(a, b): if a == FooType(): return lambda a, b: "RAN CUSTOM NE OVERLOAD" @njit def f(a): return a == "A", a != "A" self.assertEqual(("RAN CUSTOM EQ OVERLOAD", "RAN CUSTOM NE OVERLOAD"), f(Foo()))
def test_jit_module_jit_options_override(self): source_lines = """ from numba import jit, jit_module @jit(nogil=True, forceobj=True) def inc(x): return x + 1 def add(x, y): return x + y jit_module({jit_options}) """ jit_options = {"nopython": True, "error_model": "numpy", "boundscheck": False, } with create_temp_module(source_lines=source_lines, **jit_options) as test_module: self.assertEqual(test_module.add.targetoptions, jit_options) # Test that manual jit-wrapping overrides jit_module options self.assertEqual(test_module.inc.targetoptions, {'nogil': True, 'forceobj': True, 'boundscheck': None})
def test_externally_defined_type_is_external(self): with create_temp_module(self.source_lines) as test_module: FooType = test_module.FooType self.assertFalse(FooType().is_internal) # set up an extension type class Foo(object): pass register_model(FooType)(models.OpaqueModel) @typeof_impl.register(Foo) def _typ_foo(val, c): return FooType() @unbox(FooType) def unbox_foo(typ, obj, c): return NativeValue(c.context.get_dummy_value()) # function to overload def false_if_not_array(a): pass # Set up an overload which will accept all types irrespective of # whether they are from Numba's closed type system @overload(false_if_not_array) def ol_false_if_not_array(a): if isinstance(a, types.Array): return lambda a: True else: return lambda a: False @njit def call_false_if_not_array(a): return false_if_not_array(a) self.assertTrue(call_false_if_not_array(np.zeros(10))) self.assertFalse(call_false_if_not_array(10)) # The extension type was accepted self.assertFalse(call_false_if_not_array(Foo())) # Now do the same sort of overload but put in a guard based on the # use of internal types def false_if_not_array_closed_system(a): pass @overload(false_if_not_array_closed_system) def ol_false_if_not_array_closed_system(a): if a.is_internal: # guard if isinstance(a, types.Array): return lambda a: True else: return lambda a: False @njit def call_false_if_not_array_closed_system(a): return false_if_not_array_closed_system(a) self.assertTrue(call_false_if_not_array_closed_system( np.zeros(10))) self.assertFalse(call_false_if_not_array_closed_system(10)) with self.assertRaises(errors.TypingError) as raises: call_false_if_not_array_closed_system(Foo()) estr = str(raises.exception) self.assertIn(_header_lead, estr) self.assertIn("false_if_not_array_closed_system", estr) self.assertIn("(Foo)", estr)