Hi,
On 21 May 2014 14:08, Xiaoqiang Wang <
xiaoqi...@gmail.com> wrote:
> class Foo(object):
> (...)
> @ffi.callback('void(int)')
> def callback_method_decorated(self, arg):
> print arg
> (...)
> lib.add_callback(foo.callback_method_decorated)
It is hard to do anything more explicit. From cffi's point of view,
this is the same as:
class Foo(object):
p = ffi.new("char[]", 10)
print Foo().p
I.e. cdata pointer objects don't have a __get__, so you can read them
unmodified out of instances or classes. To do anything better in this
case would require adding a __get__ that returns a copy of the
callback cdata object in which the "self" argument is stored. Such a
__get__ would have to detect the case where it is a function pointer
implemented as a callback, and not do anything in other cases (e.g. a
normal function pointer, or any other kind of cdata). It's not
impossible, but a bit of work...
More importantly, it would be very slow: it would need a new libffi
callback each time you read the attribute
"foo.callback_method_decorated". Better to try to make callbacks be
just plain global functions, which is documented and follows more
closely the C way. Any general-purpose C callback should take a
generic "void *" argument, which is convenient to pass a
ffi.new_handle().
A bientôt,
Armin.