Commit d6992aa4 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Fix conversion of python iterators (generators, etc) to std::vector<T>

parent 4914c902
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include <boost/python.hpp> #include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp> #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python/stl_iterator.hpp>
#include "bytesobject.h" #include "bytesobject.h"
using namespace std; using namespace std;
...@@ -73,27 +74,19 @@ struct vector_from_list ...@@ -73,27 +74,19 @@ struct vector_from_list
static void* convertible(PyObject* obj_ptr) static void* convertible(PyObject* obj_ptr)
{ {
handle<> x(borrowed(obj_ptr)); // can't verify without potentially exhausting an iterator
object o(x);
size_t N = len(o);
for (size_t i = 0; i < N; ++i)
{
extract<ValueType> elem(o[i]);
if (!elem.check())
return 0;
}
return obj_ptr; return obj_ptr;
} }
static void construct(PyObject* obj_ptr, static void construct(PyObject* obj_ptr,
boost::python::converter::rvalue_from_python_stage1_data* data) boost::python::converter::rvalue_from_python_stage1_data* data)
{ {
handle<> x(borrowed(obj_ptr)); python::handle<> x(python::borrowed(obj_ptr));
object o(x); python::object o(x);
vector<ValueType> value; vector<ValueType> value;
size_t N = len(o); python::stl_input_iterator<ValueType> iter(o), end;
for (size_t i = 0; i < N; ++i) for (; iter != end; ++iter)
value.push_back(extract<ValueType>(o[i])()); value.push_back(*iter);
void* storage = void* storage =
( (boost::python::converter::rvalue_from_python_storage ( (boost::python::converter::rvalue_from_python_storage
<vector<ValueType> >*) data)->storage.bytes; <vector<ValueType> >*) data)->storage.bytes;
...@@ -212,8 +205,6 @@ struct pair_from_tuple ...@@ -212,8 +205,6 @@ struct pair_from_tuple
handle<> x(borrowed(obj_ptr)); handle<> x(borrowed(obj_ptr));
object o(x); object o(x);
pair<T1,T2> value; pair<T1,T2> value;
if (boost::python::len(o) < 2)
throw ValueException("Invalid conversion to pair... Sequence is too short.");
value.first = extract<T1>(o[0])(); value.first = extract<T1>(o[0])();
value.second = extract<T2>(o[1])(); value.second = extract<T2>(o[1])();
void* storage = void* storage =
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment