Appropirate method to expose an internal c-array to Python and vice versa

16 views
Skip to first unread message

Ghislain Vaillant

unread,
May 24, 2013, 4:09:30 AM5/24/13
to cython...@googlegroups.com
Hi everyone,

I know there is a wide number of posts regarding ways to expose internal C-buffers to Python, but I can't my head around a slightly more advanced design. Say I have a structure declared that way:

struct A{
    size_t n
    float* f
<some other stuff maybe>
}

and initialized using a function A_init which let me choose whether I can provide a pointer to an external array or malloc it. How can I reproduce this behaviour in wrapper class ? Right now, I am doing something like:

cdef class A_wrapped
   A __myA
   object _f
 
and in A_wrapped.__cinit__(self, *args), I force __myA.f to be malloc'd and then expose it using:

self._f = PyArray_SimpleNewFromData(1, __myA.size_t, <void*>__myA.f)

However, I now want to be able to specify an optional numpy array f_ext to the constructor, whose data pointer would be used for __myA.f instead of malloc'd. Besides the usual checks whether f_ext is contiguous, has got the right size and type, is there anything else I should be aware of ? What happens if f_ext gets deleted afterwards, does A_wrapped._f still holds it ?

Thank you for your help
Reply all
Reply to author
Forward
0 new messages