Reading and writing C structs from Python
Edward C. Jones
edcjones at erols.com
Tue Feb 12 14:40:55 EST 2002
Suppose I have a C struct for example
typedef struct {
float x;
int n[4];
} test;
and a pointer to an instance of the struct
test* t = (test*) malloc(sizeof(test));
I would like to wrap this in a python object "py_t" so that (in Python)
"x = py_t.x" reads "t.x" and "py_t.x = 13.0" writes to "t.x". Ditto for
"py_t.n[i]". Ideally I would like to do this using a C function
something like:
PyObject* PyObject_FromCStruct(void* t, char* format)
void* PyObject_AsCStruct(PyObject* pyo, char* format)
The format strings used in module "struct" would be fine.
My guess is that this is a job for the notoriously confusing "buffer
interface". Does anyone have any sample code for problems like this?
Thanks,
Ed Jones
More information about the Python-list
mailing list