Commit 9f30098b authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Update numpy bindings to comply with upcoming API deprecation

parent a5990d2a
......@@ -26,6 +26,7 @@
#ifndef NUMPY_EXPORT
#define NO_IMPORT_ARRAY
#endif
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "arrayobject.h"
#include <boost/array.hpp>
......@@ -74,8 +75,8 @@ python::object wrap_vector_owned(vector<ValueType>& vec)
ndarray = (PyArrayObject*) PyArray_SimpleNewFromData(1, size, val_type,
new_data);
}
ndarray->flags = NPY_ALIGNED | NPY_C_CONTIGUOUS | NPY_OWNDATA |
NPY_WRITEABLE;
PyArray_ENABLEFLAGS(ndarray, NPY_ARRAY_ALIGNED | NPY_ARRAY_C_CONTIGUOUS |
NPY_ARRAY_OWNDATA | NPY_ARRAY_WRITEABLE);
handle<> x((PyObject*) ndarray);
object o(x);
return o;
......@@ -92,7 +93,8 @@ python::object wrap_vector_not_owned(vector<ValueType>& vec)
else
ndarray = (PyArrayObject*) PyArray_SimpleNewFromData(1, &size, val_type,
&vec[0]);
ndarray->flags = NPY_ALIGNED | NPY_C_CONTIGUOUS | NPY_WRITEABLE;
PyArray_ENABLEFLAGS(ndarray,NPY_ARRAY_ALIGNED | NPY_ARRAY_C_CONTIGUOUS |
NPY_ARRAY_WRITEABLE);
handle<> x((PyObject*) ndarray);
object o(x);
return o;
......@@ -111,8 +113,8 @@ python::object wrap_multi_array_owned(multi_array<ValueType,Dim>& array)
PyArrayObject* ndarray =
(PyArrayObject*) PyArray_SimpleNewFromData(Dim, shape, val_type,
new_data);
ndarray->flags = NPY_ALIGNED | NPY_C_CONTIGUOUS | NPY_OWNDATA |
NPY_WRITEABLE;
PyArray_ENABLEFLAGS(ndarray, NPY_ARRAY_ALIGNED | NPY_ARRAY_C_CONTIGUOUS |
NPY_ARRAY_OWNDATA | NPY_ARRAY_WRITEABLE);
handle<> x((PyObject*) ndarray);
object o(x);
return o;
......@@ -125,7 +127,8 @@ python::object wrap_multi_array_not_owned(multi_array<ValueType,Dim>& array)
PyArrayObject* ndarray =
(PyArrayObject*) PyArray_SimpleNewFromData(Dim, array.shape(), val_type,
array.origin());
ndarray->flags = NPY_ALIGNED | NPY_C_CONTIGUOUS | NPY_WRITEABLE;
PyArray_ENABLEFLAGS(ndarray, NPY_ARRAY_ALIGNED | NPY_ARRAY_C_CONTIGUOUS |
NPY_ARRAY_WRITEABLE);
handle<> x((PyObject*) ndarray);
object o(x);
return o;
......@@ -148,29 +151,29 @@ multi_array_ref<ValueType,dim> get_array(python::object points)
{
PyArrayObject* pa = (PyArrayObject*) points.ptr();
if (pa->nd != dim)
if (PyArray_NDIM(pa) != dim)
throw invalid_numpy_conversion("invalid array dimension!");
if (mpl::at<numpy_types,ValueType>::type::value != pa->descr->type_num)
if (mpl::at<numpy_types,ValueType>::type::value != PyArray_DESCR(pa)->type_num)
{
using python::detail::gcc_demangle;
handle<> x((PyObject*) pa->descr->typeobj);
handle<> x((PyObject*) PyArray_DESCR(pa)->typeobj);
object dtype(x);
string type_name = python::extract<string>(python::str(dtype));
string error = "invalid array value type: " + type_name;
error += " (id: " + lexical_cast<string>(pa->descr->type_num) + ")";
error += " (id: " + lexical_cast<string>(PyArray_DESCR(pa)->type_num) + ")";
error += ", wanted: " + string(gcc_demangle(typeid(ValueType).name()));
error += " (id: " + lexical_cast<string>(mpl::at<numpy_types,ValueType>::type::value) + ")";
throw invalid_numpy_conversion(error);
}
vector<size_t> shape(pa->nd);
for (int i = 0; i < pa->nd; ++i)
shape[i] = pa->dimensions[i];
if ((pa->flags ^ NPY_C_CONTIGUOUS) != 0)
return multi_array_ref<ValueType,dim>((ValueType *) pa->data, shape);
vector<size_t> shape(PyArray_NDIM(pa));
for (int i = 0; i < PyArray_NDIM(pa); ++i)
shape[i] = PyArray_DIMS(pa)[i];
if ((PyArray_FLAGS(pa) ^ NPY_ARRAY_C_CONTIGUOUS) != 0)
return multi_array_ref<ValueType,dim>((ValueType *) PyArray_DATA(pa), shape);
else
return multi_array_ref<ValueType,dim>((ValueType *) pa->data, shape,
return multi_array_ref<ValueType,dim>((ValueType *) PyArray_DATA(pa), shape,
fortran_storage_order());
}
......
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