title: How to pass Python list to C function using Cython
body: <p>I am using a Raspberry Pi to interface with custom hardware connected to the GPIO. The controlling software is written in Python, and the interface to the custom hardware is written in C, as it is a much faster C implementation. I now need to start calling my C functions from my Python, and have recently been learning how to wrap C in Cython. I have got everything to work, except passing a Python list to a C function.</p>
  
  <p>My custom hardware needs to be sent anywhere from 1 to 32 bytes, hence the use of an array.</p>
  
  <p>The Cython tutorials and other references I have read online either are really simple, and do not include how to pass lists to C, use numpy, which I am not using, or use very complicated code examples that lack sufficient documentation for me to understand it properly.</p>
  
  <p>What I have now are:</p>
  
  <p>test.c</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "test.h"
  void pop(void) {
      a[0] = 0x55;
      a[1] = 0x66;
      a[2] = 0x77;
      a[3] = '\0';
  }
  void putAll(int n, char c[]) {
      memcpy(a, c, n);
  }
  char *getAll(void) {
      return &amp;a[0];
  }
  </code></pre>
  
  <p>test.h</p>
  
  <pre><code>char a[4];
  
  void putAll(int n, char[]);
  char *getAll(void);
  </code></pre>
  
  <p>pytest.pyx</p>
  
  <pre><code>cimport defns
  
  # Populate C array with values
  def pypop():
      defns.pop()
  
  # Pass python list to C
  def pyPutAll(int n, char[:] pyc):
      cdef char* c = pyc
      defns.putAll(n, c)
  
  # Get array from C
  def pyGetAll():
      cdef char* c = defns.getAll()
      cdef bytes pyc = c
      print pyc
  </code></pre>
  
  <p>defns.pxd</p>
  
  <pre><code>cdef extern from "test.h":
      char a[4]
      void pop()
      void putAll(int n, char c[])
      char *getAll()
  </code></pre>
  
  <p>Using the tutorials at <a href="http://docs.cython.org/src/tutorial/index.html" rel="noreferrer">cython.org</a>, my getAll() and pop() functions work, but when I include the putAll() function (taken from the process_byte_data example code found at the link, under Unicode and passing strings > Accepting strings from Python code), I get this error:</p>
  
  <pre><code>python setup.py build_ext -i
  
  Error compiling Cython file:
  ------------------------------------------------------------
  ...
  
  def pyputAll(int n, char[:] pyc):
                          ^
  ------------------------------------------------------------
  
  pytest.pyx:13:25: Expected an identifier or literal
  </code></pre>
  
  <p>Now, I have a way around this - combining up to 32 bytes into an int and passing as a long int, and then pulling it apart in C - but it is very ugly.</p>
  
  <p>Also, I do not require Cython for any performance gains, other than that of using the C implemented library for interfacing with my custom hardware vs a Python implemented one.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <hr>
  
  <p><strong>(Edit) Solution</strong></p>
  
  <p>I managed to get this working. Here is the code I now have for anyone who needs it.</p>
  
  <p>pytest.pyx</p>
  
  <pre><code>...
  def pyPutAll(int n, c):
      cdef int *ptr
      ptr = &lt;int *&gt;malloc(n*cython.sizeof(int))
      if ptr is NULL:
              raise MemoryError()
      for i in xrange(n):
              ptr[i] = c[i]
      defns.putAll(n, ptr)
      free(ptr)
  ...
  </code></pre>
  
  <p>test.c</p>
  
  <pre><code>void putAll(int n, int c[])
  {
      char d[n];
      int i;
      for (i=0;i&lt;n;i++) {
              d[i] = c[i];
      }
      memcpy(addr, d, n);
  }
  </code></pre>
  
  <p>This code is not optimal, as it uses ints in the python/cython code, then converts it to char in the C function. The <code>pyPutAll()</code> function in pytest.pyc accepts an ordinary python list. It then creates a C pointer and allocates memory. Iterating through the list, each value is put into a C array, and then finally passes the pointer to the C function.</p>
  
  <p>It gets the job done, but I'm sure someone else can give a much more efficient solution.</p>
  
  <p>Matt</p>