Hi there. I have an issue where I need to create a callback function that accepts a void pointer to char array, extracts the content of the array and prints to screen for debugging purposes.
So far, I’ve managed to do the following
import numpy as np
from numba import cfunc, carray, types
import ctypes
import numpy as np
from numba import cfunc, carray, types
import ctypes
str_ = "Hello World!".encode('utf-8')
ptr_ = ctypes.cast(ctypes.c_char_p(str_), ctypes.c_void_p)
sig = types.int8(types.voidptr, types.int8)
@cfunc(sig)
def get_string_from_ptr(ptr_, index):
s=carray(ptr_, 1, types.int8)
return s[index]
string_ = ""
for i in range(len(str_)):
val = get_string_from_ptr.ctypes(ptr_, i)
string_ += chr(val)
print(string_)
this lets me print each character outside of the c-func one-by-one, if I iterate over the index of the array. Strangely, if I print any of the values inside get_string_from_ptr
, for example
@cfunc(sig)
def get_string_from_ptr(ptr_, index):
s=carray(ptr_, 1, types.int8)
print(s[0])
the program hangs. So I can’t actually do anything with the string inside the c-wrapped function.
I’m sorry for all these questions, but numba’s documentation about this particular library is very limited, and I have not found any posts on the forum that discusses this usecase.
Thank you