Boost Python 找不到 std::string 的 to_python 转换器

发布于 2024-11-14 03:05:03 字数 1485 浏览 2 评论 0原文

所以,我试图创建一个 to_python 转换器,它允许我从公开的函数返回 boost::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:找不到 C++ 类型的 to_python (按值)转换器:std::string”。我知道 C++ 能够将字符串转换为 python,因为我公开的大多数函数都返回字符串。为什么 boost::python::to_python_value 无法识别它,我如何利用它具有的任何转换器?

通过更改为以下内容来修复(基于 此文章):

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>();
  }
};

现在只做另一个版本,以便它更干净并且工作得更好。

So, I am trying to create a to_python converter that will allow me to return a boost::optional from an exposed function and have it treated as T if the optional is set and None if not. Based on a post I found on C++Sig, I wrote the following code.

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>();
  }
};

As far as I can tell, it works for converting the optionals, but python throws the following exception "TypeError: No to_python (by-value) converter found for C++ type: std::string". I know that C++ is able to convert strings to python since most of my exposed functions return strings. Why doesn't boost::python::to_python_value recognize it, and how can I utilize whatever converter it has?

Fixed by changing to the following (based on 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>();
  }
};

Now to just do the other version so that it is cleaner and works better.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肩上的翅膀 2024-11-21 03:05:03

好的,这里是基于原始 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
      );
    }
  }
};

Ok here is the entire to and from optional converter based on the original C++ sig post but rewritten to use the high level boost.python API (sorry about the weird spacing).

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
      );
    }
  }
};
一桥轻雨一伞开 2024-11-21 03:05:03

上面的代码有一些拼写错误 - 这是更正的版本:

#include <boost/noncopyable.hpp>
#include <boost/optional.hpp>
#include <boost/python.hpp>

template<typename T>
struct python_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 python_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
      );
    }
  }
};

There are a few typos in the above code - here is the corrected version:

#include <boost/noncopyable.hpp>
#include <boost/optional.hpp>
#include <boost/python.hpp>

template<typename T>
struct python_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 python_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
      );
    }
  }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文