On Tue, Jun 26, 2012 at 5:30 PM, Sarvi Shanmugham <
sarv...@gmail.com> wrote:
> I understand.
> We have an interface generator that takes an interface definition in custom
> Interface Definition Language and generates C code and headers that get
> compiled into a hared library.
> I am trying to generate a python wrapper around that library. So I am not
> have info ahead of time what semantics are good or what fields are
> unnecessary or how deep the sub structure nesting goes.
So are you generating the .pyx files as well in that case?
> So I would like them to be read/write as follows, where z is substruct of y
> is a substruct of x
> x.y.z='hello'
>
> As long as X is malloced Y and Z can point to within it.
>
> if a C Struct is as follows
> typedef struct subsubstruct {
> char chararr[255];
> } subsubstruct_t;
>
> typedef struct substruct {
> substruct_t subsubstruct;
> } substruct_t;
>
> typedef struct mainstruct {
> substruct_t substruct;
> } mainstruct_t;
>
>
> When wrapped with a python wrapper I want the folloing python API
> x=mainstruct() #Mallocs mainstruct_t or can point static definition of
> mainstruct defined within the C library
> x.substruct.subsubstruct.chararr = 'Hello World'
> print x.substruct.subsubstruct.chrarr # this prints 'Hello World'
> y=x.substruct
> y.subsubstruct.chararr = 'This overwrites Hello World'
> print y.subsubstruct.chararr # this prints 'This overwrites Hellow World'
> z=y.subsubstruct
> z.chararr = 'This overwrites again'
> print x.chararr # this prints 'This overwrites again'
> x=None
> y=None
> z=None # Frees mainstruct IF it was malloced
You've hit the nail on the head in terms of the memory management issues.
> I am trying to use cython for the embedded development space, where everyone
> is using C.
> And I am trying to demonstrate that a blend of Python/Cython can be much
> more productive
>
> At this point though, I am quite frustrated and have half a mind to switch
> to using ctypes for this wrapper.
If you're trying to provide a very thin wrapper, ctypes might be a
better fit, though it won't help with the memory issues (i.e. you'll
either end up doing too much copying or having to be very careful that
you don't segfault).
> Atleast the C data structure to python data structure mapping seems quite
> straighforward for this case.
>
> Considering one of Cython's goals is to build python wrappers around C
> libraries, I am surprised this is so difficult.
Typically one doesn't try to mirror the C library interface, as it's
often not very natural to do in Python.
What you might be interested in is the fact that Cython does automatic
dict <-> struct conversion. Thus if one had a C function "structA
cfoo(structB*)" one could simply write
def foo(**kwds):
cdef structB value_c = kwds
return cfoo(&value_c)
And use it as
print foo(int_value=3, substruct=dict(int_field=5, charptr="abc"))
rather than worry about building wrappers for each of these struct
objects with complicated allocation characteristics.
> It is also very likely, that it is because I haven't got the Cython concepts
> straight enough in my head yet.
>
> In ctypes I believe the following would have provided the sort of python API
> I am looking for
>
> class subsubstruct_t(Structure):
> _fields_=[
> ('chararr', c_char *255)
> ]
>
> class substruct_t(Structure):
> _fields_=[
> ('subsubstruct', subsubstruct_t)
> ]
>
> class mainstruct_t(Structure):
> _fields_=[
> ('mainstruct', substruct_t)
> ]
>
>
> Sarvi