Function pointer as part of a C struct
Alexander Eisenhuth <AlexanderEisenhuth <at> web.de>
2013-05-13 09:50:51 GMT
Hello everybody,
I want to use a callback as part of a C struct with read/write access.
Is this possible? The following describes the problem. And if you can
help, how do I connect a python function to the callback?
Any help welcome!
Regards
Alexander
---- stderr of compiling Device.pyx ---
Error compiling Cython file:
------------------------------------------------------------
...
def __setattr__(self, name, value):
print "DeviceDescriptor_.__setattr__", name, value
if name == "deviceNumber":
self._device_descriptor.deviceNumber = <Uint8> value
elif name == "deviceMeasurement":
self._device_descriptor.deviceMeasurement =
<DeviceMeasureFunc> value
^
------------------------------------------------------------
Device.pyx:55:56: Python objects cannot be cast to pointers of primitive
types
----- Device.h ----
typedef ErrorStatus (*DeviceMeasureFunc)(Uint32*);
typedef struct DeviceDescriptorTag
{
Uint8 deviceNumber;
DeviceMeasureFunc deviceMeasurement;
} DeviceDescriptor;
------- Device.pyx ---
cdef extern from "Device.h":
ctypedef ErrorStatus (*DeviceMeasureFunc)(Uint32*)
ctypedef struct DeviceDescriptor_c "DeviceDescriptor":
Uint8 deviceNumber
DeviceMeasureFunc deviceMeasurement
cdef class DeviceDescriptor:
cdef DeviceDescriptor_c _device_descriptor
def __getattr__(self, name):
ret_val = None
if name == "deviceNumber":
ret_val = self._device_descriptor.deviceNumber
else:
self.__raise_attrib_not_found(name)
return ret_val
def __setattr__(self, name, value):
if name == "deviceNumber":
self._device_descriptor.deviceNumber = <Uint8> value
elif name == "deviceMeasurement":
self._device_descriptor.deviceMeasurement =
<DeviceMeasureFunc> value
else:
self.__raise_attrib_not_found(name)
def __raise_attrib_not_found(self, name):
raise AttributeError("Attribute %s not in %s" % (name, type(self)))
--
--
---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.