Boost :: Python,将元组转换为Python工作,vector <tuple>没有

Sva*_*zen 27 c++ python boost tuples boost-python

我已经使用Boost :: Python了一段时间,而且一切都很好.然而昨天我试图找出为什么我认为我注册的特定类型(一个元组)在我尝试从Python访问它时给了我错误.

事实证明,虽然元组实际上已经注册,但是当试图通过std::vector包裹来访问它时,vector_indexing_suite这已经不够了.

我在想,为什么它不起作用?有没有办法让这项工作?我应该尝试用手包裹矢量吗?

以下是我的MVE:

#include <tuple>
#include <vector>

#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>

template <typename T>
struct TupleToPython {
    TupleToPython() {
        boost::python::to_python_converter<T, TupleToPython<T>>();
    }

    template<int...>
    struct sequence {};

    template<int N, int... S>
    struct generator : generator<N-1, N-1, S...> { };

    template<int... S>
    struct generator<0, S...> {
        using type = sequence<S...>;
    };

    template <int... I>
    static boost::python::tuple boostConvertImpl(const T& t, sequence<I...>) {
        return boost::python::make_tuple(std::get<I>(t)...);
    }

    template <typename... Args>
    static boost::python::tuple boostConvert(const std::tuple<Args...> & t) {
        return boostConvertImpl(t, typename generator<sizeof...(Args)>::type());
    }

    static PyObject* convert(const T& t) {
        return boost::python::incref(boostConvert(t).ptr());
    }
};

using MyTuple = std::tuple<int>;
using Tuples = std::vector<MyTuple>;

MyTuple makeMyTuple() {
    return MyTuple();
}

Tuples makeTuples() {
    return Tuples{MyTuple()};
}

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

    TupleToPython<MyTuple>();
    def("makeMyTuple", makeMyTuple);

    class_<std::vector<MyTuple>>{"Tuples"}
        .def(vector_indexing_suite<std::vector<MyTuple>>());
    def("makeTuples", makeTuples);
}
Run Code Online (Sandbox Code Playgroud)

.so通过Python 访问结果导致:

>>> print makeMyTuple()
(0,)
>>> print makeTuples()[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: No Python class registered for C++ class std::tuple<int>
>>> 
Run Code Online (Sandbox Code Playgroud)

编辑:我已经意识到,如果vector_indexing_suiteNoProxy参数设置为true,则不会发生错误.但是,我更喜欢这不是必需的,因为它使导出的类在Python中不直观.

n. *_* m. 2

TupleToPython注册 C++ 到 Python 转换器和 Python 到 C++ 转换器。这可以。

另一方面,您希望通过引用返回向量元素。但是 Python 方面没有任何东西可以作为对您的元组的引用。转换为 Python 的元组可能保存相同的值,但它与原始 C++ 元组完全分离。

看起来为了通过引用导出元组,需要为其创建一个索引套件,而不是 to/from-Python 转换器。我从来没有这样做过,也不能保证它会起作用。

以下是如何将元组公开为最小的类似元组的 Python 对象(仅具有 len() 和索引)。首先定义一些辅助函数:

template <typename A>
int tuple_length(const A&)
{
    return std::tuple_size<A>::value;
}

template <int cidx, typename ... A>
typename std::enable_if<cidx >= sizeof...(A), boost::python::object>::type
get_tuple_item_(const std::tuple<A...>& a, int idx, void* = nullptr)
{
    throw std::out_of_range{"Ur outta range buddy"};
}

template <int cidx, typename ... A, typename = std::enable_if<(cidx < sizeof ...(A))>>
typename std::enable_if<cidx < sizeof...(A), boost::python::object>::type
get_tuple_item_(const std::tuple<A...>& a, int idx, int = 42)
{
    if (idx == cidx)
        return boost::python::object{std::get<cidx>(a)};
    else
        return get_tuple_item_<cidx+1>(a, idx);
};

template <typename A>
boost::python::object get_tuple_item(const A& a, int index)
{
    return get_tuple_item_<0>(a, index);
}
Run Code Online (Sandbox Code Playgroud)

然后公开特定的元组:

using T1 = std::tuple<int, double, std::string>;
using T2 = std::tuple<std::string, int>;

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

    class_<T1>("T1", init<int, double, std::string>())
      .def("__len__", &tuple_length<T1>)
      .def("__getitem__", &get_tuple_item<T1>);

    class_<T2>("T2", init<std::string, int>())
      .def("__len__", &tuple_length<T2>)
      .def("__getitem__", &get_tuple_item<T2>);
}
Run Code Online (Sandbox Code Playgroud)

请注意,这些准元组与真正的 Python 元组不同,是可变的(通过 C++)。由于元组的不变性,通过转换器导出NoProxy看起来是一个可行的替代方案。