Re: [ctypes-users] judge a return callback is NULL, how?
Brought to you by:
theller
From: Lenard L. <le...@te...> - 2007-12-27 21:34:20
|
oyster wrote: > that is the defination in C: > typedef struct Ihandle_ Ihandle; > typedef int (*Icallback)(Ihandle*); > Icallback IupGetFunction (const char *name); > > and this is in the user application in C > int btn_pause_cb(void) > { > if(!IupGetFunction(IUP_IDLE_ACTION)) > IupSetFunction(IUP_IDLE_ACTION, (Icallback) idle_cb); > else > IupSetFunction(IUP_IDLE_ACTION, NULL); > > return IUP_DEFAULT; > } > > > in my python interface, even I call IupGetFunction(IUP_IDLE_ACTION) > for the first time, I get a value such as > IupGetFunction(IUP_IDLE_ACTION)= <CFunctionType object at 0x00A3D378> > obviously, it is always True > > so, how can I judge whether a returned value( which is a function > type) is NULL or not? Thank you > > A null pointer evaluates to the boolean value False. Other pointers are True. So if not IupGetFunction(IUP_IDLE_ACTION): ..... > BTW: I think I can modify the Python code to use a global variant to > index wheter we have called btn_pause_cb, but I think it is not a good > way. > > Callbacks need not be functions. Any callable Python object will do. class SomeState(object): def __init__(self): self.was_called = False def btn_pause_cb(self): ..... self.was_called = True return IUP_DEFAULT state = SomeState() Then create a ctypes callback for state.btn_pause_cb, keep a Python reference to the callback and the state object, and register the callback. Whenever it is called then state.was_called becomes True. Good Python programming practice usually involves wrapping C structures as classes and calling the C functions that manipulate them from the class's methods. -- Lenard Lindstrom <le...@te...> |