Ejemplo n.º 1
0
    def test_index(self):
        pyfunc = tuple_index
        cfunc = jit(nopython=True)(pyfunc)

        p = Point(4, 5, 6)
        for i in range(len(p)):
            self.assertPreciseEqual(cfunc(p, i), pyfunc(p, i))

        # test uintp indexing (because, e.g., parfor generates unsigned prange)
        for i in range(len(p)):
            self.assertPreciseEqual(cfunc(p, types.uintp(i)), pyfunc(p, i))
Ejemplo n.º 2
0
    def test_index(self):
        pyfunc = tuple_index
        cfunc = jit(nopython=True)(pyfunc)

        p = Point(4, 5, 6)
        for i in range(len(p)):
            self.assertPreciseEqual(cfunc(p, i), pyfunc(p, i))

        # test uintp indexing (because, e.g., parfor generates unsigned prange)
        for i in range(len(p)):
            self.assertPreciseEqual(cfunc(p, types.uintp(i)), pyfunc(p, i))
Ejemplo n.º 3
0
    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 negative indexing
        for i in range(len(tup) + 1):
            self.assertPreciseEqual(cr.entry_point(tup, -i), tup[-i])

        # oob indexes, +ve then -ve
        with self.assertRaises(IndexError) as raises:
            cr.entry_point(tup, len(tup))
        self.assertEqual("tuple index out of range", str(raises.exception))
        with self.assertRaises(IndexError) as raises:
            cr.entry_point(tup, -(len(tup) + 1))
        self.assertEqual("tuple index out of range", str(raises.exception))

        # 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))

        # test uintp indexing (because, e.g., parfor generates unsigned prange)
        cr = compile_isolated(pyfunc,
                              [types.UniTuple(types.int64, 3), types.uintp])
        for i in range(len(tup)):
            self.assertPreciseEqual(cr.entry_point(tup, types.uintp(i)),
                                    tup[i])

        # 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, ))

        # test unpack, staticgetitem with imprecise type (issue #3895)
        pyfunc = tuple_unpack_static_getitem_err
        with self.assertTypingError() as raises:
            cr = compile_isolated(pyfunc, ())
        msg = ("Cannot infer the type of variable 'c', have imprecise type: "
               "list(undefined).")
        self.assertIn(msg, str(raises.exception))
Ejemplo n.º 4
0
    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 negative indexing
        for i in range(len(tup) + 1):
            self.assertPreciseEqual(cr.entry_point(tup, -i), tup[-i])

        # oob indexes, +ve then -ve
        with self.assertRaises(IndexError) as raises:
            cr.entry_point(tup, len(tup))
        self.assertEqual("tuple index out of range", str(raises.exception))
        with self.assertRaises(IndexError) as raises:
            cr.entry_point(tup, -(len(tup) + 1))
        self.assertEqual("tuple index out of range", str(raises.exception))

        # 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))

        # test uintp indexing (because, e.g., parfor generates unsigned prange)
        cr = compile_isolated(pyfunc,
                              [types.UniTuple(types.int64, 3), types.uintp])
        for i in range(len(tup)):
            self.assertPreciseEqual(cr.entry_point(tup, types.uintp(i)), tup[i])

        # 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,))

        # test unpack, staticgetitem with imprecise type (issue #3895)
        pyfunc = tuple_unpack_static_getitem_err
        with self.assertTypingError() as raises:
            cr = compile_isolated(pyfunc, ())
        msg = ("Cannot infer the type of variable 'c', have imprecise type: "
               "list(undefined).")
        self.assertIn(msg, str(raises.exception))
Ejemplo n.º 5
0
def gen_hash_compare_ops(key_type):

    deref_voidptr = gen_deref_voidptr(key_type)
    c_sig_hash = types.uintp(types.voidptr)
    c_sig_eq = types.boolean(types.voidptr, types.voidptr)

    @cfunc(c_sig_hash)
    def hash_func_adaptor(voidptr_to_data):
        obj = deref_voidptr(voidptr_to_data)
        return hash(obj)

    @cfunc(c_sig_eq)
    def eq_func_adaptor(lhs_ptr, rhs_ptr):
        lhs_str = deref_voidptr(lhs_ptr)
        rhs_str = deref_voidptr(rhs_ptr)
        return lhs_str == rhs_str

    hasher_ptr = hash_func_adaptor.address
    eq_ptr = eq_func_adaptor.address

    return hasher_ptr, eq_ptr