Skip to content

Instantly share code, notes, and snippets.

@jey
Created October 23, 2012 01:19
Show Gist options
  • Save jey/3936094 to your computer and use it in GitHub Desktop.
Save jey/3936094 to your computer and use it in GitHub Desktop.
segfaulting python extension module
crash: mytest.so
python -c "import mytest; import numpy; mytest.foo(numpy.array([123.0]))"
mytest.so: test1.c test2.c
$(CC) -fPIC -shared `python-config --cflags` -o $@ $^ `python-config --ldflags`
#include "Python.h"
#include "numpy/arrayobject.h"
PyObject* foo(PyObject *self, PyObject *args);
static PyMethodDef methods[] = {
{ "foo", foo, METH_VARARGS, "" },
{ 0 }
};
PyMODINIT_FUNC initmytest()
{
Py_InitModule("mytest", methods);
import_array();
}
#include "Python.h"
#include "numpy/arrayobject.h"
#include <stdio.h>
PyObject* foo(PyObject *self, PyObject *args)
{
PyArrayObject *arr = NULL;
if(!PyArg_ParseTuple(args, "O&", PyArray_Converter, &arr)) return NULL;
printf("%f\n", *(double*)PyArray_DATA(arr));
Py_DECREF(arr);
Py_RETURN_NONE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment