Providing array interface from SWIG-wrapped C struct

Lisandro Dalcin dalcinl at
Thu Oct 12 13:40:46 CDT 2006

On 10/12/06, Michele Vallisneri <vallis at> 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 = vec.__array_struct__

and not with a method call like this

arrstr = vec.__array_struct__()

So here is what I would do (can fail with some SWIG optimizations)

%extend Vec {

PyObject* __array_struct__ () { /* ...  */ }

  %pythoncode {
  __array_struct__ = property(__array_struct__,
                                                 doc='Array protocol')


Hope you got the idea.

Lisandro Dalcín
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo

More information about the Numpy-discussion mailing list