[Numpy-discussion] SWIG + Numeric 20.1.0 = core dump: the fix

Edward C. Jones edcjones at erols.com
Sat Aug 11 21:56:22 EDT 2001

    Numeric and SWIG are used together in creating a Python extension
    there are C/C++ files as part of the extension
  Numeric 20.1.0 will probably break your code.

This happens because changes were made to arrayobject.h between
Numeric 20.0.0 and 20.1.0.

The changes that need to be made in your code are explained
by Konrad Hinsen:

     FROM: Konrad Hinsen
     DATE: 05/09/2001 09:12:25
     SUBJECT:  [Numpy-discussion] Modified header files

     Recently we had a discussion about how to use NumPy arrays from
     extension modules with multiple source files, on various
     platforms.  The modified header files that are attached to this
     message provide a (hopefully!) general solution. In fact, I
     propose to make them part of the official distribution, unless
     there are objections.

     If used like before, these header files give exactly the same
     result as the ones in NumPy 20.0.0. However, they permit to define
     the name of the C API pointer array and make it globally visible.
     Under the condition that the chosen name is unique, this should
     not create problems under any platform, no matter if static or
     dynamic linking is used.

     To use NumPy features in multiple source file extension modules,
     you have to write

        #define PY_ARRAY_UNIQUE_SYMBOL PyArrayXXX
        #include "Numeric/arrayobject.h"

     in the main source file (the one that contains the init function)
        #define PY_ARRAY_UNIQUE_SYMBOL PyArrayXXX
        #define NO_IMPORT_ARRAY
        #include "Numeric/arrayobject.h"

     in all the others. The symbol you choose instead of PyArrayXXX
     should contain both the name of the imported module (array) and
     the name of the importing module (whatever your module is called)
     in order to be unique with a reasonably high probability.

     The same applies to the Ufunc module, just replace "array" by
     "ufunc" in the example. I have also applied the "static"
     correction to the Ufunc header file, there is no reason not to do

     Konrad Hinsen                            | E-Mail:
     hinsen at NO-SPAM.cnrs-orleans.fr
     Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-
     Rue Charles Sadron                       | Fax:  +33-
     45071 Orleans Cedex 2                    | Deutsch/Esperanto/English/
     France                                   | Nederlands/Francais

I do this by modifying one header file that is included in all
the ".c" and ".i" files. In the following example the header file
is "spam.h".


#! /usr/bin/python

import Numeric, spam

print 'spam.funct completed'

#include "Python.h"
#define PY_ARRAY_UNIQUE_SYMBOL Py_Array_API_spam

#include "/usr/include/python2.1/Numeric/arrayobject.h"
void funct(void);

%module spam

#include "spam.h"

%init %{
printf("called import_array\n");

void funct(void);

#include "spam.h"

void funct(void)
    PyArrayObject *pao;
    int dims[2];

    dims[0] = 100;
    dims[1] = 200;
    printf("Calling PyArray_FromDims\n");
    pao = (PyArrayObject*) PyArray_FromDims(2, dims, PyArray_UBYTE);
    printf("Completed PyArray_FromDims\n");
compile script:

swig -python spam.i
gcc -c -Wall spammodule.c spam_wrap.c -I/usr/include/python2.1
ld -shared spammodule.o spam_wrap.o -o spam.so

I use RedHat 7.1 Linux on a PC, Python 2.1, Numeric 20.1.0,
SWIG 1.1 build 883, and gcc 2.96. Anyone is welcome to use
the above example in documentation.

More information about the NumPy-Discussion mailing list