I'm trying to write a Cython wrapper to interface C code from Python.
The C library makes use of Suitesparse's CHOLMOD, so I figured installing scikit-sparse
(which uses a cholmod.pyx
that contains everything I need) would be a neat way.
However, I unsuccessfully looked for solutions to include these definitions of CHOLMOD and I would like to avoid writing "my own" cholmod.pxd
with typedefs of the structs that I need.
As a minimal example, let's say I have a foo.h
header file that defines a struct which in turn contains some CHOLMOD structs, along with some dummy functions. My Cython definition file looks like this:
cdef extern from "foo.h":
ctypedef struct foostruct:
cholmod_common c
cholmod_factor *f
cholmod_dense *d
void initialize_foostruct(foostruct* bar)
void modify_foostruct(foostruct* bar)
And the implementation could e.g. be:
from libc.stdlib cimport calloc, malloc, free
from foo cimport *
cdef class Foo:
cdef foostruct* _bar
def __cinit__(self):
self._bar = <foostruct*> calloc(1, sizeof(foostruct))
if self._bar is NULL:
raise MemoryError()
initialize_foostruct(self._bar)
def __dealloc__(self):
if self._bar is not NULL:
free(self._bar)
self._bar = NULL
def do_something(self):
modify_foostruct(self._bar)
Obviously this will fail since cholmod_common
etc. are not known in the definition file (error reads 'cholmod_common' is not a type identifier
). I tried thing like from sksparse.cholmod cimport *
, but to no avail...
Is there a way to somehow import those type identifiers (either from scikit-sparse
, or another source) to use them in the way described in my definition file?