How do I pass a Python list of my object type ClassName
to a C++ function that accepts a vector<ClassName>
?
The best I found is something like this: example. Unfortunately, the code crashes and I can't seem to figure out why. Here's what I used:
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;
}
}