I am embedding Python in a C++ library which I am making. I would like users to be able to pass C functions in the form of function pointers PyObject* (fpFunc*)(PyObject*,PyObject*);
so that I can use those functions in the embedded Python.
So I have a function pointer and I know that it is possible to put this function as a module's method using a PyMethodDef
struct and passing it to Py_InitModule("module", ModMethods);
and thus obtaining a PyObject* module
which I can easily grab functions from.
But, what I would really appreciate is to be able to create this function on the fly without necessarily recreating another module each time.
I have looked into the Python docs, and some of the Python headers to think about a hacky way of doing this without real success... but I'm wondering if's there's a more conventional way of doing this.
From what I understood, every function belongs to a module, even __builtin__
, so I guess I would require at least on module.
Any idea on how to achieve this?