[Pyrex] Passing struct between C function

Lenard Lindstrom len-l at telus.net
Sat Nov 5 18:09:10 CET 2005


The OO solution is to build an extension type around the structure 
and call foo_A and foo_B from methods.

cdef class C:
    cdef some_big_struct s

    def __new__(self, a, b, c):
        foo_A(&self.s, a, b, c)

    def do_foo_B(self, d, e, f):
        foo_B(&self.s, d, e, f)

Now all memory management is done by Pyrex and Python.

Lenard Lindstrom
<len-l at telus.net>

On 5 Nov 2005 at 15:21, Nicolas Pernetty wrote:

> Hello,
> 
> I would like to use some C functions within Python by using Pyrex but I
> don't know how to overcome one major difficulty.
> 
> I have 2 C-functions foo_A and foo_B.
> 
> foo_A read some conf/data files and put all data in one major and
> overcomplicated structure (which contain other structures and so on)
> 
> foo_B get this famous structure and other parameters to compute some
> outputs.
> 
> Basically you only have to call foo_A once (to initialize the
> structure) and after you can call foo_B as many times you want.
> 
> Now I don't know how to make pyrex wrappers to use foo_A and foo_B
> because of the structure.
> 
> One solution I'm think about is to malloc'ed some area in memory to
> contain the structure and only send to foo_B the pointer to this memory
> (and after use a cast to convert) but I also don't know how to use
> pointer with Pyrex (I mean passing one pointer to one Pyrex function to
> another).
> 
> Does someone has made something like this ? If yes I would really like
> an example...
> 




More information about the Pyrex mailing list