jeudi 7 juin 2018

How to convert a 'managedbuffer' into a Callable in Python

I have written a Python Wrapper for a C++14 library using SWIG. Within the C++ API I can register std::functions as callbacks.

I have a SWIG typemap for std::function's to pass a lambda expression which invokes the Python callback:

auto callback = [$input](auto&&... params) {
    PyGILState_STATE state = PyGILState_Ensure();

    PyObject* result = PyObject_CallFunctionObjArgs($input,makePyObject(std::forward<decltype(params)>(params))..., NULL);
    const int retVal = PyObject_IsTrue(result);

    Py_DECREF(result);
    PyGILState_Release(state);
    return retVal == 1;
};

When I run a test script, the following Python expression works fine:

callback = lambda a,b: self.doStuff(a,b)
self.cppInterface.registerFunc(callback)

This expression however does not work:

self.cppInterface.registerFunc(lambda a,b: self.doStuff)

Why is that? How do I allow both Python expressions?

Aucun commentaire:

Enregistrer un commentaire