C++ List 到 Python List 的转换

当我们编写 C++ 库的封装器通常涉及使用一种跨语言的接口技术,比如使用C接口或者使用特定的跨语言库,比如SWIG(Simplified Wrapper and Interface Generator)或者Pybind11。这里我将简要介绍如何使用Pybind11来封装一个C++库,以便在Python中使用。

1、问题背景

在编写 C++ 库的封装器时,需要将 C++ 中的 list 容器转换为 Python 中的 list。由于 C++ 库不能被修改,因此希望避免使用 vector 来替代 list。

为了更好地理解这种情况,使用 list 作为代理来注册从 C++ 到 Python 的转换(只读)。当前的实现可以编译,Python 可以正常导入,并且可以创建对象,但是在调用数据成员时会出错。

Python 控制台输出:

python 复制代码
In [1]: import my_list

In [2]: x = my_list.Bob()

In [3]: x.foos
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-2f015d13a87d> in <module>()
----> 1 x.foos

TypeError: No Python class registered for C++ class std::list<int, std::allocator<int> >

C++ 文件:

c++ 复制代码
#include <list>
#include <boost/python.hpp>

#include <boost/foreach.hpp>
#ifndef FOREACH
    #define FOREACH BOOST_FOREACH
#endif

using namespace std;
using namespace boost::python;

template<typename T>
struct list_to_list
{
    static PyObject* convert(const std::list<T>& src)
    {
        boost::python::list result;
        FOREACH (const T& val, src)
        {
            result.append(val);
        }

        return incref(result.ptr());
    }
};

struct Bob
{
    std::list<int> foos;
};

BOOST_PYTHON_MODULE(my_list)
{
    using namespace boost::python;

    to_python_converter<std::list<int>, list_to_list<int> >();

    class_<Bob>("Bob")
        .def_readonly("foos", &Bob::foos)
    ;
}

2、解决方案

如常见问题解答中所述,通过 def_readonly()def_readwrite()add_property() 暴露的属性,使用默认策略不会使用自定义转换器。要解决此问题,请用 add_property() 替换 def_readonly()def_readwrite(),提供一个 boost::python::return_value_policy,其类型为 boost::python::return_by_value

在原始代码中,它应该是:

c++ 复制代码
BOOST_PYTHON_MODULE(my_list)
{
  using namespace boost::python;

  to_python_converter<std::list<int>, list_to_list<int> >();

  class_<Bob>("Bob")
    .add_property("foos", make_getter(&Bob_foos,
        return_value_policy<return_by_value>())) 
    ;
}

完整的示例如下:

c++ 复制代码
#include <list>
#include <boost/assign/list_of.hpp>
#include <boost/foreach.hpp>
#include <boost/python.hpp>

namespace python = boost::python;

/// @brief Type to convert from an iterable to a Python list.
template <typename T>
struct list_to_list
{
  static PyObject* convert(const std::list<T>& container)
  {
    python::list result;
    BOOST_FOREACH(const T& value, container)
      result.append(value);
    return python::incref(result.ptr());
  }
};

/// @brief mockup type.
struct Spam
{
  Spam()
  {
    foos = boost::assign::list_of(1)(2)(3)(5);
  }

  std::list<int> foos;
};

BOOST_PYTHON_MODULE(example)
{
  // Enable std::list<int> to Python list conversion.
  python::to_python_converter<std::list<int>, list_to_list<int> >();

  python::class_<Spam>("Spam")
    .add_property("foo", python::make_getter(&Spam::foos,
          python::return_value_policy<python::return_by_value>()))
    ;
}

相应的用法:

python 复制代码
>>> import example
>>> spam = example.Spam()
>>> spam.foo
[1, 2, 3, 5]

上面这个就是一个简单的例子,展示了如何使用Pybind11来封装一个C++库。具体的细节可能会因为我们的C++库的复杂性而有所不同,但这应该可以帮助你入门。如果有更多的问题可以留言讨论。

相关推荐
wuminyu19 分钟前
专家视角看Java字节码加载与存储指令机制
java·linux·c语言·jvm·c++
段一凡-华北理工大学22 分钟前
【高炉炼铁领域炉温监测、预警、调控智能体设计与应用】~系列文章08:多模态数据融合:让数据更聪明
人工智能·python·高炉炼铁·ai赋能·工业智能体·高炉炉温
万粉变现经纪人26 分钟前
如何解决 pip install llama-cpp-python 报错 未安装 CMake/Ninja 或 CPU 不支持 AVX 问题
开发语言·python·开源·aigc·pip·ai写作·llama
清风明月一壶酒38 分钟前
OpenClaw自动处理Word文档全流程
开发语言·c#·word
其实防守也摸鱼42 分钟前
CTF密码学综合教学指南--第五章
开发语言·网络·笔记·python·安全·网络安全·密码学
木喃的井盖1 小时前
无锁队列细节
c++·工程
王老师青少年编程1 小时前
csp信奥赛C++高频考点专项训练之字符串 --【字符串基础】:输出亲朋字符串
c++·字符串·csp·高频考点·信奥赛·专项训练·输出亲朋字符串
WBluuue2 小时前
数据结构与算法:莫队(一):普通莫队与带修莫队
c++·算法
callJJ2 小时前
Spring Data Redis 两种编程模型详解:同步 vs 响应式
java·spring boot·redis·python·spring
小郑加油2 小时前
python学习Day12:pandas安装与实际运用
开发语言·python·学习