I’ve read many documents on how to create a method for a type using additional data. I want a feature similar to PyGetSetDef.closure that allows passing user data when there is no good way to do it otherwise.
struct PyMethodDef {
const char *ml_name; /* The name of the built-in function/method */
PyCFunction ml_meth; /* The C function that implements it */
int ml_flags; /* Combination of METH_xxx flags, describing expected args */
const char *ml_doc; /* The __doc__ attribute, or NULL */
};
Although I can access some data from self
:
typedef PyObject *(*PyCFunction)(PyObject */*self*/, PyObject */*arguments*/);
I still need to use many hacks to access method-related data. My situation is: I have an array that stores native function pointers and userdata:
struct NativeMethodDef {
void* NativeImpl;
void* UserData;
}
And I’m creating a PyMethodDef
array to map these. It’s difficult to create the function without using lambda capture.
Desired Feature
struct PyMethodDef {
const char *ml_name; /* The name of the built-in function/method */
PyCFunction ml_meth; /* The C function that implements it */
int ml_flags; /* Combination of METH_xxx flags, describing expected args */
++ void *closure; /* Optional user data pointer for PyCFunction. */
const char *ml_doc; /* The __doc__ attribute, or NULL */
};
typedef PyObject *(*PyCFunction)(PyObject */*self*/, PyObject */*arguments*/, void */*closure*/);