python - Struct definition with typed memoryview as member -


currently trying struct typed memoryview work. e.g.

ctypedef struct node:     unsigned int[:] inds 

if inds not memoryview works flawlessly far can see. however, memoryview , using

def testfunction():     cdef node testnode        testnode.inds = numpy.ones(3,dtype=numpy.uint32) 

i segmentation fault. there obvious overlooking or typed memoryviews not work in structs?


edit:

so rewrote example little bit (due larsman's questions) clarify problem little bit:

def testfunction():     cdef node testnode     cdef unsigned int[:] tempinds     tempinds = numpy.ones(3,dtype=numpy.uintc)     testnode.inds = tempinds 

only last line produces segfault. don't know how check problem in more detail. me looks structs can't handle typed memory views... missing something.

i using cython 0.20 (just updated now, 0.19 gave same error). updated numpy 1.8.0 well.


edit 2:

if similar problems reads in future: searched quite lot , can't work. else working except memory views guess not (easily) possible. intended use memory views due convenient slicing of multidimensional arrays, apparently don't work in structs. saving c pointer in struct , cast them memory view if need slicing... 1 has careful arrays accessed correctly, in case seems work. if there no resolving answers in thread in couple of weeks maybe doing feature request cython.

the problem comes uninitialized memory because of cython lack of constructors primitive cdefed structs. when testnode.inds = tempinds assignment made cython tries release testnode.inds not initialized , dereferences random pointer.

this indeed bug in cython (please file!) , there's no solution (to knowledge), here's ugly hack works.

from libc.string cimport memset  ctypedef struct node:     int dummy_hack     unsigned int[:] inds  def testfunction():     cdef node testnode     cdef unsigned int[:] tempinds     tempinds = numpy.ones(3,dtype=numpy.uintc)     # 0 init memory avoid deref garbage     memset(&testnode.dummy_hack, 0, sizeof(void*)*4)     testnode.inds = tempinds 

fyi. actual memory view object in c++ looks (on cython 21.2):

memoryview object  __pyx_memviewslice struct typedef struct {   struct __pyx_memoryview_obj *memview;   char *data;   py_ssize_t shape[8];   py_ssize_t strides[8];   py_ssize_t suboffsets[8]; }; 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -