6

I use ctypes to access a file reading C function in python. As the read data is huge and unknown in size I use **float in C .

int read_file(const char *file,int *n_,int *m_,float **data_) {...}

The functions mallocs an 2d array, called data, of the appropriate size, here n and m, and copies the values to the referenced ones. See following snippet:

*data_ = data;
*n_ = n;
*m_ = m;

I access this function with the following python code:

p_data=POINTER(c_float)
n=c_int(0)
m=c_int(0)
filename='datasets/usps'
read_file(filename,byref(n),byref(m),byref(p_data))

Afterwards I try to acces p_data using contents, but I get only a single float value.

p_data.contents
c_float(-1.0)

My question is: How can I access data in python?

What do you recommended? Please don't hesitate to point out if I left something unclear!

1
  • +1 for ctypes, the best python library Commented Sep 1, 2010 at 18:36

1 Answer 1

3

might be simpler to do the whole thing in python with the struct library. but if you're sold on ctypes (and I don't blame you, it's pretty cool):

#include <malloc.h>
void floatarr(int* n, float** f)
{
    int i;
    float* f2 = malloc(sizeof(float)*10);
    n[0] = 10;
    for (i=0;i<10;i++)
    { f2[i] = (i+1)/2.0; }
    f[0] = f2;
}

and then in python:

from ctypes import *

fd = cdll.LoadLibrary('float.dll')
fd.floatarr.argtypes = [POINTER(c_int),POINTER(POINTER(c_float))]

fpp = POINTER(c_float)()
ip = c_int(0)
fd.floatarr(pointer(ip),pointer(fpp))
print ip
print fpp[0]
print fpp[1]

the trick is that capitals POINTER makes a type and lowercase pointer makes a pointer to existing storage. you can use byref instead of pointer, they claim it's faster. I like pointer better because it's clearer what's happening.

Sign up to request clarification or add additional context in comments.

3 Comments

You can also use ctypes to parse binary files. I find it a little easier to work with then the struct module.
Hi Amwinter, your answer already made me understand more, but I have two questions left. 1. I read 15.16.1.7. Specifying the required argument types from docs.python.org/library/ctypes.html and I don't understand why argtypes is needed. I tried my example without using it and it worked as well. 2. I mentioned it in the tags but forgot to do so in the actual questions. How would this work when data would be of more than one dimension? I tried print p_data[0][0] but I got an TypeError: 'float' object is unsubscriptable.
Hi @amwinter, I can see you are very experimented in Python can you help me with this, please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.