def test_arrayscalar_const(self): pyfunc = use_arrayscalar_const cres = compile_isolated(pyfunc, ()) cfunc = cres.entry_point self.assertEqual(pyfunc(), cfunc())
def check_min_max_unary_non_iterable(self, pyfunc, flags=enable_pyobj_flags): cr = compile_isolated(pyfunc, (types.int32, ), flags=flags) cfunc = cr.entry_point cfunc(1)
def run_nullary_func(self, pyfunc, flags): cr = compile_isolated(pyfunc, (), flags=flags) cfunc = cr.entry_point expected = pyfunc() self.assertPreciseEqual(cfunc(), expected)
def test_mod_complex(self, flags=force_pyobj_flags): pyfunc = self.op.mod_usecase with self.assertTypingError(): cres = compile_isolated(pyfunc, (types.complex64, types.complex64))
def test_locals(self, flags=enable_pyobj_flags): pyfunc = locals_usecase with self.assertRaises(errors.ForbiddenConstruct): cr = compile_isolated(pyfunc, (types.int64, ), flags=flags)
def compile_func(arr): cr = compile_isolated(pyfunc, (typeof(arr), types.intp, types.intp), flags=flags) return cr.entry_point
def get_cfunc(self, pyfunc): rectype = numpy_support.from_dtype(recordwithcharseq) cres = compile_isolated(pyfunc, (rectype[:], types.intp)) return cres.entry_point
def test_unituple(self): tuple_type = types.UniTuple(types.int32, 2) cr_first = compile_isolated(tuple_first, (tuple_type,)) cr_second = compile_isolated(tuple_second, (tuple_type,)) self.assertPreciseEqual(cr_first.entry_point((4, 5)), 4) self.assertPreciseEqual(cr_second.entry_point((4, 5)), 5)
def test_hetero_tuple(self): tuple_type = types.Tuple((types.int64, types.float32)) cr_first = compile_isolated(tuple_first, (tuple_type,)) cr_second = compile_isolated(tuple_second, (tuple_type,)) self.assertPreciseEqual(cr_first.entry_point((2**61, 1.5)), 2**61) self.assertPreciseEqual(cr_second.entry_point((2**61, 1.5)), 1.5)
def generic_run(pyfunc, arr, shape): cres = compile_isolated(pyfunc, (typeof(arr), typeof(shape))) return cres.entry_point(arr, shape)
def test_bad_float_index_npm(self): with self.assertTypingError() as raises: compile_isolated(bad_float_index, (types.Array(types.float64, 2, "C"), )) self.assertIn("unsupported array index type float64", str(raises.exception))
def check_array_const(self, pyfunc): cres = compile_isolated(pyfunc, (types.int32,)) cfunc = cres.entry_point for i in [0, 1, 2]: np.testing.assert_array_equal(pyfunc(i), cfunc(i))
def test_constant_bytes(self): pyfunc = bytes_as_const_array cres = compile_isolated(pyfunc, ()) cfunc = cres.entry_point np.testing.assert_array_equal(pyfunc(), cfunc())
def test_write_to_global_array(self): pyfunc = write_to_global_array with self.assertRaises(TypingError): compile_isolated(pyfunc, ())
def compile_simple_nopython(self): with captured_stdout() as out: cres = compile_isolated(simple_nopython, (types.int64, )) # Sanity check compiled function self.assertPreciseEqual(cres.entry_point(2), 3) return out.getvalue()
def check_conversion(self, fromty, toty, val): pyfunc = identity cr = compile_isolated(pyfunc, (fromty,), toty) cfunc = cr.entry_point res = cfunc(val) self.assertEqual(res, val)
def compile_simple_gen(self): with captured_stdout() as out: cres = compile_isolated(simple_gen, (types.int64, types.int64)) # Sanity check compiled function self.assertPreciseEqual(list(cres.entry_point(2, 5)), [2, 5]) return out.getvalue()
def check_array_iter_items(self, arr): pyfunc = array_iter_items cres = compile_isolated(pyfunc, [typeof(arr)]) cfunc = cres.entry_point expected = pyfunc(arr) self.assertPreciseEqual(cfunc(arr), expected)
def get_cfunc(self, pyfunc, argspec): cres = compile_isolated(pyfunc, argspec) return cres.entry_point
def check_array_view_iter(self, arr, index): pyfunc = array_view_iter cres = compile_isolated(pyfunc, [typeof(arr), typeof(index)]) cfunc = cres.entry_point expected = pyfunc(arr, index) self.assertPreciseEqual(cfunc(arr, index), expected)
def test_is_ellipsis(self): cres = compile_isolated(self.op.is_usecase, (types.ellipsis, types.ellipsis)) cfunc = cres.entry_point self.assertTrue(cfunc(Ellipsis, Ellipsis))
def check_array_unary(self, arr, arrty, func): cres = compile_isolated(func, [arrty]) cfunc = cres.entry_point self.assertPreciseEqual(cfunc(arr), func(arr))
def test_globals(self, flags=enable_pyobj_flags): pyfunc = globals_usecase cr = compile_isolated(pyfunc, (), flags=flags) cfunc = cr.entry_point g = cfunc() self.assertIs(g, globals())
def test_np_ndindex_empty(self): func = np_ndindex_empty cres = compile_isolated(func, []) cfunc = cres.entry_point self.assertPreciseEqual(cfunc(), func())
def check_min_max_invalid_types(self, pyfunc, flags=enable_pyobj_flags): cr = compile_isolated(pyfunc, (types.int32, types.Dummy('list')), flags=flags) cfunc = cr.entry_point cfunc(1, [1])
def get_cfunc(self, pyfunc): cres = compile_isolated(pyfunc, (self.nbrecord, )) return cres.entry_point
def check_min_max_empty_tuple(self, pyfunc, func_name): with self.assertTypingError() as raises: compile_isolated(pyfunc, (), flags=no_pyobj_flags) self.assertIn("%s() argument is an empty tuple" % func_name, str(raises.exception))
def assert_prune(self, func, args_tys, prune, *args, **kwargs): # This checks that the expected pruned branches have indeed been pruned. # func is a python function to assess # args_tys is the numba types arguments tuple # prune arg is a list, one entry per branch. The value in the entry is # encoded as follows: # True: using constant inference only, the True branch will be pruned # False: using constant inference only, the False branch will be pruned # None: under no circumstances should this branch be pruned # *args: the argument instances to pass to the function to check # execution is still valid post transform # **kwargs: # - flags: compiler.Flags instance to pass to `compile_isolated`, # permits use of e.g. object mode func_ir = compile_to_ir(func) before = func_ir.copy() if self._DEBUG: print("=" * 80) print("before prune") func_ir.dump() rewrite_semantic_constants(func_ir, args_tys) dead_branch_prune(func_ir, args_tys) after = func_ir if self._DEBUG: print("after prune") func_ir.dump() before_branches = self.find_branches(before) self.assertEqual(len(before_branches), len(prune)) # what is expected to be pruned expect_removed = [] for idx, prune in enumerate(prune): branch = before_branches[idx] if prune is True: expect_removed.append(branch.truebr) elif prune is False: expect_removed.append(branch.falsebr) elif prune is None: pass # nothing should be removed! elif prune == 'both': expect_removed.append(branch.falsebr) expect_removed.append(branch.truebr) else: assert 0, "unreachable" # compare labels original_labels = set([_ for _ in before.blocks.keys()]) new_labels = set([_ for _ in after.blocks.keys()]) # assert that the new labels are precisely the original less the # expected pruned labels try: self.assertEqual(new_labels, original_labels - set(expect_removed)) except AssertionError as e: print("new_labels", sorted(new_labels)) print("original_labels", sorted(original_labels)) print("expect_removed", sorted(expect_removed)) raise e supplied_flags = kwargs.pop('flags', False) compiler_kws = {'flags': supplied_flags} if supplied_flags else {} cres = compile_isolated(func, args_tys, **compiler_kws) if args is None: res = cres.entry_point() expected = func() else: res = cres.entry_point(*args) expected = func(*args) self.assertEqual(res, expected)
def test_enumerate_start_invalid_start_type_npm(self): pyfunc = enumerate_invalid_start_usecase with self.assertRaises(errors.TypingError) as raises: cr = compile_isolated(pyfunc, (), flags=no_pyobj_flags) msg = "Only integers supported as start value in enumerate" self.assertIn(msg, str(raises.exception))
def test_cast_mydummy(self): pyfunc = get_dummy cr = compile_isolated(pyfunc, (), types.float64) self.assertPreciseEqual(cr.entry_point(), 42.0)