On Thu, Jul 23, 2009 at 7:11 PM, Richard Clarke<[email protected]> wrote:
>
> If we consider Bar to be the encapsulating class for the foo_t struct,
> in my case I need access to this foo_t struct in other classes.
That's common use case... And Cython supports that almost for free..
> Using
> Lisandro's Bar class as above with an extra method, class Qux
> demonstrates how to obtain the foo_t pointer.
No need at all of such hackery..
> This in turn could be
> passed in to other c library function calls etc.
>
you can do this:
def py_function1(obj):
cdef Bar bar = obj
some_c_function(bar.foo) # pass foo_t* to a C function
of even this:
def py_function1(Bar bar):
some_c_function(bar.foo)
So in Cython code, cdef members are "public" and you can access them
as long as you use a "cdef" typeded variable. However, remember that
these cdef member are not accessible from Python side, outside Cython
code.
> // foo.pyx
>
> cdef extern from "stdlib.h":
> ctypedef unsigned long size_t
> void free(void *ptr)
>
> cdef extern from "foo.h":
> ctypedef struct foo_t:
> int fd
>
> foo_t* init()
>
> cdef class Bar:
> cdef foo_t *foo
>
> def __cinit__(self):
> self.foo = self.__open()
> self.foo.fd = 1
>
> def __dealloc__(self):
> if self.foo != NULL:
> free(self.foo)
>
> cdef foo_t* __open(self):
> cdef foo_t *f
> f = init()
> return f
>
> cdef foo_t* get_foo(self):
> return self.foo
>
> class Qux:
> def __init__(self):
> global qux
> qux = Bar()
>
> def something(self):
> cdef foo_t* _foo
> _foo = Bar.get_foo(qux)
> if _foo != NULL:
> #Do something with the struct
>
> Thanks to both of you for your help,
> Richard.
>
>>>
>>> --
>>> Lisandro Dalcín
>>> ---------------
>>> Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
>>> Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
>>> Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
>>> PTLC - Güemes 3450, (3000) Santa Fe, Argentina
>>> Tel/Fax: +54-(0)342-451.1594
>>> _______________________________________________
>>> Cython-dev mailing list
>>> [email protected]
>>> http://codespeak.net/mailman/listinfo/cython-dev
>>
>> _______________________________________________
>> Cython-dev mailing list
>> [email protected]
>> http://codespeak.net/mailman/listinfo/cython-dev
>>
> _______________________________________________
> Cython-dev mailing list
> [email protected]
> http://codespeak.net/mailman/listinfo/cython-dev
>
--
Lisandro Dalcín
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev