Hi all,
I'm using cython to wrap an existing C library is being used as
part of a python library for runtime code generation.
The basic setup is that we have a bunch of python objects, each
of which has a C counterpart (a struct)
The C struct is wrapped in a cython cdef class. So far, so good.
Now, we're generating C code at runtime that manipulates the data
associated with these objects and compiling and calling that with
instant.
For almost all use cases, this works fine: the data we need to
pass into the function started life on the python side. But for
one type of object, the data pointer that needs to be passed to
the compiled function lives in C land.
So we need a way of wrapping this pointer up as a python object
to pass back to python, and then unwrapping it in the C code.
At the moment we're doing this:
cdef cython_class:
cdef core.c_struct _handle
def __cinit__(self, python_class):
...
self._handle = core.build_c_struct(...)
The python class looks like:
def python_class(object):
def __init__(self, ...):
...
self._handle = cython_class(self)
Now we build some C code to compile:
_fun = instant.inline("code_that_needs_c_struct")
_fun(python_class_instance._handle)
To get the C struct from this object we're currently doing:
typedef struct {
PyObject_HEAD;
c_struct _handle;
} cython_c_struct;
void code_that_needs_c_struct(PyObject *o)
{
c_struct foo = ((cython_c_struct *)o)->_handle;
...
}
This works. But obviously we're worried that we're relying on an
internal Cython implementation detail of how it lays its data
structures out. Is this the best way to do things, or have we
missed some obvious correct method?
Cheers,
Lawrence
--
Lawrence Mitchell <
we...@gmx.li>