Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing cython function to cython function

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?

like image 712
Thomas Johnson Avatar asked Oct 31 '25 23:10

Thomas Johnson


1 Answers

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)
like image 110
mkimball Avatar answered Nov 03 '25 12:11

mkimball



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!