c++ - Boost Python没有为std::string找到to_python转换器

标签 c++ boost-python

所以,我正在尝试创建一个 to_python 转换器,它允许我从公开的函数返回一个 boost::optional ,如果设置了 optional 则将其视为 T ,否则将其视为 None 。基于我在 C++Sig 上找到的帖子,我写了如下代码。

template<typename T>
struct optional_ : private boost::noncopyable {
  struct conversion {
    static PyObject* convert(boost::optional<T> const& value) {
      if (value) {
        return boost::python::to_python_value<T>()(*value);
      }
      Py_INCREF(Py_None);
      return Py_None;
    }
  };
  explicit optional_() {
    boost::python::to_python_converter<boost::optional<T>, conversion>();
  }
};

据我所知,它适用于转换选项,但 python 抛出以下异常“TypeError: No to_python (by-value) converter found for C++ type: std::string”。我知道 C++ 能够将字符串转换为 python,因为我公开的大多数函数都返回字符串。为什么 boost::python::to_python_value 不能识别它,我该如何利用它所具有的任何转换器?

已通过更改为以下内容进行修复(基于 this article):

template<typename T>
struct optional_ : private boost::noncopyable {
  struct conversion {
    static PyObject* convert(boost::optional<T> const& value) {
      using namespace boost::python;
      return incref((value ? object(*value) : object()).ptr());
    }
  };
  explicit optional_() {
    boost::python::to_python_converter<boost::optional<T>, conversion>();
  }
};

现在只做另一个版本,这样它更干净,效果更好。

最佳答案

好的,这里是基于原始 C++ sig 帖子但重写为使用高级 boost.python API 的整个往返可选转换器(对不起,奇怪的间距)。

template<typename T>
struct optional_ : private boost::noncopyable
{
  struct conversion :
    public boost::python::converter::expected_from_python_type<T>
  {
    static PyObject* convert(boost::optional<T> const& value) {
      using namespace boost::python;
      return incref((value ? object(*value) : object()).ptr());
    }
  };

  static void* convertible(PyObject *obj) {
    using namespace boost::python;
    return obj == Py_None || extract<T>(obj).check() ? obj : NULL;
  }

  static void constructor(PyObject *obj,
       boost::python::converter::rvalue_from_python_stage1_data *data)
  {
    using namespace boost::python;
    void *const storage =
      reinterpret_cast<
        converter::rvalue_from_python_storage<boost::optional<T> >*
      >(data)->storage.bytes;
    if(obj == Py_None) {
      new (storage) boost::optional<T>();
    } else {
      new (storage) boost::optional<T>(extract<T>(obj));
    }
    data->convertible = storage;
  }

  explicit optional_() {
    using namespace boost::python;
    if(!extract<boost::optional<T> >(object()).check()) {
      to_python_converter<boost::optional<T>, conversion, true>();
      converter::registry::push_back(
        &convertible,
        &constructor,
        type_id<boost::optional<T> >(),
        &conversion::get_pytype
      );
    }
  }
};

关于c++ - Boost Python没有为std::string找到to_python转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6274822/

相关文章:

python - 使用 boost::python 时将 python.io 对象转换为 std::istream

python - 提取并转换 boost::python::list 的列表元素

python - boost .Python : inheret from C++ object in python: Cannot add to C++ base class list

python - Boost Python 从纯虚类继承

c++ - 需要帮助在 C++ 中进行多线程

c++ - 烦人的 Ctrl+M 问题解析 python 文件

python - 使用 pipe() 和 fdopen() 将数据从 Python 脚本传递到 Windows 中的 C++ 应用程序

c++ - i2c smbus 过滤器函数破坏变量

c++ - 读取数据时如何处理不同版本的格式?

c++ - 有没有办法知道Qt中哪个信号触发了插槽?