How to pass Python object to callback function?

Nikolaus Rath <[email protected]>
Newsgroups gmane.comp.python.ctypes
Message-ID <[email protected]>
Hello,

How can I pass an arbitrary Python object to a callback function?

Suppose I have this C library:

typedef void cb_fun_t(void*);
void do_callback(cb_fun_t cb_fun, void* ptr) {
    cb_fun(ptr);
}

and I want to call it from Python with f being a Python function as
well:

cbfun_t = CFUNCTYPE(None, c_void_p)
lib.do_callback.argtypes = [cbfun_t, c_void_p]
lib.do_callback.restype = None

def callback(obj):
    print "callback received %s" % repr(obj)

How do I do that? Calling lib.do_callback(cfun_t(callback),
POINTER([1,2])) just raises a TypeError...


Best,

   -Nikolaus

-- 
 »Time flies like an arrow, fruit flies like a Banana.«

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C


------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.