Passing C pionters to Python for use with cffi

E

Eric Frederich

Hello,

I'm extending an application that supports customization using the C
language.
I am able to write standalone python applications that use the C API's
using cffi.
This is good, but only a first step.

This application allows me to register code that will run on various events
but it has to be C code.
I'd like to write Python code instead.
So basically, my C code will use the Python C API to get a handle to the
module and function (like how they do it here
http://docs.python.org/2/extending/embedding.html#pure-embedding)
What would be the proper way to pass a pointer to a C structure from C to
Python so that I can use ffi.cast and be able to use it from within Python?

I have got this to work but I'm not certain that it is correct, fool-proof,
or portable.

This is how I got it to work from the C side....

PyObject* pArgs = Py_BuildValue("(k)", &some_structure);
PyObject_CallObject(pFunc, pArgs)

.... and from the Python side...

def my_function(struct_ptr):
struct = ffi.cast("mystruct_t *", struct_ptr)


Like I said, this works fine. I am able to manipulate the structure from
within Python.
I just want to know the correct way to to this.

Thanks,
~Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top