Greetings,
I'm trying to wrap a function in a C library as a compiled C Python
module. Everything is going great, but I've hit a snag. There's a
function in the form of this:
First the typedef:
typedef void(*FPtr_Devi ceMessageHandle r) (const DeviceMessage, const
char*);
Then the actual function prototype:
FPtr_DeviceMess ageHandler
RegisterDeviceM essageHandler(F Ptr_DeviceMessa geHandler);
Whenever this USB device I'm using generates a message, it's then sent
to that function whose reference you passed to
RegisterDeviceM essageHandler() . Here's an example:
void testfunc() {
printf("test");
}
....on to main()
RegisterDeviceM essageHandler(& testfunc)
So I've defined a similar function on my C module to do just this,
using the passed function reference to allow the device to send
messages to a Python function of the user's choice. I'm just not sure
how to do this, and the tutorials I've been digging through don't
offer any help. I've found some functions that look promising, but
can't figure out how to cast the function reference from Python into
something the C RegisterDevice. .. function can handle. Here's what
I've got:
static PyObject *
Py1_RegisterDev iceMessageHandl er(PyObject *self, PyObject *args)
{
PyObject *handle, *parsed;
if(!PyArg_Parse Tuple(args, "O", handle)) {
return NULL;
}
parsed = PyMethod_Functi on(handle);
I1_RegisterDevi ceMessageHandle r(PyMethod_Func tion(handle));
Py_RETURN_TRUE;
} // end Py1_RegisterDev iceMessageHandl er()
This fails since PyMethod_Functi on returns a PyObject. Is there a way
to cast this to something generic? Casting to (void*) didn't seem to
work.
Thanks in advance!
I'm trying to wrap a function in a C library as a compiled C Python
module. Everything is going great, but I've hit a snag. There's a
function in the form of this:
First the typedef:
typedef void(*FPtr_Devi ceMessageHandle r) (const DeviceMessage, const
char*);
Then the actual function prototype:
FPtr_DeviceMess ageHandler
RegisterDeviceM essageHandler(F Ptr_DeviceMessa geHandler);
Whenever this USB device I'm using generates a message, it's then sent
to that function whose reference you passed to
RegisterDeviceM essageHandler() . Here's an example:
void testfunc() {
printf("test");
}
....on to main()
RegisterDeviceM essageHandler(& testfunc)
So I've defined a similar function on my C module to do just this,
using the passed function reference to allow the device to send
messages to a Python function of the user's choice. I'm just not sure
how to do this, and the tutorials I've been digging through don't
offer any help. I've found some functions that look promising, but
can't figure out how to cast the function reference from Python into
something the C RegisterDevice. .. function can handle. Here's what
I've got:
static PyObject *
Py1_RegisterDev iceMessageHandl er(PyObject *self, PyObject *args)
{
PyObject *handle, *parsed;
if(!PyArg_Parse Tuple(args, "O", handle)) {
return NULL;
}
parsed = PyMethod_Functi on(handle);
I1_RegisterDevi ceMessageHandle r(PyMethod_Func tion(handle));
Py_RETURN_TRUE;
} // end Py1_RegisterDev iceMessageHandl er()
This fails since PyMethod_Functi on returns a PyObject. Is there a way
to cast this to something generic? Casting to (void*) didn't seem to
work.
Thanks in advance!
Comment