Created
October 23, 2012 01:19
-
-
Save jey/3936094 to your computer and use it in GitHub Desktop.
segfaulting python extension module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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