Python extension module segfault
<p>I wrote a nifty fitter in C, and I want to provide an interface in python. So I wrote an extension module for it (below). However, when I run</p>

<pre><code>from nelder_mead import minimize
minimize(lambda x: x[0]**2, [42])
</code></pre>

<p>I get a segfault. I have no idea where to begin debugging, is the problem at all obvious? When I call the fitter from C, it works great.</p>

<p>Here's my wrapper, the function primitive for the fitter is at the top:</p>

<pre><code>#include "Python.h"
void _minimize(double (*func)(double* x, void* args), double* x0, int N, void* args);

typedef struct arg_struct {
   PyObject* func;
   int N;
} arg_struct;

double func(double* x, void* args){
  //Build a PyList off of x
  int N = ((arg_struct*)args)-&gt;N;
  PyObject* x_list = PyList_New(N);
  for (int i=0; i&lt;N; i++){ PyList_SetItem(x_list, i, PyFloat_FromDouble(x[i])); }

  //Pass x_list into the python objective function
  PyObject* arglist = Py_BuildValue("(O)", x_list);
  PyObject* result = PyObject_CallObject(((arg_struct*)args)-&gt;func, arglist);

  //Convert result to a double and return
  return PyFloat_AsDouble(result);
}

static PyObject* minimize(PyObject* self, PyObject* py_args){
  //Get object pointers to f and x0
  PyObject* py_func, *x0_list;
  if (!PyArg_ParseTuple(py_args, "OO", py_func, x0_list)) return NULL;

  //Copy doubles out of x0_list to a regular double* array
  int N = PyList_Size(x0_list);
  double* x0 = (double*)malloc(N*sizeof(double));
  for (int i=0; i&lt;N; i++) x0[i] = PyFloat_AsDouble(PyList_GetItem(x0_list, i));

  //Set up an arg_struct and minimize py_func(x0)
  arg_struct c_args = { py_func, N };
  _minimize(&amp;func, x0, N, (void*)&amp;c_args);

  //Now build a list off of x0 and return it
  PyObject* result_list = PyList_New(N);
  for (int i=0; i&lt;N; i++){ PyList_SetItem(result_list, i, PyFloat_FromDouble(x0[i])); }
  return result_list;
  }

//Initialize the module with the module table and stuff
static PyMethodDef module_methods[] = {
   { "minimize", minimize, METH_VARARGS, "Minimize a function." },
   { NULL, NULL, 0, NULL }
};
PyMODINIT_FUNC
initnelder_mead(void){
  (void) Py_InitModule("nelder_mead", module_methods);
}
</code></pre>

<p>And here's my <code>setup.py</code>:</p>

<pre><code>from distutils.core import setup
from distutils.extension import Extension

setup(name='nelder_mead',
      ext_modules = [Extension('nelder_mead', sources = ['wrapper.c', 'nm.c'])])
</code></pre>

<p>Turned out to be two small things. As @robyschek pointed out, I should have put <code>x[0]</code> instead of <code>x</code>, which caused a type error. Additionally, it turns out the argument parsing needs pointers to pointers:
changing it to <code>PyArg_ParseTuple(py_args, "OO", &amp;py_func, &amp;x0_list)</code> works fine.</p>

python c python-c-api