Hi,

welcome to Cython.

Martin Gysel wrote:
> I'm quite new to cython and try to bind some c code to python. Now I'm
> not sure if I understood the cython doc correctly since what I'm trying
> do do doesn't work :-(
> It looks like this:
> 
> cdef class Cobj:
>     cdef a_c_struct_t *loop
> 
>     def __init__(self):
>         self.loop = c_lib_call()
>         c_lib_call_reg_callback(<c_cb_t>self.callback, NULL)
> 
>     cdef void callback(self, void *userdata) with gil:
>        # but here, self.loop seems to be NULL instead of something
>        # to test this:
>        # if self.loop == NULL: print "isNull"
>        doSomething(self.loop)
> 
> in the callback self.loop is NULL but in the init it wasn't. is it
> possible to access self and its members in a c callback?

Yes. However, since "callback" is a C function (and not a Python method),
it cannot remember what instance it belongs to. You will have to pass along
the pointer to its Python object yourself. This is what the "userdata"
pointer is there for. It should work to do this:


  cdef class Cobj:
      cdef a_c_struct_t *loop

      def __init__(self):
          self.loop = c_lib_call()
          c_lib_call_reg_callback(<c_cb_t>self.callback, <void*>self)

      cdef void callback(self) with gil:
         if self.loop == NULL: print "isNull"
         doSomething(self.loop)

There is also a callback example under "Demos/callback/" in the Cython sources.

Stefan

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to