Commit ebc3df31 authored by Stefan Behnel's avatar Stefan Behnel

implement recursive carray-to-Python coercion

parent d05cbd9b
...@@ -2258,7 +2258,6 @@ class CArrayType(CPointerBaseType): ...@@ -2258,7 +2258,6 @@ class CArrayType(CPointerBaseType):
'cname': to_py_function, 'cname': to_py_function,
'to_tuple_cname': to_tuple_function, 'to_tuple_cname': to_tuple_function,
'base_type': base_type, 'base_type': base_type,
'to_py_func': self.base_type.to_py_function,
} }
env.use_utility_code(CythonUtilityCode.load( env.use_utility_code(CythonUtilityCode.load(
"carray.to_py", "CConvert.pyx", "carray.to_py", "CConvert.pyx",
......
...@@ -66,29 +66,32 @@ cdef int {{cname}}(object o, {{base_type}} *v, Py_ssize_t length) except -1: ...@@ -66,29 +66,32 @@ cdef int {{cname}}(object o, {{base_type}} *v, Py_ssize_t length) except -1:
#################### carray.to_py #################### #################### carray.to_py ####################
cdef extern from *: cdef extern from *:
ctypedef struct PyObject void Py_INCREF(object o)
PyObject* {{to_py_func}}({{base_type}}) except NULL
tuple PyTuple_New(Py_ssize_t size) tuple PyTuple_New(Py_ssize_t size)
void PyTuple_SET_ITEM(object p, Py_ssize_t pos, PyObject* o)
list PyList_New(Py_ssize_t size) list PyList_New(Py_ssize_t size)
void PyList_SET_ITEM(object p, Py_ssize_t pos, PyObject* o) void PyTuple_SET_ITEM(object p, Py_ssize_t pos, object o)
void PyList_SET_ITEM(object p, Py_ssize_t pos, object o)
@cname("{{cname}}") @cname("{{cname}}")
cdef inline list {{cname}}({{base_type}} *v, Py_ssize_t length): cdef inline list {{cname}}({{base_type}} *v, Py_ssize_t length):
cdef size_t i cdef size_t i
cdef object value
l = PyList_New(length) l = PyList_New(length)
for i in range(<size_t>length): for i in range(<size_t>length):
PyList_SET_ITEM(l, i, {{to_py_func}}(v[i])) value = v[i]
Py_INCREF(value)
PyList_SET_ITEM(l, i, value)
return l return l
@cname("{{to_tuple_cname}}") @cname("{{to_tuple_cname}}")
cdef inline tuple {{to_tuple_cname}}({{base_type}} *v, Py_ssize_t length): cdef inline tuple {{to_tuple_cname}}({{base_type}} *v, Py_ssize_t length):
cdef size_t i cdef size_t i
cdef object value
t = PyTuple_New(length) t = PyTuple_New(length)
for i in range(<size_t>length): for i in range(<size_t>length):
PyTuple_SET_ITEM(t, i, {{to_py_func}}(v[i])) value = v[i]
Py_INCREF(value)
PyTuple_SET_ITEM(t, i, value)
return t return t
...@@ -52,6 +52,21 @@ cpdef tuple tuple_from_typedef_int_array(): ...@@ -52,6 +52,21 @@ cpdef tuple tuple_from_typedef_int_array():
return v return v
def from_int_array_array():
"""
>>> from_int_array_array()
[[11, 12, 13], [21, 22, 23]]
"""
cdef int[2][3] v
v[0][0] = 11
v[0][1] = 12
v[0][2] = 13
v[1][0] = 21
v[1][1] = 22
v[1][2] = 23
return v
ctypedef struct MyStructType: ctypedef struct MyStructType:
int x int x
double y double y
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment