Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing arrays with ctypes

I have a C function

void read_FIFO_AI0(int16_t** input, size_t size, NiFpga_Session* session, NiFpga_Status* status)
{
  *input = (int16_t*) malloc (size*sizeof(int16_t));
  // function that populates the array *input
}

which populates the array "*input". Now I want to pass the data in that array to python for further processing. I try to use ctypes to do that:

def read_FIFO_AI0(size,session,status):
    _libfpga.read_FIFO_AI0.argtypes = [POINTER(ARRAY(c_int16, size)), c_int, POINTER(c_uint32), POINTER(c_int32)]
    _libfpga.read_FIFO_AI0.restype = None

    values = (c_int16*size)()
    _libfpga.read_FIFO_AI0(byref(values),size,byref(session),byref(status))
    return values

The code executes but I get wrong results in the array. When I try to use the C function from within C I get proper results:

size_t size=20;
int16_t* input;

read_FIFO_AI0(&input, size, &session, &status);

What would be the right way to populate the array such that I can access the data in Python? I'm not tied to using a pointer to an array that gets populated, I would also be fine with creating the array in the C function and sending this as a return to Python, but I did not get to work either.

like image 287
erik Avatar asked Oct 09 '14 11:10

erik


1 Answers

The first argument's type is POINTER(POINTER(c_int16)) not POINTER(ARRAY(c_int16,size)).

Here's a short example:

x.c (compiled with cl /LD x.c:

#include <stdlib.h>
#include <stdint.h>
__declspec(dllexport) void read(int16_t** input, size_t size)
{
  int i;
  int16_t* p = (int16_t*) malloc (size*sizeof(int16_t));
  for(i=0;i<size;i++)
    p[i] = i;
  *input = p;
}
__declspec(dllexport) void release(int16_t* input)
{
    free(input);
}

x.py

from ctypes import *
x = CDLL('x')
x.read.argtypes = POINTER(POINTER(c_int16)),c_size_t
x.read.restype = None
x.release.argtypes = [POINTER(c_int16)]
x.release.restype = None
p = POINTER(c_int16)()
x.read(p,5)
for i in range(5):
    print(p[i])
x.release(p)

Output:

0
1
2
3
4

Note this leaves you with potential memory leak if you don't remember to free the malloc. A better way would be to allocate the buffer in Python and tell the C function the size:

x.c

#include <stdlib.h>
#include <stdint.h>
__declspec(dllexport) void read(int16_t* input, size_t size)
{
  int i;
  for(i=0;i<size;i++)
    input[i] = i;
}

x.py

from ctypes import *
x = CDLL('x')
x.read.argtypes = POINTER(c_int16),c_size_t
x.read.restype = None
p = (c_int16*5)()
x.read(p,len(p))
print(list(p))

Output

[0, 1, 2, 3, 4]
like image 54
Mark Tolonen Avatar answered Oct 02 '22 11:10

Mark Tolonen