[Pyrex] "Dynamic" callbacks?
Greg Ewing
greg.ewing at canterbury.ac.nz
Tue Mar 22 06:43:33 CET 2005
Timothy Toroni wrote:
>
> Hello all,
>
> I've got a C library with timers which provide callbacks. I can
> successfully use the callback as per the demo find cheese example.
> However it uses a cdef function as the actual callback, and suppose you
> can't use a "userdata" data passback, is it possible to "CLASSify
> this?" For example,
>
> cdef CLibFunctionRegisterCallback( void (*func)(void), timeout )
>
> cdef class Timer:
> def __init__(self, python_callback_function, timeout):
> self.python_callback_function = python_callback_function
> CLibFunctionRegisterCallback( <void *> self.c_callback, timeout)
> cdef void c_callback(self):
> self.python_callback_function()
No, that won't work, because a C method expects 'self' as the
first parameter. There's no way you can make it conform to the
signature expected by the library.
The best you can do in a portable way is have a global variable
somewhere that holds a Python function, and install a C callback
that calls that function. This means you can only have one Python
callback function installed at a time, but that's all the C library's
API provides anyway.
Someone mentioned earlier that ctypes provides some way of
creating the C equivalent of a bound method, but I don't
know any details.
--
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury, | A citizen of NewZealandCorp, a |
Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. |
greg.ewing at canterbury.ac.nz +--------------------------------------+
More information about the Pyrex
mailing list