I have a Cython function where I would like to pass in a serialization function as a parameter:
cdef my_serializer(serialization_func, data):
return serialization_func(data)
All the serializers I'm going to be dealing with (msgpack, ujson) are also C/cython functions. What's the proper way to declare serialization_func so Cython can do early binding?
This works and is reasonably readable:
ctypedef void (*SERIALIZATION_FUNC)(char *data)
cdef void my_serializer(SERIALIZATION_FUNC func, char *data):
func(data)
Alternately, if you really want to use python objects as parameters and return values
ctypedef object (*SERIALIZATION_FUNC)(object data)
cdef my_serializer(SERIALIZATION_FUNC func, data):
return func(data)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With