'Usecase test: custom reinterpret cast to turn int values to pointers'

def test_ll_pointer_cast(self):

    from ctypes import CFUNCTYPE, POINTER, c_float, c_int
    def unsafe_caster(result_type):
        assert isinstance(result_type, types.CPointer)
        @intrinsic
        def unsafe_cast(typingctx, src):
            self.assertIsInstance(typingctx, typing.Context)
            if isinstance(src, types.Integer):
                sig = result_type(types.uintp)
                def codegen(context, builder, signature, args):
                    [src] = args
                    rtype = signature.return_type
                    llrtype = context.get_value_type(rtype)
                    return builder.inttoptr(src, llrtype)
                return (sig, codegen)
        return unsafe_cast
    def unsafe_get_ctypes_pointer(src):
        raise NotImplementedError('not    callable    from    python')
    @overload(unsafe_get_ctypes_pointer)
    def array_impl_unsafe_get_ctypes_pointer(arrtype):
        if isinstance(arrtype, types.Array):
            unsafe_cast = unsafe_caster(types.CPointer(arrtype.dtype))
            def array_impl(arr):
                return unsafe_cast(src=arr.ctypes.data)
            return array_impl
    def my_c_fun_raw(ptr, n):
        for i in range(n):
            print(ptr[i])
    prototype = CFUNCTYPE(None, POINTER(c_float), c_int)
    my_c_fun = prototype(my_c_fun_raw)
    @jit(nopython=True)
    def foo(arr):
        ptr = unsafe_get_ctypes_pointer(arr)
        my_c_fun(ptr, arr.size)
    arr = np.arange(10, dtype=np.float32)
    foo(arr)
    with captured_stdout() as buf:
        foo(arr)
        got = buf.getvalue().splitlines()
    buf.close()
    expect = list(map(str, arr))
    self.assertEqual(expect, got)
