title: Using Py_buffer and PyMemoryView_FromBuffer with different itemsizes
body: <p>This question is related to a previous question I asked. Namely <a href="https://stackoverflow.com/questions/15033683/calling-a-python-function-from-a-c-thread-with-a-mutable-c-array">this one</a> if anyone is interested. Basically, what I want to do is to expose a C array to Python using a <code>Py_buffer</code> wrapped in a <code>memoryview</code>-object. I've gotten it to work using <code>PyBuffer_FillInfo</code> (work = I can manipulate the data in Python and write it to stdout in C), but if I try to roll my own buffer I get a segfault <em>after</em> the C function returns.</p>
  
  <p>I need to create my own buffer because PyBuffer_FillInfo assumes that the format is char, making the itemsize field 1. I need to be able to provide items of size 1, 2, 4 and 8.</p>
  
  <p>Some code, this is a working example:</p>
  
  <pre><code>Py_buffer *buf = (Py_buffer *) malloc(sizeof(*buf));
  int r = PyBuffer_FillInfo(buf, NULL, malloc(sizeof(char) * 4), 4, 0, PyBUF_CONTIG);
  PyObject *mv = PyMemoryView_FromBuffer(buf);
  //Pack the memoryview object into an argument list and call the Python function
  for (blah)
    printf("%c\n", *buf-&gt;buf++); //this prints the values i set in the Python function
  </code></pre>
  
  <p>Looking at the implementation of <code>PyBuffer_FillInfo</code>, which is really simple, I rolled my own function to be able to provide custom itemsizes:</p>
  
  <pre><code>//buffer creation function
  Py_buffer *getReadWriteBuffer(int nitems, int itemsize, char *fmt) {
    Py_buffer *buf = (Py_buffer *) malloc(sizeof(*buf));
    buf-&gt;obj = NULL
    buf-&gt;buf = malloc(nitems * itemsize);
    buf-&gt;len = nitems * itemsize;
    buf-&gt;readonly = 0;
    buf-&gt;itemsize = itemsize;
    buf-&gt;format = fmt;
    buf-&gt;ndim = 1;
    buf-&gt;shape = NULL;
    buf-&gt;strides = NULL;
    buf-&gt;suboffsets = NULL;
    buf-&gt;internal = NULL;
    return buf;
  }
  </code></pre>
  
  <p>How i use it:</p>
  
  <pre><code>Py_buffer *buf = getReadWriteBuffer(32, 2, "h");
  PyObject *mv = PyMemoryView_FromBuffer(buf);
  // pack the memoryview into an argument list and call the Python function as before
  
  for (blah)
    printf("%d\n", *buf-&gt;buf); //this prints all zeroes even though i modify the array in Python
  
  return 0;
  //the segfault happens somewhere after here
  </code></pre>
  
  <p>The result of using my own buffer object is a segfault after the C function returns. I really don't understand why this happens at all. Any help would be most appreciated.</p>
  
  <p><strong>EDIT</strong>
  According to <a href="https://stackoverflow.com/questions/2297026/assignment-into-python-3-x-buffers-with-itemsize-1?rq=1">this question</a>, which I failed to find before, itemsize > 1 might not even be supported at all. Which makes this question even more interesting. Maybe I could use <code>PyBuffer_FillInfo</code> with a large enough block of memory to hold what I want (32 C floats for example). In that case, the question is more about how to assign Python floats to the <code>memoryview</code> object in the Python function. Questions questions.</p>