Can Cython be used to define C-callable variadic functions?

57 Views Asked by At

I'm writing a Python library for testing DLLs that implement a particular C API. This library also exposes functionality to the native DLLs through callback functions fetched at runtime. There's a specific function I'd like to implement, and it's declared like so:

enum retro_log_level; // definition omitted for brevity

// typedef for a printf-like function
typedef void (RETRO_CALLCONV *retro_log_printf_t)(enum retro_log_level level, const char *fmt, ...);

struct retro_log_callback
{
   // defined in Python, called by C
   retro_log_printf_t log;
};

The problem is that retro_log_printf_t is variadic, and therefore cannot be implemented with ctypes. I didn't design or implement this API, so I can't change it.

One idea I had was to implement retro_log_printf_t in C (or similar) so that I can bypass the linked ctypes limit. I'm considering Cython for this, but I don't know if that's possible.

So my question is this: How can I use Cython to declare and implement a variadic function that can be called from C and is compatible with the aforementioned retro_log_printf_t?

0

There are 0 best solutions below