This fixes tests 2, 4 and 7, but not 8 and 9 (see below). I am not sure about the details of what the code in Buffer.c is supposed to do and whether my modification would break some other functionality, but my code (not only the tests) works with this fix.
Or am I possibly simply missing "the right way" to handle arrays of structs containing arrays in Cython? But in either case the compiler shouldn't crash like in test 9...
Here is the code of my tests with and without numpy with results ranging from "ok" to "compiler crash".
import numpy as np
cimport numpy as np
Test 1 (OK)
cdef packed struct My_Struct:
double my_first_value
double my_second_value
cpdef test():
cdef My_Struct my_struct
cdef My_Struct[:] my_view = <My_Struct[:1]>&my_struct
Test 2 (FAILS)
cdef packed struct My_Struct:
double my_first_value
double my_second_value[1]
cpdef test():
cdef My_Struct my_struct
cdef My_Struct[:] my_view = <My_Struct[:1]>&my_struct
ValueError: Expected 1 dimension(s), got 1
Test 3 (OK)
cdef packed struct My_Struct:
double my_first_value
double my_second_value[1]
cpdef test():
cdef np.ndarray my_array = np.ndarray(1, dtype=np.dtype([('my_first_value','float64'), ('my_second_value','float64',1)], align=False)
cdef My_Struct[:] my_view = my_array
Test 4 (FAILS)
cdef packed struct My_Struct:
double my_first_value
double my_second_value[2]
cpdef test():
cdef np.ndarray my_array = np.ndarray(1, dtype=np.dtype([('my_first_value','float64'), ('my_second_value','float64',2)], align=False)
cdef My_Struct[:] my_view = my_array
ValueError: Expected 1 dimension(s), got 1
Test 5 (OK)
cdef packed struct My_Struct:
double my_first_value
char my_second_value[2]
cpdef test():
cdef np.ndarray my_array = np.ndarray(1, dtype=np.dtype([('my_first_value','float64'), ('my_second_value','a',2)], align=False)
cdef My_Struct[:] my_view = my_array
Test 6 (OK)
cdef packed struct My_Array_Struct:
double values[1]
cdef packed struct My_Struct:
double my_first_value
My_Array_Struct my_second_value
cpdef test():
cdef np.ndarray my_array = np.ndarray(1, dtype=np.dtype([('my_first_value','float64'), ('my_second_value','float64',1)], align=False)
cdef My_Struct[:] my_view = my_array
Test 7 (FAILS)
cdef packed struct My_Array_Struct:
double values[2]
cdef packed struct My_Struct:
double my_first_value
My_Array_Struct my_second_value
cpdef test():
cdef np.ndarray my_array = np.ndarray(1, dtype=np.dtype([('my_first_value','float64'), ('my_second_value','float64',2)], align=False)
cdef My_Struct[:] my_view = my_array
ValueError: Expected 1 dimension(s), got 1
Test 8 (FAILS)
cdef packed struct My_Struct:
double my_first_value[1]
double my_second_value
cpdef test():
cdef My_Struct my_struct
cdef My_Struct[:] my_view = <My_Struct[:1]>&my_struct
ValueError: Buffer dtype mismatch, expected 'double' but got end in 'My_Struct.my_second_value'
cdef packed struct My_Array_Struct:
double values[2]
cdef packed struct My_Struct:
double my_first_value
My_Array_Struct my_second_value[1]
cpdef test():
cdef My_Struct my_struct
cdef My_Struct[:] my_view = <My_Struct[:1]>&my_struct