ctypes: pointer to method
Nobody
nobody at nowhere.com
Thu Aug 5 15:32:25 EDT 2010
On Thu, 05 Aug 2010 10:50:21 -0700, Martin Landa wrote:
> is it possible to pass pointer to a method using ctypes.
I don't know about methods, but it works for functions.
> Sample code:
>
> ...
> G_set_error_routine(byref(self._print_error))
This won't work; you have to be more explicit, e.g.:
errtype = CFUNCTYPE(c_int, POINTER(c_char), POINTER(c_int))
errfunc = errtype(print_error)
G_set_error_routine(errfunc)
NOTE: be sure to keep a reference to the wrapper, as ctypes doesn't hold
references itself. IOW, you can't replace the above with:
errtype = CFUNCTYPE(c_int, POINTER(c_char), POINTER(c_int))
G_set_error_routine(errtype(print_error))
If you do this, the wrapper is eligible for garbage collection as soon as
G_set_error_routine() returns, and will probably have vanished by the time
that G_fatal_error() gets called.
For more information see:
http://docs.python.org/library/ctypes.html#callback-functions
More information about the Python-list
mailing list