Passing C pionters to Python for use with cffi

Eric Frederich eric.frederich at gmail.com
Thu Oct 10 11:47:49 EDT 2013


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20131010/b62e0cd2/attachment.html>


More information about the Python-list mailing list