From: Lisandro D. <da...@gm...> - 2006-10-12 18:40:50
|
On 10/12/06, Michele Vallisneri <va...@va...> wrote: > Does anybody here have experience about offering the array interface > from a SWIG-wrapped C struct? I have. > I have tried the following, borrowing code from numpy's arrayobject.c: > > %extend real_vec_t { > PyObject *__array_struct__() { > /* From numpy/arrayobject.c/array_struct_get */ You are extending real_vec_t with a new METHOD, but what numpy requests is an ATTRIBUTE. So, numpy simply queries your vec like: arrstr =3D vec.__array_struct__ and not with a method call like this arrstr =3D vec.__array_struct__() So here is what I would do (can fail with some SWIG optimizations) %extend Vec { PyObject* __array_struct__ () { /* ... */ } %pythoncode { __array_struct__ =3D property(__array_struct__, doc=3D'Array protocol') } } Hope you got the idea. --=20 Lisandro Dalc=EDn --------------- Centro Internacional de M=E9todos Computacionales en Ingenier=EDa (CIMEC) Instituto de Desarrollo Tecnol=F3gico para la Industria Qu=EDmica (INTEC) Consejo Nacional de Investigaciones Cient=EDficas y T=E9cnicas (CONICET) PTLC - G=FCemes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 |