迭代 boost::python 向量时出现 TypeError
TypeError when iterating over a boost::python vector
我遇到了一个非常相似的问题:
unexpected result iterating over a boost::python vector_indexing_suite
我有以下 C++ 代码:
#include <vector>
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
using namespace boost::python;
class Composite {
public:
std::string name;
std::vector<Composite*>& getChildren() {
return children;
};
std::vector<Composite*> children;
};
typedef std::vector<Composite*> CompositeArray;
BOOST_PYTHON_MODULE(coin)
{
class_<CompositeArray>("CompositeArray")
.def(vector_indexing_suite<CompositeArray, true>());
class_<Composite>("Composite", "Composite*", init<>())
.def("getChildren", &Composite::getChildren, return_internal_reference<>())
.def_readwrite("name", &Composite::name, "str")
;
}
以及以下 Python 代码:
import coin
gna = coin.Composite()
gna.name = "test"
gna2 = coin.Composite()
gna2.name = "test2"
gna.getChildren().append(gna2)
for slip in gna.getChildren():
print(slip.name)
产生错误:
Traceback (most recent call last):
File "gna.py", line 34, in <module>
for prout in gna.getChildren():
TypeError: No to_python (by-value) converter found for C++ type: class Composite * __ptr64
这在 Boost 1.59 上运行良好,但在 Boost 1.60 上就不行了。
有什么想法吗?
编辑:
我尝试通过更新(见下面的建议):
class_<Composite>("Composite", "Composite*", init<>())
至:
class_<Composite, Composite*>("Composite", "Composite*", init<>())
而且我确认它适用于 boost 1.59,但不适用于 boost 1.60。
改变
class_<Composite>("Composite", "Composite*", init<>())
至
class_<Composite, Composite*>("Composite", "Composite*", init<>())
帮助提升 1.55。
我可以为这个问题找到任何 "nice" 解决方案,并将我所有的原始指针迁移到 std::shared_ptr 以使其工作。这个问题在这里讨论:
https://github.com/boostorg/python/issues/29
和这里:
https://github.com/boostorg/python/issues/56
我遇到了一个非常相似的问题: unexpected result iterating over a boost::python vector_indexing_suite
我有以下 C++ 代码:
#include <vector>
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
using namespace boost::python;
class Composite {
public:
std::string name;
std::vector<Composite*>& getChildren() {
return children;
};
std::vector<Composite*> children;
};
typedef std::vector<Composite*> CompositeArray;
BOOST_PYTHON_MODULE(coin)
{
class_<CompositeArray>("CompositeArray")
.def(vector_indexing_suite<CompositeArray, true>());
class_<Composite>("Composite", "Composite*", init<>())
.def("getChildren", &Composite::getChildren, return_internal_reference<>())
.def_readwrite("name", &Composite::name, "str")
;
}
以及以下 Python 代码:
import coin
gna = coin.Composite()
gna.name = "test"
gna2 = coin.Composite()
gna2.name = "test2"
gna.getChildren().append(gna2)
for slip in gna.getChildren():
print(slip.name)
产生错误:
Traceback (most recent call last):
File "gna.py", line 34, in <module>
for prout in gna.getChildren():
TypeError: No to_python (by-value) converter found for C++ type: class Composite * __ptr64
这在 Boost 1.59 上运行良好,但在 Boost 1.60 上就不行了。
有什么想法吗?
编辑: 我尝试通过更新(见下面的建议):
class_<Composite>("Composite", "Composite*", init<>())
至:
class_<Composite, Composite*>("Composite", "Composite*", init<>())
而且我确认它适用于 boost 1.59,但不适用于 boost 1.60。
改变
class_<Composite>("Composite", "Composite*", init<>())
至
class_<Composite, Composite*>("Composite", "Composite*", init<>())
帮助提升 1.55。
我可以为这个问题找到任何 "nice" 解决方案,并将我所有的原始指针迁移到 std::shared_ptr 以使其工作。这个问题在这里讨论: https://github.com/boostorg/python/issues/29 和这里: https://github.com/boostorg/python/issues/56