def test_1d_slicing3(self, flags=enable_pyobj_flags): pyfunc = slicing_1d_usecase3 arraytype = types.Array(types.int32, 1, "C") argtys = (arraytype, types.int32, types.int32) cr = compile_isolated(pyfunc, argtys, flags=flags) cfunc = cr.entry_point a = np.arange(10, dtype="i4") args = [(3, 10), (2, 3), (10, 0), (0, 10), (5, 10)] for arg in args: self.assertEqual(pyfunc(a, *arg), cfunc(a, *arg)) # Any arraytype = types.Array(types.int32, 1, "A") argtys = (arraytype, types.int32, types.int32) cr = compile_isolated(pyfunc, argtys, flags=flags) cfunc = cr.entry_point a = np.arange(20, dtype="i4")[::2] self.assertFalse(a.flags["C_CONTIGUOUS"]) self.assertFalse(a.flags["F_CONTIGUOUS"]) for arg in args: self.assertEqual(pyfunc(a, *arg), cfunc(a, *arg))
def _test_compare(self, pyfunc): def eq(pyfunc, cfunc, args): self.assertIs(cfunc(*args), pyfunc(*args), "mismatch for arguments %s" % (args,)) # Same-sized tuples argtypes = [types.Tuple((types.int64, types.float32)), types.UniTuple(types.int32, 2)] for ta, tb in itertools.product(argtypes, argtypes): cr = compile_isolated(pyfunc, (ta, tb)) cfunc = cr.entry_point for args in [((4, 5), (4, 5)), ((4, 5), (4, 6)), ((4, 6), (4, 5)), ((4, 5), (5, 4))]: eq(pyfunc, cfunc, args) # Different-sized tuples argtypes = [types.Tuple((types.int64, types.float32)), types.UniTuple(types.int32, 3)] cr = compile_isolated(pyfunc, tuple(argtypes)) cfunc = cr.entry_point for args in [((4, 5), (4, 5, 6)), ((4, 5), (4, 4, 6)), ((4, 5), (4, 6, 7))]: eq(pyfunc, cfunc, args)
def test_2d_slicing3(self, flags=enable_pyobj_flags): """ arr_2d[a:b:c, d] """ # C layout pyfunc = slicing_2d_usecase3 arraytype = types.Array(types.int32, 2, "C") argtys = (arraytype, types.int32, types.int32, types.int32, types.int32) cr = compile_isolated(pyfunc, argtys, flags=flags) cfunc = cr.entry_point a = np.arange(100, dtype="i4").reshape(10, 10) args = [(0, 10, 1, 0), (2, 3, 1, 1), (10, 0, -1, 8), (9, 0, -2, 4), (0, 10, 2, 3), (0, -1, 3, 1)] for arg in args: expected = pyfunc(a, *arg) self.assertPreciseEqual(cfunc(a, *arg), expected) # Any layout arraytype = types.Array(types.int32, 2, "A") argtys = (arraytype, types.int32, types.int32, types.int32, types.int32) cr = compile_isolated(pyfunc, argtys, flags=flags) cfunc = cr.entry_point a = np.arange(400, dtype="i4").reshape(20, 20)[::2, ::2] for arg in args: expected = pyfunc(a, *arg) self.assertPreciseEqual(cfunc(a, *arg), expected)
def test_index(self): pyfunc = tuple_index cr = compile_isolated(pyfunc, [types.UniTuple(types.int64, 3), types.int64]) tup = (4, 3, 6) for i in range(len(tup)): self.assertPreciseEqual(cr.entry_point(tup, i), tup[i]) # Test empty tuple cr = compile_isolated(pyfunc, [types.UniTuple(types.int64, 0), types.int64]) with self.assertRaises(IndexError) as raises: cr.entry_point((), 0) self.assertEqual("tuple index out of range", str(raises.exception)) # With a compile-time static index (the code generation path is different) pyfunc = tuple_index_static for typ in (types.UniTuple(types.int64, 4), types.Tuple((types.int64, types.int32, types.int64, types.int32))): cr = compile_isolated(pyfunc, (typ,)) tup = (4, 3, 42, 6) self.assertPreciseEqual(cr.entry_point(tup), pyfunc(tup)) typ = types.UniTuple(types.int64, 1) with self.assertTypingError(): cr = compile_isolated(pyfunc, (typ,))
def test_try_except_raises(self): pyfunc = try_except_usecase for f in [no_pyobj_flags, enable_pyobj_flags]: with self.assertRaises(errors.UnsupportedError) as e: compile_isolated(pyfunc, (), flags=f) msg = "Use of unsupported opcode (SETUP_EXCEPT) found" self.assertIn(msg, str(e.exception))
def test_unknown_attrs(self): try: compile_isolated(bar, (types.int32,)) except TypingError as e: self.assertIn("Unknown attribute 'a' of type int32", str(e)) else: self.fail("Should raise error")
def test_1d_integer_indexing(self, flags=enable_pyobj_flags): # C layout pyfunc = integer_indexing_1d_usecase arraytype = types.Array(types.int32, 1, 'C') cr = compile_isolated(pyfunc, (arraytype, types.int32), flags=flags) cfunc = cr.entry_point a = np.arange(10, dtype='i4') self.assertEqual(pyfunc(a, 0), cfunc(a, 0)) self.assertEqual(pyfunc(a, 9), cfunc(a, 9)) self.assertEqual(pyfunc(a, -1), cfunc(a, -1)) # Any layout arraytype = types.Array(types.int32, 1, 'A') cr = compile_isolated(pyfunc, (arraytype, types.int32), flags=flags) cfunc = cr.entry_point a = np.arange(10, dtype='i4')[::2] self.assertFalse(a.flags['C_CONTIGUOUS']) self.assertFalse(a.flags['F_CONTIGUOUS']) self.assertEqual(pyfunc(a, 0), cfunc(a, 0)) self.assertEqual(pyfunc(a, 2), cfunc(a, 2)) self.assertEqual(pyfunc(a, -1), cfunc(a, -1)) # Using a 0-d array as integer index arraytype = types.Array(types.int32, 1, 'C') indextype = types.Array(types.int16, 0, 'C') cr = compile_isolated(pyfunc, (arraytype, indextype), flags=flags) cfunc = cr.entry_point a = np.arange(3, 13, dtype=np.int32) for i in (0, 9, -2): idx = np.array(i).astype(np.int16) assert idx.ndim == 0 self.assertEqual(pyfunc(a, idx), cfunc(a, idx))
def test_array_expr(self): flags = Flags() flags.set("enable_pyobject") global cnd_array_jitted scalty = types.float64 arrty = types.Array(scalty, 1, 'C') cr1 = compile_isolated(cnd_array, args=(arrty,), flags=flags) cnd_array_jitted = cr1.entry_point cr2 = compile_isolated(blackscholes_arrayexpr_jitted, args=(arrty, arrty, arrty, scalty, scalty), flags=flags) jitted_bs = cr2.entry_point OPT_N = 400 iterations = 10 stockPrice = randfloat(self.random.random_sample(OPT_N), 5.0, 30.0) optionStrike = randfloat(self.random.random_sample(OPT_N), 1.0, 100.0) optionYears = randfloat(self.random.random_sample(OPT_N), 0.25, 10.0) args = stockPrice, optionStrike, optionYears, RISKFREE, VOLATILITY callResultGold, putResultGold = blackscholes_arrayexpr(*args) callResultNumba, putResultNumba = jitted_bs(*args) delta = np.abs(callResultGold - callResultNumba) L1norm = delta.sum() / np.abs(callResultGold).sum() print("L1 norm: %E" % L1norm) print("Max absolute error: %E" % delta.max()) self.assertEqual(delta.max(), 0)
def check_argument_cleanup(self, typ, obj): """ Check that argument cleanup doesn't leak references. """ def f(x, y): pass # The exception raised when passing a negative number # to PyLong_AsUnsignedLongLong exc_type = OverflowError if sys.version_info >= (2, 7) else TypeError def _refcounts(obj): refs = [sys.getrefcount(obj)] if isinstance(obj, tuple): refs += [_refcounts(v) for v in obj] return refs cres = compile_isolated(f, (typ, types.uint32)) old_refcnt = _refcounts(obj) cres.entry_point(obj, 1) self.assertEqual(_refcounts(obj), old_refcnt) with self.assertRaises(exc_type): cres.entry_point(obj, -1) self.assertEqual(_refcounts(obj), old_refcnt) cres = compile_isolated(f, (types.uint32, typ)) old_refcnt = _refcounts(obj) cres.entry_point(1, obj) self.assertEqual(_refcounts(obj), old_refcnt) with self.assertRaises(exc_type): cres.entry_point(-1, obj) self.assertEqual(_refcounts(obj), old_refcnt)
def test_1d_slicing5(self, flags=enable_pyobj_flags): pyfunc = slicing_1d_usecase5 arraytype = types.Array(types.int32, 1, 'C') argtys = (arraytype, types.int32) cr = compile_isolated(pyfunc, argtys, flags=flags) cfunc = cr.entry_point a = np.arange(10, dtype='i4') args = [3, 2, 10, 0, 5] for arg in args: self.assertEqual(pyfunc(a, arg), cfunc(a, arg)) # Any arraytype = types.Array(types.int32, 1, 'A') argtys = (arraytype, types.int32) cr = compile_isolated(pyfunc, argtys, flags=flags) cfunc = cr.entry_point a = np.arange(20, dtype='i4')[::2] self.assertFalse(a.flags['C_CONTIGUOUS']) self.assertFalse(a.flags['F_CONTIGUOUS']) for arg in args: self.assertEqual(pyfunc(a, arg), cfunc(a, arg))
def check_round_scalar(self, unary_pyfunc, binary_pyfunc): base_values = [-3.0, -2.5, -2.25, -1.5, 1.5, 2.25, 2.5, 2.75] complex_values = [x * (1 - 1j) for x in base_values] int_values = [int(x) for x in base_values] argtypes = (types.float64, types.float32, types.int32, types.complex64, types.complex128) argvalues = [base_values, base_values, int_values, complex_values, complex_values] pyfunc = binary_pyfunc for ty, values in zip(argtypes, argvalues): cres = compile_isolated(pyfunc, (ty, types.int32)) cfunc = cres.entry_point for decimals in (1, 0, -1): for v in values: if decimals > 0: v *= 10 expected = _fixed_np_round(v, decimals) got = cfunc(v, decimals) self.assertPreciseEqual(got, expected) pyfunc = unary_pyfunc for ty, values in zip(argtypes, argvalues): cres = compile_isolated(pyfunc, (ty,)) cfunc = cres.entry_point for v in values: expected = _fixed_np_round(v) got = cfunc(v) self.assertPreciseEqual(got, expected)
def test_2d_slicing(self, flags=enable_pyobj_flags): pyfunc = slicing_1d_usecase arraytype = types.Array(types.int32, 2, 'C') argtys = (arraytype, types.int32, types.int32, types.int32) cr = compile_isolated(pyfunc, argtys, flags=flags) cfunc = cr.entry_point a = np.arange(100, dtype='i4').reshape(10, 10) self.assertTrue((pyfunc(a, 0, 10, 1) == cfunc(a, 0, 10, 1)).all()) self.assertTrue((pyfunc(a, 2, 3, 1) == cfunc(a, 2, 3, 1)).all()) self.assertTrue((pyfunc(a, 10, 0, 1) == cfunc(a, 10, 0, 1)).all()) self.assertTrue((pyfunc(a, 0, 10, -1) == cfunc(a, 0, 10, -1)).all()) self.assertTrue((pyfunc(a, 0, 10, 2) == cfunc(a, 0, 10, 2)).all()) pyfunc = slicing_2d_usecase arraytype = types.Array(types.int32, 2, 'C') argtys = (arraytype, types.int32, types.int32, types.int32, types.int32, types.int32, types.int32) cr = compile_isolated(pyfunc, argtys, flags=flags) cfunc = cr.entry_point self.assertTrue((pyfunc(a, 0, 10, 1, 0, 10, 1) == cfunc(a, 0, 10, 1, 0, 10, 1)).all()) self.assertTrue((pyfunc(a, 2, 3, 1, 2, 3, 1) == cfunc(a, 2, 3, 1, 2, 3, 1)).all()) self.assertTrue((pyfunc(a, 10, 0, 1, 10, 0, 1) == cfunc(a, 10, 0, 1, 10, 0, 1)).all()) self.assertTrue((pyfunc(a, 0, 10, -1, 0, 10, -1) == cfunc(a, 0, 10, -1, 0, 10, -1)).all()) self.assertTrue((pyfunc(a, 0, 10, 2, 0, 10, 2) == cfunc(a, 0, 10, 2, 0, 10, 2)).all())
def test_2d_slicing3(self, flags=enable_pyobj_flags): # C layout pyfunc = slicing_2d_usecase3 arraytype = types.Array(types.int32, 2, 'C') argtys = (arraytype, types.int32, types.int32, types.int32, types.int32) cr = compile_isolated(pyfunc, argtys, flags=flags) cfunc = cr.entry_point a = np.arange(100, dtype='i4').reshape(10, 10) args = [ (0, 10, 1, 0), (2, 3, 1, 2), (10, 0, 1, 9), (0, 10, -1, 0), (0, 10, 2, 4), ] for arg in args: self.assertEqual(pyfunc(a, *arg), cfunc(a, *arg)) # Any layout arraytype = types.Array(types.int32, 2, 'A') argtys = (arraytype, types.int32, types.int32, types.int32, types.int32) cr = compile_isolated(pyfunc, argtys, flags=flags) cfunc = cr.entry_point a = np.arange(400, dtype='i4').reshape(20, 20)[::2, ::2] for arg in args: self.assertEqual(pyfunc(a, *arg), cfunc(a, *arg))
def test_3d_slicing2(self, flags=enable_pyobj_flags): # C layout pyfunc = slicing_3d_usecase2 arraytype = types.Array(types.int32, 3, 'C') argtys = (arraytype, types.int32, types.int32, types.int32) cr = compile_isolated(pyfunc, argtys, flags=flags) cfunc = cr.entry_point a = np.arange(1000, dtype='i4').reshape(10, 10, 10) args = [ (0, 9, 1), (2, 3, 1), (9, 0, 1), (0, 9, -1), (0, 9, 2), ] for arg in args: self.assertEqual(pyfunc(a, *arg), cfunc(a, *arg)) # Any layout arraytype = types.Array(types.int32, 3, 'A') argtys = (arraytype, types.int32, types.int32, types.int32) cr = compile_isolated(pyfunc, argtys, flags=flags) cfunc = cr.entry_point a = np.arange(2000, dtype='i4')[::2].reshape(10, 10, 10) for arg in args: self.assertEqual(pyfunc(a, *arg), cfunc(a, *arg))
def test_unknown_attrs(self): try: compile_isolated(bar, (types.int32,)) except TypingError as e: self.assertTrue(e.msg.startswith("Unknown attribute"), e.msg) else: self.fail("Should raise error")
def test_func1_isolated(self): pyfunc = call_func1_nullary cr = compile_isolated(pyfunc, ()) self.assertPreciseEqual(cr.entry_point(), 42) pyfunc = call_func1_unary cr = compile_isolated(pyfunc, (types.float64,)) self.assertPreciseEqual(cr.entry_point(18.0), 6.0)
def test_unknown_function(self): try: compile_isolated(foo, ()) except TypingError as e: self.assertTrue(e.msg.startswith("Untyped global name"), e.msg) else: self.fail("Should raise error")
def test_random_randrange(self): for tp, max_width in [(types.int64, 2**63), (types.int32, 2**31)]: cr1 = compile_isolated(random_randrange1, (tp,)) cr2 = compile_isolated(random_randrange2, (tp, tp)) cr3 = compile_isolated(random_randrange3, (tp, tp, tp)) self._check_randrange(cr1.entry_point, cr2.entry_point, cr3.entry_point, py_state_ptr, max_width)
def test_unknown_function(self): try: compile_isolated(foo, ()) except TypingError as e: self.assertIn("Untyped global name 'what'", str(e)) else: self.fail("Should raise error")
def test_complex(self): pyfunc = docomplex x_types = [ types.int32, types.int64, types.float32, types.float64, types.complex64, types.complex128, ] x_values = [1, 1000, 12.2, 23.4, 1.5-5j, 1-4.75j] for ty, x in zip(x_types, x_values): cres = compile_isolated(pyfunc, [ty]) cfunc = cres.entry_point got = cfunc(x) expected = pyfunc(x) self.assertPreciseEqual(pyfunc(x), cfunc(x), prec='single' if ty is types.float32 else 'exact') # Check that complex(float32) really creates a complex64, # by checking the accuracy of computations. pyfunc = complex_calc x = 1.0 + 2**-50 cres = compile_isolated(pyfunc, [types.float32]) cfunc = cres.entry_point self.assertPreciseEqual(cfunc(x), 1.0) # Control (complex128) cres = compile_isolated(pyfunc, [types.float64]) cfunc = cres.entry_point self.assertGreater(cfunc(x), 1.0)
def check_argument_cleanup(self, typ, obj): """ Check that argument cleanup doesn't leak references. """ def f(x, y): pass def _objects(obj): objs = [obj] if isinstance(obj, tuple): for v in obj: objs += _objects(v) return objs objects = _objects(obj) cres = compile_isolated(f, (typ, types.uint32)) with self.assertRefCount(*objects): cres.entry_point(obj, 1) with self.assertRefCount(*objects): with self.assertRaises(OverflowError): cres.entry_point(obj, -1) cres = compile_isolated(f, (types.uint32, typ)) with self.assertRefCount(*objects): cres.entry_point(1, obj) with self.assertRefCount(*objects): with self.assertRaises(OverflowError): cres.entry_point(-1, obj)
def test_complex2(self): pyfunc = docomplex2 x_types = [ types.int32, types.int64, types.float32, types.float64 ] x_values = [1, 1000, 12.2, 23.4] y_values = [x - 3 for x in x_values] for ty, x, y in zip(x_types, x_values, y_values): cres = compile_isolated(pyfunc, [ty, ty]) cfunc = cres.entry_point self.assertPreciseEqual(pyfunc(x, y), cfunc(x, y), prec='single' if ty is types.float32 else 'exact') # Check that complex(float32, float32) really creates a complex64, # by checking the accuracy of computations. pyfunc = complex_calc2 x = 1.0 + 2**-50 cres = compile_isolated(pyfunc, [types.float32, types.float32]) cfunc = cres.entry_point self.assertPreciseEqual(cfunc(x, x), 2.0) # Control (complex128) cres = compile_isolated(pyfunc, [types.float64, types.float32]) cfunc = cres.entry_point self.assertGreater(cfunc(x, x), 2.0)
def check_argument_cleanup(self, typ, obj): """ Check that argument cleanup doesn't leak references. """ def f(x, y): pass # The exception raised when passing a negative number # to PyLong_AsUnsignedLongLong exc_type = OverflowError if sys.version_info >= (2, 7) else TypeError def _objects(obj): objs = [obj] if isinstance(obj, tuple): for v in obj: objs += _objects(v) return objs objects = _objects(obj) cres = compile_isolated(f, (typ, types.uint32)) with self.assertRefCount(*objects): cres.entry_point(obj, 1) with self.assertRefCount(*objects): with self.assertRaises(exc_type): cres.entry_point(obj, -1) cres = compile_isolated(f, (types.uint32, typ)) with self.assertRefCount(*objects): cres.entry_point(1, obj) with self.assertRefCount(*objects): with self.assertRaises(exc_type): cres.entry_point(-1, obj)
def test_2d_integer_indexing(self, flags=enable_pyobj_flags, pyfunc=integer_indexing_2d_usecase): # C layout a = np.arange(100, dtype='i4').reshape(10, 10) arraytype = types.Array(types.int32, 2, 'C') cr = compile_isolated(pyfunc, (arraytype, types.int32, types.int32), flags=flags) cfunc = cr.entry_point self.assertEqual(pyfunc(a, 0, 0), cfunc(a, 0, 0)) self.assertEqual(pyfunc(a, 9, 9), cfunc(a, 9, 9)) self.assertEqual(pyfunc(a, -1, -1), cfunc(a, -1, -1)) # Any layout a = np.arange(100, dtype='i4').reshape(10, 10)[::2, ::2] self.assertFalse(a.flags['C_CONTIGUOUS']) self.assertFalse(a.flags['F_CONTIGUOUS']) arraytype = types.Array(types.int32, 2, 'A') cr = compile_isolated(pyfunc, (arraytype, types.int32, types.int32), flags=flags) cfunc = cr.entry_point self.assertEqual(pyfunc(a, 0, 0), cfunc(a, 0, 0)) self.assertEqual(pyfunc(a, 2, 2), cfunc(a, 2, 2)) self.assertEqual(pyfunc(a, -1, -1), cfunc(a, -1, -1))
def test_optional_expansion_type_unification_error(self): pyfunc = gen_optional_and_type_unification_error with self.assertTypingError() as e: compile_isolated(pyfunc, (), flags=no_pyobj_flags) msg = ("Can't unify yield type from the following types: complex128, " "int%s, none") self.assertIn(msg % types.intp.bitwidth, str(e.exception))
def test_type_unification_error(self): pyfunc = gen_unification_error with self.assertTypingError() as e: compile_isolated(pyfunc, (), flags=no_pyobj_flags) msg = ("Can't unify yield type from the following types: complex128, " "none") self.assertIn(msg, str(e.exception))
def test_bad_hypot_usage(self): with self.assertRaises(TypingError) as raises: compile_isolated(bad_hypot_usage, ()) errmsg = str(raises.exception) # Make sure it listed the known signatures. # This is sensitive to the formatting of the error message. self.assertIn(" * (float64, float64) -> float64", errmsg)
def compile(self, func, *args, **kwargs): assert not kwargs sig = tuple([numba.typeof(x) for x in args]) std = compile_isolated(func, sig, flags=self.flags) fast = compile_isolated(func, sig, flags=self.fastflags) return std, fast
def test_bad_index_npm(self): with self.assertTypingError() as raises: arraytype1 = from_dtype(np.dtype([('x', np.int32), ('y', np.int32)])) arraytype2 = types.Array(types.int32, 2, 'C') compile_isolated(bad_index, (arraytype1, arraytype2), flags=no_pyobj_flags) self.assertIn('unsupported array index type', str(raises.exception))
def test_len(self): pyfunc = len_usecase cr = compile_isolated(pyfunc, [types.Tuple((types.int64, types.float32))]) self.assertPreciseEqual(cr.entry_point((4, 5)), 2) cr = compile_isolated(pyfunc, [types.UniTuple(types.int64, 3)]) self.assertPreciseEqual(cr.entry_point((4, 5, 6)), 3)
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 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 test_numpy_randint(self): for tp, max_width in [(types.int64, 2**63), (types.int32, 2**31)]: cr1 = compile_isolated(numpy_randint1, (tp, )) cr2 = compile_isolated(numpy_randint2, (tp, tp)) self._check_randrange(cr1.entry_point, cr2.entry_point, None, np_state_ptr, max_width)
def check(a, b, expected): cres = compile_isolated(pyfunc, (typeof(a), typeof(b))) self.assertPreciseEqual(cres.entry_point(a, b), (expected, not expected))
def test_cast_mydummy(self): pyfunc = get_dummy cr = compile_isolated(pyfunc, (), types.float64) self.assertPreciseEqual(cr.entry_point(), 42.0)
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_loop2_int16(self): pyfunc = loop2 cres = compile_isolated(pyfunc, [types.int16, types.int16]) cfunc = cres.entry_point self.assertTrue(cfunc(1, 6), pyfunc(1, 6))
def _compile_this(self, func, sig, flags): return compile_isolated(func, sig, flags=flags)
def get_cfunc(self, pyfunc, argspec): cres = compile_isolated(pyfunc, argspec) return cres.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_stdcall(self): # Just check that it doesn't crash cres = compile_isolated(use_c_sleep, [types.uintc]) cfunc = cres.entry_point cfunc(1)
def test_ctype_wrapping(self): pyfunc = use_ctype_wrapping cres = compile_isolated(pyfunc, [types.double]) cfunc = cres.entry_point x = 3.14 self.assertEqual(pyfunc(x), cfunc(x))
def test_untyped_function(self): with self.assertRaises(TypeError) as raises: compile_isolated(use_c_untyped, [types.double]) self.assertIn("ctypes function 'exp' doesn't define its argument types", str(raises.exception))
def test_np_ndindex_empty(self): func = np_ndindex_empty cres = compile_isolated(func, []) cfunc = cres.entry_point self.assertPreciseEqual(cfunc(), func())
def test_create_nested_list(self): pyfunc = create_nested_list cr = compile_isolated(pyfunc, (types.int32, types.int32, types.int32, types.int32, types.int32, types.int32)) cfunc = cr.entry_point self.assertEqual(cfunc(1, 2, 3, 4, 5, 6), pyfunc(1, 2, 3, 4, 5, 6))
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 run_nullary_func(self, pyfunc, flags): cr = compile_isolated(pyfunc, (), flags=flags) cfunc = cr.entry_point expected = pyfunc() self.assertPreciseEqual(cfunc(), expected)
def test_return_type_unification(self): with self.assertRaises(TypingError) as raises: compile_isolated(impossible_return_type, (types.int32, )) self.assertIn( "Can't unify return type from the following types: (), complex128", str(raises.exception))
def test_unknown_module(self): # This used to print "'object' object has no attribute 'int32'" with self.assertRaises(TypingError) as raises: compile_isolated(unknown_module, ()) self.assertIn("Untyped global name 'numpyz'", str(raises.exception))
def check_arr(arr): cres = compile_isolated(pyfunc, (typeof(arr), )) self.assertPreciseEqual(cres.entry_point(arr), pyfunc(arr))
def test_range_iter_list(self): range_iter_func = range_iter_len2 cres = compile_isolated(range_iter_func, [types.List(types.intp)]) cfunc = cres.entry_point arglist = [1, 2, 3, 4, 5] self.assertEqual(cfunc(arglist), len(arglist))
def test_complex_constant(self): pyfunc = complex_constant cres = compile_isolated(pyfunc, (), flags=forceobj) cfunc = cres.entry_point self.assertEqual(pyfunc(12), cfunc(12))
def test_xrange(self): pyfunc = xrange_usecase cres = compile_isolated(pyfunc, (types.int32,)) cfunc = cres.entry_point self.assertEqual(cfunc(5), pyfunc(5))
def get_cfunc(self, pyfunc): cres = compile_isolated(pyfunc, (self.nbrecord, )) return cres.entry_point
def test_random_randint(self): for tp, max_width in [(types.int64, 2**63), (types.int32, 2**31)]: cr = compile_isolated(random_randint, (tp, tp)) self._check_randint(cr.entry_point, py_state_ptr, max_width)