python - 将闭包从 Cython 传递到 C++

标签 python c++ cython

我有一个接受回调的 C++ 函数,如下所示:

void func(std::function<void(A, B)> callback) { ... }

我想通过给它一个闭包从 Cython 调用这个函数,也就是说,如果我从 C++ 调用它,我会用 lambda 做的事情。如果这是一个 C 函数,它将有一些额外的 void* 参数:

typedef void(*callback_t)(int, int, void*);

void func(callback_t callback, void *user_data) {
    callback(1, 2, user_data);
}

然后我只需将 PyObject* 作为 user_data 传递(有更详细的 example here )。

有没有办法用 C++ 的方式做更多的事情,而不必求助于显式的 user_data

最佳答案

我相信您的目标是将可调用的 Python 对象传递给接受 std::function 的对象。您需要创建一些 C++ 代码来实现它,但这相当简单。

首先尽可能简单地定义“accepts_std_function.hpp”以提供说明性示例:

#include <functional>
#include <string>

inline void call_some_std_func(std::function<void(int,const std::string&)> callback) {
    callback(5,std::string("hello"));
}

技巧是创建一个包含 PyObject* 并定义 operator() 的包装类。定义 operator() 允许将其转换为 std::function。大部分类(class)只是重新计算。 “py_obj_wrapper.hpp”:

#include <Python.h>
#include <string>
#include "call_obj.h" // cython helper file

class PyObjWrapper {
public:
    // constructors and destructors mostly do reference counting
    PyObjWrapper(PyObject* o): held(o) {
        Py_XINCREF(o);
    }

    PyObjWrapper(const PyObjWrapper& rhs): PyObjWrapper(rhs.held) { // C++11 onwards only
    }

    PyObjWrapper(PyObjWrapper&& rhs): held(rhs.held) {
        rhs.held = 0;
    }

    // need no-arg constructor to stack allocate in Cython
    PyObjWrapper(): PyObjWrapper(nullptr) {
    }

    ~PyObjWrapper() {
        Py_XDECREF(held);
    }

    PyObjWrapper& operator=(const PyObjWrapper& rhs) {
        PyObjWrapper tmp = rhs;
        return (*this = std::move(tmp));
    }

    PyObjWrapper& operator=(PyObjWrapper&& rhs) {
        held = rhs.held;
        rhs.held = 0;
        return *this;
    }

    void operator()(int a, const std::string& b) {
        if (held) { // nullptr check 
            call_obj(held,a,b); // note, no way of checking for errors until you return to Python
        }
    }

private:
    PyObject* held;
};

此文件使用一个非常短的 Cython 文件来完成从 C++ 类型到 Python 类型的转换。 “call_obj.pyx”:

from libcpp.string cimport string

cdef public void call_obj(obj, int a, const string& b):
    obj(a,b)

然后您只需要创建包装这些类型的 Cython 代码。编译此模块并调用 test_func 来运行它。 ("simple_version.pyx":)

cdef extern from "py_obj_wrapper.hpp":
    cdef cppclass PyObjWrapper:
        PyObjWrapper()
        PyObjWrapper(object) # define a constructor that takes a Python object
             # note - doesn't match c++ signature - that's fine!

cdef extern from "accepts_std_func.hpp":
    void call_some_std_func(PyObjWrapper) except +
            # here I lie about the signature
            # because C++ does an automatic conversion to function pointer
            # for classes that define operator(), but Cython doesn't know that


def example(a,b):
    print(a,b)

def test_call():
    cdef PyObjWrapper f = PyObjWrapper(example)

    call_some_std_func(f)

上面的版本可以工作,但有一些限制,如果你想用不同的 std::function 特化来做到这一点,你需要重写其中的一些(以及从 C++ 到 Python 类型的转换)不自然地适用于模板实现)。一个简单的解决方法是使用 Boost Python 库 object 类,它有一个模板化的 operator()。这是以引入额外的库依赖性为代价的。

首先定义标题“boost_wrapper.hpp”以简化the conversion from PyObject* to boost::python::object

#include <boost/python/object.hpp>

inline boost::python::object get_as_bpo(PyObject* o) {
    return boost::python::object(boost::python::handle<>(boost::python::borrowed(o)));
}

然后您只需要使用 Cython 代码来包装此类(“boost_version.pyx”)。再次调用 test_func

cdef extern from "boost_wrapper.hpp":
    cdef cppclass bpo "boost::python::object":
        # manually set name (it'll conflict with "object" otherwise
        bpo()

    bpo get_as_bpo(object)


cdef extern from "accepts_std_func.hpp":
    void call_some_std_func(bpo) except + # again, lie about signature

def example(a,b):
    print(a,b)

def test_call():
    cdef bpo f = get_as_bpo(example)

    call_some_std_func(f)

一个“setup.py”

from distutils.core import setup, Extension
from Cython.Build import cythonize

extensions = [
    Extension(
           "simple_version",                       # the extension name
           sources=["simple_version.pyx", "call_obj.pyx" ],
           language="c++",                        # generate and compile C++ code
      ),
    Extension(
           "boost_version",                       # the extension name
           sources=["boost_version.pyx"],
           libraries=['boost_python'],
           language="c++",                        # generate and compile C++ code
      )
    ]

setup(ext_modules = cythonize(extensions))

(最后一个选项是使用 ctypes 从 Python 可调用对象生成 C 函数指针。参见 Using function pointers to methods of classes without the gil(答案的下半部分)和 http://osdir.com/ml/python-cython-devel/2009-10/msg00202.html。我不打算在这里详细介绍。)

关于python - 将闭包从 Cython 传递到 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42476145/

相关文章:

python - cython-distutils 与 cmake : linking against libpython?

python - 将字节串发送到串口设备

python - 将重复行转换为独立列

python - 后台运行python,需要先获取用户输入

c++ - 无法从 C++ DLL 导出函数

python - 使用 Cython 编译时找不到库文件

python - 使用 Python setuptools 将 Cython 编译的 pyd 文件放入其原始文件夹中?

python - 如何使用索引更新张量的元素?

python - OpenCV 3.1 绘制轮廓 '(-215) npoints > 0'

java.util.concurrent 与 Boost Threads 库