Function References

Carsten Haese carsten.haese at
Thu Jul 31 19:31:55 CEST 2008

squishywaffle at wrote:
> 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_DeviceMessageHandler) (const DeviceMessage, const
> char*);
> Then the actual function prototype:
> FPtr_DeviceMessageHandler
> RegisterDeviceMessageHandler(FPtr_DeviceMessageHandler);
> [...]
> [I] can't figure out how to cast the function reference from Python into
> something the C RegisterDevice... function can handle.

Mere casting won't do the trick. The function reference from Python is 
basically just an object that contains byte-code to be executed by the 
Python Virtual Machine. There is no way you can magically transform that 
into a C function by casting the Python object pointer to anything.

You'll need a write a C function that will call a given Python function, 
and then you need to supply the pointer to that C function as your 
DeviceMessageHandler callback function, and somehow communicate to your 
C function which Python function it should call.

Good luck.

Carsten Haese

More information about the Python-list mailing list