returning custom types in a cfunc

0 views
Skip to first unread message

Joris Geessels

unread,
Aug 10, 2017, 10:21:26 AM8/10/17
to Numba Public Discussion - Public
I'm trying to combine the ctypes functionality with the custom types. Basically I'm trying to get the following working ( based on the IntervalType example ):

@numba.cfunc(interval_type())
def test():
    return Interval(1., 2.)

# We defined Interval to use a StructureModule
# so we create the equivalent ctypes Structure
class IntervalStruct(ctypes.Structure):
    _fields_ = [('lo', ctypes.c_double), ('hi', ctypes.c_double)]

# We construct our own cfunc, because test.ctypes isn't able to resolve the ctypes type of Interval.
func = ctypes.CFUNCTYPE(IntervalStruct)
interval_struct = func(test.address)()

assert interval_struct.lo == 1. # Sadly this fails.

Any idea what I'm missing here? Using 0.34 here.

Joris Geessels

unread,
Aug 11, 2017, 5:02:33 AM8/11/17
to Numba Public Discussion - Public
Made some progress on this, appears a pointer is returned ( which makes sense :-) ).
Ok, no problem I change my cfunc to:

 func = ctypes.CFUNCTYPE(ctypes.POINTER(IntervalStruct))
 interval_ptr = func(test.address)()
 lo = interval_ptr.contents.lo
 
Doing this doesn't give me rubbish, but I get the value '2.0'.  That's the value I would expect for 'hi'.
There's still something I don't understand about the memory_layout. When I do the following:

interval_ptr_dbl = ctypes.cast(interval_ptr, ctypes.POINTER(ctypes.c_double))

print(interval_ptr_dbl[0]) # -> gives '2.0' as it's the same ptr as above
print(interval_ptr_dbl[-1]) # -> gives '1.0'

Any insights on this?
Reply all
Reply to author
Forward
0 new messages