Как передать список Python моего типа объекта ClassName
в функцию C ++, которая принимает vector<ClassName>
?
Лучшее, что я нашел, это что-то вроде этого: пример ,К сожалению, код падает, и я не могу понять, почему.Вот что я использовал:
template<typename T>
void python_to_vector(boost::python::object o, vector<T>* v) {
try {
object iter_obj = object(handle<>(PyObject_GetIter(o.ptr())));
return;
for (;;) {
object obj = extract<object>(iter_obj.attr("next")());
// Should launch an exception if it cannot extract T
v->emplace_back(extract<T>(obj));
}
} catch(error_already_set) {
PyErr_Clear();
// If there is an exception (no iterator, extract failed or end of the
// list reached), clear it and exit the function
return;
}
}