Commit bd0f7fd5 authored by Tiago Peixoto's avatar Tiago Peixoto

Fix compilation errors with older numpy versions

parent 9384b6e4
......@@ -33,26 +33,24 @@
#include <boost/mpl/for_each.hpp>
using namespace std;
namespace mpl = boost::mpl;
namespace python = boost::python;
typedef mpl::map<
mpl::pair<bool, mpl::int_<NPY_BOOL> >,
mpl::pair<uint8_t, mpl::int_<NPY_BYTE> >,
mpl::pair<uint32_t, mpl::int_<NPY_UINT32> >,
mpl::pair<int16_t, mpl::int_<NPY_INT16> >,
mpl::pair<int32_t, mpl::int_<NPY_INT32> >,
mpl::pair<int64_t, mpl::int_<NPY_INT64> >,
mpl::pair<uint64_t, mpl::int_<NPY_UINT64> >,
mpl::pair<unsigned long int, mpl::int_<NPY_ULONG> >,
mpl::pair<double, mpl::int_<NPY_DOUBLE> >,
mpl::pair<long double, mpl::int_<NPY_LONGDOUBLE> >
typedef boost::mpl::map<
boost::mpl::pair<bool, boost::mpl::int_<NPY_BOOL> >,
boost::mpl::pair<uint8_t, boost::mpl::int_<NPY_BYTE> >,
boost::mpl::pair<uint32_t, boost::mpl::int_<NPY_UINT32> >,
boost::mpl::pair<int16_t, boost::mpl::int_<NPY_INT16> >,
boost::mpl::pair<int32_t, boost::mpl::int_<NPY_INT32> >,
boost::mpl::pair<int64_t, boost::mpl::int_<NPY_INT64> >,
boost::mpl::pair<uint64_t, boost::mpl::int_<NPY_UINT64> >,
boost::mpl::pair<unsigned long int, boost::mpl::int_<NPY_ULONG> >,
boost::mpl::pair<double, boost::mpl::int_<NPY_DOUBLE> >,
boost::mpl::pair<long double, boost::mpl::int_<NPY_LONGDOUBLE> >
> numpy_types;
template <class ValueType>
python::object wrap_vector_owned(vector<ValueType>& vec)
boost::python::object wrap_vector_owned(vector<ValueType>& vec)
{
int val_type = mpl::at<numpy_types,ValueType>::type::value;
int val_type = boost::mpl::at<numpy_types,ValueType>::type::value;
npy_intp size[1];
size[0] = vec.size();
PyArrayObject* ndarray;
......@@ -69,16 +67,16 @@ python::object wrap_vector_owned(vector<ValueType>& vec)
}
ndarray->flags = NPY_ALIGNED | NPY_C_CONTIGUOUS | NPY_OWNDATA |
NPY_WRITEABLE;
python::handle<> x((PyObject*) ndarray);
python::object o(x);
boost::python::handle<> x((PyObject*) ndarray);
boost::python::object o(x);
return o;
}
template <class ValueType>
python::object wrap_vector_not_owned(vector<ValueType>& vec)
boost::python::object wrap_vector_not_owned(vector<ValueType>& vec)
{
PyArrayObject* ndarray;
int val_type = mpl::at<numpy_types,ValueType>::type::value;
int val_type = boost::mpl::at<numpy_types,ValueType>::type::value;
npy_intp size = vec.size();
if (vec.empty())
return wrap_vector_owned(vec); // return an _owned_ array of size one.
......@@ -86,18 +84,18 @@ python::object wrap_vector_not_owned(vector<ValueType>& vec)
ndarray = (PyArrayObject*) PyArray_SimpleNewFromData(1, &size, val_type,
&vec[0]);
ndarray->flags = NPY_ALIGNED | NPY_C_CONTIGUOUS | NPY_WRITEABLE;
python::handle<> x((PyObject*) ndarray);
object o(x);
boost::python::handle<> x((PyObject*) ndarray);
boost::python::object o(x);
return o;
}
template <class ValueType, int Dim>
python::object wrap_multi_array_owned(boost::multi_array<ValueType,Dim>& array)
boost::python::object wrap_multi_array_owned(boost::multi_array<ValueType,Dim>& array)
{
ValueType* new_data = new ValueType[array.num_elements()];
memcpy(new_data, array.data(), array.num_elements()*sizeof(ValueType));
int val_type = mpl::at<numpy_types,ValueType>::type::value;
int val_type = boost::mpl::at<numpy_types,ValueType>::type::value;
npy_intp shape[Dim];
for (int i = 0; i < Dim; ++i)
shape[i] = array.shape()[i];
......@@ -106,21 +104,21 @@ python::object wrap_multi_array_owned(boost::multi_array<ValueType,Dim>& array)
new_data);
ndarray->flags = NPY_ALIGNED | NPY_C_CONTIGUOUS | NPY_OWNDATA |
NPY_WRITEABLE;
python::handle<> x((PyObject*) ndarray);
python::object o(x);
boost::python::handle<> x((PyObject*) ndarray);
boost::python::object o(x);
return o;
}
template <class ValueType, int Dim>
python::object wrap_multi_array_not_owned(boost::multi_array<ValueType,Dim>& array)
boost::python::object wrap_multi_array_not_owned(boost::multi_array<ValueType,Dim>& array)
{
int val_type = mpl::at<numpy_types,ValueType>::type::value;
int val_type = boost::mpl::at<numpy_types,ValueType>::type::value;
PyArrayObject* ndarray =
(PyArrayObject*) PyArray_SimpleNewFromData(Dim, array.shape(), val_type,
array.origin());
ndarray->flags = NPY_ALIGNED | NPY_C_CONTIGUOUS | NPY_WRITEABLE;
python::handle<> x((PyObject*) ndarray);
python::object o(x);
boost::python::handle<> x((PyObject*) ndarray);
boost::python::object o(x);
return o;
}
......@@ -137,23 +135,23 @@ public:
};
template <class ValueType, size_t dim>
boost::multi_array_ref<ValueType,dim> get_array(python::object points)
boost::multi_array_ref<ValueType,dim> get_array(boost::python::object points)
{
PyArrayObject* pa = (PyArrayObject*) points.ptr();
if (pa->nd != dim)
throw invalid_numpy_conversion("invalid array dimension!");
if (mpl::at<numpy_types,ValueType>::type::value != pa->descr->type_num)
if (boost::mpl::at<numpy_types,ValueType>::type::value != pa->descr->type_num)
{
using python::detail::gcc_demangle;
python::handle<> x((PyObject*) pa->descr->typeobj);
python::object dtype(x);
string type_name = python::extract<string>(python::str(dtype));
using boost::python::detail::gcc_demangle;
boost::python::handle<> x((PyObject*) pa->descr->typeobj);
boost::python::object dtype(x);
string type_name = boost::python::extract<string>(boost::python::str(dtype));
string error = "invalid array value type: " + type_name;
error += " (id: " + boost::lexical_cast<string>(pa->descr->type_num) + ")";
error += ", wanted: " + string(gcc_demangle(typeid(ValueType).name()));
error += " (id: " + boost::lexical_cast<string>(mpl::at<numpy_types,ValueType>::type::value) + ")";
error += " (id: " + boost::lexical_cast<string>(boost::mpl::at<numpy_types,ValueType>::type::value) + ")";
throw invalid_numpy_conversion(error);
}
......
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