Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy C-Api example gives a SegFault

I'm trying to understand how the Python C- Api works, and I want to exchange numpy arrays between Python and a C Extension.

So, I started this tutorial: http://dsnra.jpl.nasa.gov/software/Python/numpydoc/numpy-13.html

Tried to do the first example there, a C module that calculates the trace of a 2d numpy array, was very neat for me, since I want to do elementary operations in 2d arrays also.

#include <Python.h>
#include "Numeric/arrayobject.h"
#include<stdio.h>

int main(){
Py_Initialize();
import_array();
}

static char doc[] =
"This is the C extension for xor_masking routine";

    static PyObject *
    trace(PyObject *self, PyObject *args)
    {
    PyObject *input;
    PyArrayObject *array;
    double sum;
    int i, n;

    if (!PyArg_ParseTuple(args, "O", &input))
    return NULL;
    array = (PyArrayObject *)
    PyArray_ContiguousFromObject(input, PyArray_DOUBLE, 2, 2);
    if (array == NULL)
    return NULL;

    n = array->dimensions[0];
    if (n > array->dimensions[1])
    n = array->dimensions[1];
    sum = 0.;
    for (i = 0; i < n; i++)
    sum += *(double *)(array->data + i*array->strides[0] + i*array->strides[1]);
    Py_DECREF(array);
    return PyFloat_FromDouble(sum);
    }

static PyMethodDef TraceMethods[] = {
    {"trace", trace, METH_VARARGS, doc},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
inittrace(void)
{
    (void) Py_InitModule("trace", TraceMethods);
}


}

The module's name is trace, and it is compiled with the setup.py file:

from distutils.core import setup, Extension

module = Extension('trace', sources = ['xor_masking.cpp'])
setup(name = 'Trace Test', version = '1.0', ext_modules = [module])

The file is compiled, trace.so is imported in IPython, but when I try to use the method trace(), I get a Segmentation Fault, I don't know why.

I run this with Fedora 15, Python 2.7.1, gcc 4.3.0, Numpy 1.5.1

like image 663
mehmet.ali.anil Avatar asked Oct 11 '11 18:10

mehmet.ali.anil


1 Answers

Your init function for the module needs to call

import_array();

after

(void) Py_InitModule("trace", TraceMethods);

It mentions this in the tutorial near the top, but it is easy to miss. Without this, it segfaults on PyArray_ContiguousFromObject.

like image 110
Justin Peel Avatar answered Nov 18 '22 10:11

Justin Peel