Yet another Numeric Swig core dump problem
Travis Oliphant
oliphant.travis at ieee.org
Sat Aug 11 14:27:06 EDT 2001
Edward C. Jones wrote:
> The following Python / SWIG / Numeric program dumps core. I use RedHat
> Linux on a PC. Python 2.1, Numeric 20.0.0, SWIG 1.1 build 883, gcc 2.96.
>
> The output looks like:
>
> > testdb.py
> called import_array
> L11
> Segmentation fault (core dumped)
> >KLTfeatures.KLTSelectGoodFeatures()
> >
>
> The program files are:
>
> -----------
> testdb.py:
>
> #! /usr/bin/python
>
> import Numeric, KLTfeatures
>
> KLTfeatures.KLTSelectGoodFeatures()
>
> -----------
> KLTfeatures.h:
>
> #include "Python.h"
> #include "/usr/include/python2.1/Numeric/arrayobject.h"
>
> void KLTSelectGoodFeatures(void);
>
> -----------
> KLTfeatures.i:
>
> %module KLTfeatures
>
> %{
> #include "KLTfeatures.h"
> %}
>
> %init %{
> import_array();
> printf("called import_array\n");
>
> %}
>
> void KLTSelectGoodFeatures(void);
>
> -----------
> KLTfeaturesmodule.c:
>
> #include "KLTfeatures.h"
>
> void KLTSelectGoodFeatures()
> { PyArrayObject *pao;
> int dims[2];
>
> dims[0] = 100;
> dims[1] = 200;
> printf("L11\n");
> pao = (PyArrayObject*) PyArray_FromDims(2, dims, PyArray_UBYTE);
> printf("L17\n");
> Py_DECREF(pao);
> }
>
> ----------
> How to compile:
>
> swig -python KLTfeatures.i
> gcc -c -Wall KLTfeaturesmodule.c KLTfeatures_wrap.c
> -I/usr/include/python2.1 ld -shared KLTfeaturesmodule.o KLTfeatures_wrap.o
> -o KLTfeatures.so
>
import_array must be in the file that is accessing the C-API for Numeric.
You are using PyArray_FromDims in a file without the import_array statement.
A quick hack is to place import_array in the KLTfeatures file.
A better way is to use some defines appropriately as described by Paul
Dubois (I'm not sure exactly how to do it).
-Travis
More information about the Python-list
mailing list