Hi Christoph,
Post by Christoph Kokeyou currently get a deprecated API warnings when using pyublas with
numpy 1.7. I made some changes to avoid it, which I would like to
provide for others too. I could test them with numpy 1.6.2 and numpy
1.7.1 on debian linux. I hope this is useful.
Thanks again for your patch. I've ended up reverting these changes for
the time being as it has turned out that your changes broke some of my
code.
I've pasted the compiler errors below. I don't have time to investigate
in depth right now, so the easiest route for me was to revert. I'd be
happy to take an updated patch. As I was looking through your changes
this time around, I realized that that I would have preferred if the
handle<> smart pointer were holding a PyArrayObject in the first
place. That would reduce the amount of casting needing to be done.
Andreas
src/wrapper/wrap_silo.cpp: In function ?NPY_TYPES {anonymous}::get_varlist_dtype(boost::python::api::object)?:
src/wrapper/wrap_silo.cpp:357:52: error: cannot convert ?PyObject* {aka _object*}? to ?const PyArrayObject* {aka const tagPyArrayObject*}? for argument ?1? to ?int PyArray_TYPE(const PyArrayObject*)?
result = (NPY_TYPES) PyArray_TYPE(var.ptr());
^
src/wrapper/wrap_silo.cpp:360:48: error: cannot convert ?PyObject* {aka _object*}? to ?const PyArrayObject* {aka const tagPyArrayObject*}? for argument ?1? to ?int PyArray_TYPE(const PyArrayObject*)?
else if (result != PyArray_TYPE(var.ptr()))
^
src/wrapper/wrap_silo.cpp: In function ?boost::python::handle<> {anonymous}::curve_x(boost::python::api::object)?:
src/wrapper/wrap_silo.cpp:565:30: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?PyObject* PyArray_BASE(PyArrayObject*)?
PyArray_BASE(result.get()) = py_curve.ptr(); \
^
src/wrapper/wrap_silo.cpp:570:3: note: in expansion of macro ?PYVISFILE_CURVE_DATA_GETTER?
PYVISFILE_CURVE_DATA_GETTER(x);
^
In file included from /usr/include/python2.7/Python.h:80:0,
from /usr/include/boost/python/detail/wrap_python.hpp:142,
from /usr/include/boost/python/detail/prefix.hpp:13,
from /usr/include/boost/python/class.hpp:8,
from /usr/include/boost/python/suite/indexing/indexing_suite.hpp:9,
from /usr/include/boost/python/suite/indexing/vector_indexing_suite.hpp:9,
from src/wrapper/wrap_silo.cpp:9:
src/wrapper/wrap_silo.cpp:566:40: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?PyObject* PyArray_BASE(PyArrayObject*)?
Py_INCREF(PyArray_BASE(result.get())); \
^
src/wrapper/wrap_silo.cpp:570:3: note: in expansion of macro ?PYVISFILE_CURVE_DATA_GETTER?
PYVISFILE_CURVE_DATA_GETTER(x);
^
src/wrapper/wrap_silo.cpp: In function ?boost::python::handle<> {anonymous}::curve_y(boost::python::api::object)?:
src/wrapper/wrap_silo.cpp:565:30: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?PyObject* PyArray_BASE(PyArrayObject*)?
PyArray_BASE(result.get()) = py_curve.ptr(); \
^
src/wrapper/wrap_silo.cpp:571:3: note: in expansion of macro ?PYVISFILE_CURVE_DATA_GETTER?
PYVISFILE_CURVE_DATA_GETTER(y);
^
In file included from /usr/include/python2.7/Python.h:80:0,
from /usr/include/boost/python/detail/wrap_python.hpp:142,
from /usr/include/boost/python/detail/prefix.hpp:13,
from /usr/include/boost/python/class.hpp:8,
from /usr/include/boost/python/suite/indexing/indexing_suite.hpp:9,
from /usr/include/boost/python/suite/indexing/vector_indexing_suite.hpp:9,
from src/wrapper/wrap_silo.cpp:9:
src/wrapper/wrap_silo.cpp:566:40: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?PyObject* PyArray_BASE(PyArrayObject*)?
Py_INCREF(PyArray_BASE(result.get())); \
^
src/wrapper/wrap_silo.cpp:571:3: note: in expansion of macro ?PYVISFILE_CURVE_DATA_GETTER?
PYVISFILE_CURVE_DATA_GETTER(y);
^
src/wrapper/wrap_silo.cpp: In function ?boost::python::api::object {anonymous}::quadmesh_coords(boost::python::api::object)?:
src/wrapper/wrap_silo.cpp:634:37: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?PyObject* PyArray_BASE(PyArrayObject*)?
PyArray_BASE(coord_array.get()) = py_quadmesh.ptr();
^
In file included from /usr/include/python2.7/Python.h:80:0,
from /usr/include/boost/python/detail/wrap_python.hpp:142,
from /usr/include/boost/python/detail/prefix.hpp:13,
from /usr/include/boost/python/class.hpp:8,
from /usr/include/boost/python/suite/indexing/indexing_suite.hpp:9,
from /usr/include/boost/python/suite/indexing/vector_indexing_suite.hpp:9,
from src/wrapper/wrap_silo.cpp:9:
src/wrapper/wrap_silo.cpp:635:47: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?PyObject* PyArray_BASE(PyArrayObject*)?
Py_INCREF(PyArray_BASE(coord_array.get()));
^
src/wrapper/wrap_silo.cpp: In function ?boost::python::api::object {anonymous}::quadvar_vals(boost::python::api::object)?:
src/wrapper/wrap_silo.cpp:701:20: error: ?NPY_CARRAY? was not declared in this scope
ary_flags |= NPY_CARRAY;
^
src/wrapper/wrap_silo.cpp:703:20: error: ?NPY_FARRAY? was not declared in this scope
ary_flags |= NPY_FARRAY;
^
src/wrapper/wrap_silo.cpp:719:35: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?PyObject* PyArray_BASE(PyArrayObject*)?
PyArray_BASE(val_array.get()) = py_quadvar.ptr();
^
In file included from /usr/include/python2.7/Python.h:80:0,
from /usr/include/boost/python/detail/wrap_python.hpp:142,
from /usr/include/boost/python/detail/prefix.hpp:13,
from /usr/include/boost/python/class.hpp:8,
from /usr/include/boost/python/suite/indexing/indexing_suite.hpp:9,
from /usr/include/boost/python/suite/indexing/vector_indexing_suite.hpp:9,
from src/wrapper/wrap_silo.cpp:9:
src/wrapper/wrap_silo.cpp:720:45: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?PyObject* PyArray_BASE(PyArrayObject*)?
Py_INCREF(PyArray_BASE(val_array.get()));
^
In file included from /usr/lib/pymodules/python2.7/numpy/core/include/numpy/arrayobject.h:15:0,
from /home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:47,
from src/wrapper/wrap_silo.cpp:10:
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp: In instantiation of ?typename Super::value_type& pyublas::detail::vector_functionality<Derived, Super>::sub(npy_intp, npy_intp) [with Derived = pyublas::numpy_vector<float>; Super = boost::numeric::ublas::vector<float, pyublas::numpy_array<float> >; typename Super::value_type = float; npy_intp = long int]?:
src/wrapper/wrap_silo.cpp:890:34: required from ?void {anonymous}::DBfileWrapper::put_ucdmesh(const char*, boost::python::api::object, pyublas::numpy_vector<T>, int, const char*, const char*, {anonymous}::DBoptlistWrapper&) [with T = float]?
src/wrapper/wrap_silo.cpp:1495:50: required from here
/usr/lib/pymodules/python2.7/numpy/core/include/numpy/ndarrayobject.h:150:68: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?npy_intp* PyArray_STRIDES(PyArrayObject*)?
(j)*PyArray_STRIDES(obj)[1]))
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:675:13: note: in expansion of macro ?PyArray_GETPTR2?
PyArray_GETPTR2(
^
/usr/lib/pymodules/python2.7/numpy/core/include/numpy/ndarrayobject.h:149:68: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?npy_intp* PyArray_STRIDES(PyArrayObject*)?
(i)*PyArray_STRIDES(obj)[0] + \
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:675:13: note: in expansion of macro ?PyArray_GETPTR2?
PyArray_GETPTR2(
^
/usr/lib/pymodules/python2.7/numpy/core/include/numpy/ndarrayobject.h:148:63: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?char* PyArray_BYTES(PyArrayObject*)?
#define PyArray_GETPTR2(obj, i, j) ((void *)(PyArray_BYTES(obj) + \
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:675:13: note: in expansion of macro ?PyArray_GETPTR2?
PyArray_GETPTR2(
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp: In instantiation of ?typename Super::value_type& pyublas::detail::vector_functionality<Derived, Super>::sub(npy_intp, npy_intp) [with Derived = pyublas::numpy_vector<double>; Super = boost::numeric::ublas::vector<double, pyublas::numpy_array<double> >; typename Super::value_type = double; npy_intp = long int]?:
src/wrapper/wrap_silo.cpp:890:34: required from ?void {anonymous}::DBfileWrapper::put_ucdmesh(const char*, boost::python::api::object, pyublas::numpy_vector<T>, int, const char*, const char*, {anonymous}::DBoptlistWrapper&) [with T = double]?
src/wrapper/wrap_silo.cpp:1496:51: required from here
/usr/lib/pymodules/python2.7/numpy/core/include/numpy/ndarrayobject.h:150:68: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?npy_intp* PyArray_STRIDES(PyArrayObject*)?
(j)*PyArray_STRIDES(obj)[1]))
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:675:13: note: in expansion of macro ?PyArray_GETPTR2?
PyArray_GETPTR2(
^
/usr/lib/pymodules/python2.7/numpy/core/include/numpy/ndarrayobject.h:149:68: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?npy_intp* PyArray_STRIDES(PyArrayObject*)?
(i)*PyArray_STRIDES(obj)[0] + \
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:675:13: note: in expansion of macro ?PyArray_GETPTR2?
PyArray_GETPTR2(
^
/usr/lib/pymodules/python2.7/numpy/core/include/numpy/ndarrayobject.h:148:63: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?char* PyArray_BYTES(PyArrayObject*)?
#define PyArray_GETPTR2(obj, i, j) ((void *)(PyArray_BYTES(obj) + \
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:675:13: note: in expansion of macro ?PyArray_GETPTR2?
PyArray_GETPTR2(
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp: In instantiation of ?const typename Super::value_type& pyublas::detail::vector_functionality<Derived, Super>::sub(npy_intp) const [with Derived = pyublas::numpy_vector<float>; Super = boost::numeric::ublas::vector<float, pyublas::numpy_array<float> >; typename Super::value_type = float; npy_intp = long int]?:
src/wrapper/wrap_silo.cpp:910:9: required from ?void {anonymous}::DBfileWrapper::put_ucdvar1(const char*, const char*, const pyublas::numpy_vector<T>&, int, {anonymous}::DBoptlistWrapper&) [with T = float]?
src/wrapper/wrap_silo.cpp:1497:50: required from here
/usr/lib/pymodules/python2.7/numpy/core/include/numpy/ndarrayobject.h:146:65: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?npy_intp* PyArray_STRIDES(PyArrayObject*)?
(i)*PyArray_STRIDES(obj)[0]))
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:668:13: note: in expansion of macro ?PyArray_GETPTR1?
PyArray_GETPTR1(
^
/usr/lib/pymodules/python2.7/numpy/core/include/numpy/ndarrayobject.h:145:60: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?char* PyArray_BYTES(PyArrayObject*)?
#define PyArray_GETPTR1(obj, i) ((void *)(PyArray_BYTES(obj) + \
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:668:13: note: in expansion of macro ?PyArray_GETPTR1?
PyArray_GETPTR1(
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp: In instantiation of ?const typename Super::value_type& pyublas::detail::vector_functionality<Derived, Super>::sub(npy_intp) const [with Derived = pyublas::numpy_vector<double>; Super = boost::numeric::ublas::vector<double, pyublas::numpy_array<double> >; typename Super::value_type = double; npy_intp = long int]?:
src/wrapper/wrap_silo.cpp:910:9: required from ?void {anonymous}::DBfileWrapper::put_ucdvar1(const char*, const char*, const pyublas::numpy_vector<T>&, int, {anonymous}::DBoptlistWrapper&) [with T = double]?
src/wrapper/wrap_silo.cpp:1498:51: required from here
/usr/lib/pymodules/python2.7/numpy/core/include/numpy/ndarrayobject.h:146:65: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?npy_intp* PyArray_STRIDES(PyArrayObject*)?
(i)*PyArray_STRIDES(obj)[0]))
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:668:13: note: in expansion of macro ?PyArray_GETPTR1?
PyArray_GETPTR1(
^
/usr/lib/pymodules/python2.7/numpy/core/include/numpy/ndarrayobject.h:145:60: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?char* PyArray_BYTES(PyArrayObject*)?
#define PyArray_GETPTR1(obj, i) ((void *)(PyArray_BYTES(obj) + \
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:668:13: note: in expansion of macro ?PyArray_GETPTR1?
PyArray_GETPTR1(
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp: In instantiation of ?const typename Super::value_type& pyublas::detail::vector_functionality<Derived, Super>::sub(npy_intp, npy_intp) const [with Derived = pyublas::numpy_vector<float>; Super = boost::numeric::ublas::vector<float, pyublas::numpy_array<float> >; typename Super::value_type = float; npy_intp = long int]?:
src/wrapper/wrap_silo.cpp:1057:44: required from ?void {anonymous}::DBfileWrapper::put_pointmesh(const char*, const pyublas::numpy_vector<T>&, {anonymous}::DBoptlistWrapper&) [with T = float]?
src/wrapper/wrap_silo.cpp:1503:54: required from here
/usr/lib/pymodules/python2.7/numpy/core/include/numpy/ndarrayobject.h:150:68: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?npy_intp* PyArray_STRIDES(PyArrayObject*)?
(j)*PyArray_STRIDES(obj)[1]))
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:682:13: note: in expansion of macro ?PyArray_GETPTR2?
PyArray_GETPTR2(
^
/usr/lib/pymodules/python2.7/numpy/core/include/numpy/ndarrayobject.h:149:68: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?npy_intp* PyArray_STRIDES(PyArrayObject*)?
(i)*PyArray_STRIDES(obj)[0] + \
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:682:13: note: in expansion of macro ?PyArray_GETPTR2?
PyArray_GETPTR2(
^
/usr/lib/pymodules/python2.7/numpy/core/include/numpy/ndarrayobject.h:148:63: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?char* PyArray_BYTES(PyArrayObject*)?
#define PyArray_GETPTR2(obj, i, j) ((void *)(PyArray_BYTES(obj) + \
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:682:13: note: in expansion of macro ?PyArray_GETPTR2?
PyArray_GETPTR2(
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp: In instantiation of ?const typename Super::value_type& pyublas::detail::vector_functionality<Derived, Super>::sub(npy_intp, npy_intp) const [with Derived = pyublas::numpy_vector<double>; Super = boost::numeric::ublas::vector<double, pyublas::numpy_array<double> >; typename Super::value_type = double; npy_intp = long int]?:
src/wrapper/wrap_silo.cpp:1057:44: required from ?void {anonymous}::DBfileWrapper::put_pointmesh(const char*, const pyublas::numpy_vector<T>&, {anonymous}::DBoptlistWrapper&) [with T = double]?
src/wrapper/wrap_silo.cpp:1504:55: required from here
/usr/lib/pymodules/python2.7/numpy/core/include/numpy/ndarrayobject.h:150:68: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?npy_intp* PyArray_STRIDES(PyArrayObject*)?
(j)*PyArray_STRIDES(obj)[1]))
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:682:13: note: in expansion of macro ?PyArray_GETPTR2?
PyArray_GETPTR2(
^
/usr/lib/pymodules/python2.7/numpy/core/include/numpy/ndarrayobject.h:149:68: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?npy_intp* PyArray_STRIDES(PyArrayObject*)?
(i)*PyArray_STRIDES(obj)[0] + \
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:682:13: note: in expansion of macro ?PyArray_GETPTR2?
PyArray_GETPTR2(
^
/usr/lib/pymodules/python2.7/numpy/core/include/numpy/ndarrayobject.h:148:63: error: cannot convert ?_object*? to ?PyArrayObject* {aka tagPyArrayObject*}? for argument ?1? to ?char* PyArray_BYTES(PyArrayObject*)?
#define PyArray_GETPTR2(obj, i, j) ((void *)(PyArray_BYTES(obj) + \
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:682:13: note: in expansion of macro ?PyArray_GETPTR2?
PyArray_GETPTR2(
^
In file included from src/wrapper/wrap_silo.cpp:10:0:
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp: In member function ?typename Super::value_type& pyublas::detail::vector_functionality<Derived, Super>::sub(npy_intp, npy_intp) [with Derived = pyublas::numpy_vector<float>; Super = boost::numeric::ublas::vector<float, pyublas::numpy_array<float> >; typename Super::value_type = float; npy_intp = long int]?:
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:678:7: warning: control reaches end of non-void function [-Wreturn-type]
}
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp: In member function ?typename Super::value_type& pyublas::detail::vector_functionality<Derived, Super>::sub(npy_intp, npy_intp) [with Derived = pyublas::numpy_vector<double>; Super = boost::numeric::ublas::vector<double, pyublas::numpy_array<double> >; typename Super::value_type = double; npy_intp = long int]?:
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:678:7: warning: control reaches end of non-void function [-Wreturn-type]
}
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp: In member function ?const typename Super::value_type& pyublas::detail::vector_functionality<Derived, Super>::sub(npy_intp) const [with Derived = pyublas::numpy_vector<float>; Super = boost::numeric::ublas::vector<float, pyublas::numpy_array<float> >; typename Super::value_type = float; npy_intp = long int]?:
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:671:7: warning: control reaches end of non-void function [-Wreturn-type]
}
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp: In member function ?const typename Super::value_type& pyublas::detail::vector_functionality<Derived, Super>::sub(npy_intp) const [with Derived = pyublas::numpy_vector<double>; Super = boost::numeric::ublas::vector<double, pyublas::numpy_array<double> >; typename Super::value_type = double; npy_intp = long int]?:
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:671:7: warning: control reaches end of non-void function [-Wreturn-type]
}
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp: In member function ?const typename Super::value_type& pyublas::detail::vector_functionality<Derived, Super>::sub(npy_intp, npy_intp) const [with Derived = pyublas::numpy_vector<float>; Super = boost::numeric::ublas::vector<float, pyublas::numpy_array<float> >; typename Super::value_type = float; npy_intp = long int]?:
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:685:7: warning: control reaches end of non-void function [-Wreturn-type]
}
^
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp: In member function ?const typename Super::value_type& pyublas::detail::vector_functionality<Derived, Super>::sub(npy_intp, npy_intp) const [with Derived = pyublas::numpy_vector<double>; Super = boost::numeric::ublas::vector<double, pyublas::numpy_array<double> >; typename Super::value_type = double; npy_intp = long int]?:
/home/andreas/src/pyublas/pyublas/include/pyublas/numpy.hpp:685:7: warning: control reaches end of non-void function [-Wreturn-type]
}
^
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
Post by Christoph KokeBest Wishes
Christoph
From 3c1c26f310e79d3209f8a50ac2fc79d5773b5e8d Mon Sep 17 00:00:00 2001
From: Christoph Koke <koke at kip.uni-heidelberg.de>
Date: Tue, 1 Oct 2013 16:11:58 +0200
Subject: [PATCH 1/2] #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION, to
avoid warnings
---
pyublas/include/pyublas/numpy.hpp | 125 ++++++++++++++++++++++++--------------
src/wrapper/converters.cpp | 20 +++---
2 files changed, 92 insertions(+), 53 deletions(-)
diff --git a/pyublas/include/pyublas/numpy.hpp b/pyublas/include/pyublas/numpy.hpp
index 595bebd..7de5178 100644
--- a/pyublas/include/pyublas/numpy.hpp
+++ b/pyublas/include/pyublas/numpy.hpp
@@ -37,13 +37,15 @@
#include <boost/python.hpp>
#include <boost/python.hpp>
#include <boost/foreach.hpp>
-#include <numpy/arrayobject.h>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/iterator/iterator_categories.hpp>
#include <boost/range.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_class.hpp>
+#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
+#include <numpy/arrayobject.h>
+
namespace
{
static struct pyublas_array_importer
@@ -95,9 +97,25 @@ namespace pyublas
inline NPY_TYPES get_typenum(boost::python::handle<>) { return NPY_OBJECT; }
/* NPY_STRING, NPY_UNICODE unsupported for now */
+ typedef ::PyArrayObject PyArrayObject;
+
+ inline PyArrayObject * toPyArrayObject(boost::python::handle<> & obj) {
+ if (obj.get() && PyArray_Check(obj.get()))
+ return reinterpret_cast<PyArrayObject *>(obj.get());
+ else
+ return 0;
+ }
+
+ inline const PyArrayObject * toPyArrayObject(const boost::python::handle<> & obj) {
+ if (obj.get() && PyArray_Check(obj.get()))
+ return reinterpret_cast<const PyArrayObject *>(obj.get());
+ else
+ return 0;
+ }
+
template <class T>
inline
- bool is_storage_compatible(PyObject *ary)
+ bool is_storage_compatible(const PyArrayObject *ary)
{
/* This piece of code works around the fact that 'int' and
* 'long int' are the same on 32-bit machines, which can lead
@@ -108,20 +126,21 @@ namespace pyublas
*/
NPY_TYPES typenum = NPY_TYPES(PyArray_TYPE(ary));
+ size_t itemsize = PyArray_ITEMSIZE(ary);
- if (boost::is_integral<T>::value && PyArray_ISINTEGER(ary))
+ if (boost::is_integral<T>::value && PyTypeNum_ISINTEGER(typenum))
{
- return (sizeof(T) == PyArray_ITEMSIZE(ary)
+ return (sizeof(T) == itemsize
&& bool(boost::is_signed<T>::value)
- == bool(PyArray_ISSIGNED(ary)));
+ == PyTypeNum_ISSIGNED(typenum));
}
- else if (typenum == NPY_BOOL && (
+ else if (PyTypeNum_ISBOOL(typenum) && (
boost::is_same<T, signed char>::value ||
boost::is_same<T, unsigned char>::value))
{
- return (sizeof(T) == PyArray_ITEMSIZE(ary)
+ return (sizeof(T) == itemsize
&& bool(boost::is_signed<T>::value)
- == bool(PyArray_ISSIGNED(ary)));
+ == PyArray_ISSIGNED(ary));
}
else
return typenum == get_typenum(T());
@@ -129,7 +148,6 @@ namespace pyublas
-
// tool functions -----------------------------------------------------------
inline
npy_intp size_from_dims(int ndim, const npy_intp *dims)
@@ -224,22 +242,24 @@ namespace pyublas
return;
}
- if (!PyArray_Check(obj.get()))
- PYUBLAS_PYERROR(TypeError, "argument is not a numpy array");
- if (!is_storage_compatible<T>(obj.get()))
- PYUBLAS_PYERROR(TypeError, "argument is numpy array of wrong type");
- if (!PyArray_CHKFLAGS(obj.get(), NPY_ALIGNED))
+ if (const PyArrayObject * array = toPyArrayObject(obj))
+ {
+ if (!PyArray_Check(array))
+ PYUBLAS_PYERROR(TypeError, "argument is not a numpy array");
+ if (!is_storage_compatible<T>(array))
+ PYUBLAS_PYERROR(TypeError, "argument is numpy array of wrong type");
+ if (!PyArray_CHKFLAGS(array, NPY_ARRAY_ALIGNED))
PYUBLAS_PYERROR(ValueError, "argument array is not aligned");
- if (PyArray_CHKFLAGS(obj.get(), NPY_NOTSWAPPED))
+ if (PyArray_CHKFLAGS(array, NPY_ARRAY_NOTSWAPPED))
PYUBLAS_PYERROR(ValueError, "argument array does not have native endianness");
- if (PyArray_ITEMSIZE(obj.get()) != sizeof(T))
+ if (PyArray_ITEMSIZE(array) != sizeof(T))
PYUBLAS_PYERROR(ValueError, "itemsize does not match size of target type");
+ }
}
numpy_array copy() const
{
- boost::python::handle<> cp(PyArray_NewCopy(
- reinterpret_cast<PyArrayObject *>(m_numpy_array.get()), NPY_ANYORDER));
+ boost::python::handle<> cp(PyArray_NewCopy(get_array()), NPY_ANYORDER);
return numpy_array(cp);
}
@@ -247,7 +267,7 @@ namespace pyublas
void resize_internal (size_type new_size, value_type init, bool preserve = true)
{
size_type old_size;
- if (m_numpy_array.get())
+ if (get_array())
old_size = size();
else
{
@@ -260,8 +280,9 @@ namespace pyublas
npy_intp dims[] = { new_size };
boost::python::handle<> new_array = boost::python::handle<>(
PyArray_SimpleNew(1, dims, get_typenum(T())));
- pointer new_data = reinterpret_cast<T *>(
- PyArray_DATA(new_array.get()));
+ PyArrayObject * new_array_ptr = toPyArrayObject(new_array);
+
+ pointer new_data = reinterpret_cast<T *>(PyArray_DATA(new_array_ptr));
if (preserve)
{
@@ -298,22 +319,22 @@ namespace pyublas
// metadata
bool is_valid() const
- { return m_numpy_array.get(); }
+ { return get_array(); }
size_type ndim() const
- { return PyArray_NDIM(m_numpy_array.get()); }
+ { return PyArray_NDIM(get_array()); }
const npy_intp *dims() const
- { return PyArray_DIMS(m_numpy_array.get()); }
+ { return PyArray_DIMS(const_cast<numpy_array*>(this)->get_array()); }
const npy_intp dim(npy_intp i) const
- { return PyArray_DIM(m_numpy_array.get(), i); }
+ { return PyArray_DIM(get_array(), i); }
const npy_intp *strides() const
- { return PyArray_STRIDES(m_numpy_array.get()); }
+ { return PyArray_STRIDES(const_cast<numpy_array*>(this)->get_array()); }
const npy_intp stride(npy_intp i) const
- { return PyArray_STRIDE(m_numpy_array.get(), i); }
+ { return PyArray_STRIDE(get_array(), i); }
npy_intp itemsize() const
{ return sizeof(T); }
bool writable() const
- { return PyArray_ISWRITEABLE(m_numpy_array.get()); }
+ { return PyArray_ISWRITEABLE(get_array()); }
// shape manipulation
void reshape(int ndim_, const npy_intp *dims_,
@@ -322,20 +343,20 @@ namespace pyublas
PyArray_Dims d = { const_cast<npy_intp *>(dims_), ndim_ };
m_numpy_array = boost::python::handle<>(
PyArray_Newshape(
- (PyArrayObject *) m_numpy_array.get(), &d, order));
+ (PyArrayObject *) get_array(), &d, order));
}
// Raw data access
T *data()
{
return reinterpret_cast<T *>(
- PyArray_DATA(m_numpy_array.get()));
+ PyArray_DATA(get_array()));
}
const T *data() const
{
return reinterpret_cast<const T *>(
- PyArray_DATA(m_numpy_array.get()));
+ PyArray_DATA(const_cast<numpy_array*>(this)->get_array()));
}
// Element access
@@ -472,6 +493,14 @@ namespace pyublas
{
return handle();
}
+
+ PyArrayObject * get_array() {
+ return toPyArrayObject(m_numpy_array);
+ }
+
+ const PyArrayObject * get_array() const {
+ return toPyArrayObject(m_numpy_array);
+ }
};
@@ -492,38 +521,41 @@ namespace pyublas
{
typedef numpy_array<T> mat_type;
- if (PyArray_NDIM(ary.handle().get()) != 2)
+ const PyArrayObject * pyarray = ary.get_array();
+
+ if (PyArray_NDIM(pyarray) != 2)
throw std::runtime_error("ndarray->matrix converteee has dimension != 2");
- if (PyArray_STRIDE(ary.handle().get(), 1)
- == PyArray_ITEMSIZE(ary.handle().get()))
+ if (PyArray_STRIDE(pyarray, 1)
+ == PyArray_ITEMSIZE(pyarray))
{
// row-major
if (!is_row_major(OCat()))
throw std::runtime_error("input array is not row-major (like the target type)");
- if (!PyArray_CHKFLAGS(ary.handle().get(), NPY_C_CONTIGUOUS))
+ if (!PyArray_CHKFLAGS(pyarray, NPY_ARRAY_C_CONTIGUOUS))
throw std::runtime_error("ndarray->matrix converteee is not C-contiguous");
}
- else if (PyArray_STRIDE(ary.handle().get(), 0)
- == PyArray_ITEMSIZE(ary.handle().get()))
+ else if (PyArray_STRIDE(pyarray, 0)
+ == PyArray_ITEMSIZE(pyarray))
{
// column-major
if (is_row_major(OCat()))
throw std::runtime_error("input array is not column-major (like the target type)");
- if (!PyArray_CHKFLAGS(ary.handle().get(), NPY_F_CONTIGUOUS))
+ if (!PyArray_CHKFLAGS(pyarray, NPY_ARRAY_F_CONTIGUOUS))
throw std::runtime_error("ndarray->matrix converteee is not F-contiguous");
}
else
throw std::runtime_error("input array is does not have dimension with stride==1");
- return PyArray_DIM(ary.handle().get(), 0);
+ return PyArray_DIM(pyarray, 0);
}
template <class T>
typename numpy_array<T>::size_type get_array_size2(numpy_array<T> const &ary)
{
// checking is done in size1()
- return PyArray_DIM(ary.handle().get(), 1);
+ const PyArrayObject * pyarray = ary.get_array();
+ return PyArray_DIM(pyarray, 1);
}
template<class T, class L = boost::numeric::ublas::row_major>
@@ -540,26 +572,29 @@ namespace pyublas
if (is_row_major(typename mat_type::orientation_category()))
{
+ PyArrayObject * orig_ptr = toPyArrayObject(orig_handle);
result = boost::python::handle<>(PyArray_New(
&PyArray_Type, 2, dims,
get_typenum(typename mat_type::value_type()),
/*strides*/0,
- PyArray_DATA(orig_handle.get()),
+ PyArray_DATA(orig_ptr),
/* ? */ 0,
- NPY_CARRAY, NULL));
+ NPY_ARRAY_CARRAY, NULL));
}
else
{
+ PyArrayObject * orig_ptr = toPyArrayObject(orig_handle);
result = boost::python::handle<>(PyArray_New(
&PyArray_Type, 2, dims,
get_typenum(typename mat_type::value_type()),
/*strides*/0,
- PyArray_DATA(orig_handle.get()),
+ PyArray_DATA(orig_ptr),
/* ? */ 0,
- NPY_FARRAY, NULL));
+ NPY_ARRAY_FARRAY, NULL));
}
- PyArray_BASE(result.get()) = boost::python::handle<>(orig_handle).release();
+ PyArrayObject * result_ptr = toPyArrayObject(result);
+ PyArray_SetBaseObject(result_ptr, boost::python::handle<>(orig_handle).release());
return result;
}
diff --git a/src/wrapper/converters.cpp b/src/wrapper/converters.cpp
index 03ff010..246c9d0 100644
--- a/src/wrapper/converters.cpp
+++ b/src/wrapper/converters.cpp
@@ -133,17 +133,19 @@ namespace
}
- static void *check(PyObject* obj)
+ static void *check(PyObject* pyobj)
{
- if (!PyArray_Check(obj))
+ if (!PyArray_Check(pyobj))
{
if (trace_conversion)
std::cerr
- << boost::format("obj %1% rejected as vec: not a numpy array") % obj
+ << boost::format("obj %1% rejected as vec: not a numpy array") % pyobj
<< std::endl;
return 0;
}
+ PyArrayObject * obj = reinterpret_cast<PyArrayObject *>(pyobj);
+
if (!is_storage_compatible<typename super::value_type>(obj))
{
if (trace_conversion)
@@ -197,17 +199,19 @@ namespace
typedef array_converter_base<MatrixType> super;
- static void *check(PyObject* obj)
+ static void *check(PyObject* pyobj)
{
- if (!PyArray_Check(obj))
+ if (!PyArray_Check(pyobj))
{
if (trace_conversion)
std::cerr
- << boost::format("obj %1% rejected as mat: not a numpy array") % obj
+ << boost::format("obj %1% rejected as mat: not a numpy array") % pyobj
<< std::endl;
return 0;
}
+ PyArrayObject * obj = reinterpret_cast<PyArrayObject *>(pyobj);
+
if (!is_storage_compatible<typename super::value_type>(obj))
{
if (trace_conversion)
@@ -240,7 +244,7 @@ namespace
return 0;
}
- if (!PyArray_CHKFLAGS(obj, NPY_C_CONTIGUOUS))
+ if (!PyArray_CHKFLAGS(obj, NPY_ARRAY_C_CONTIGUOUS))
{
if (trace_conversion)
std::cerr
@@ -260,7 +264,7 @@ namespace
return 0;
}
- if (!PyArray_CHKFLAGS(obj, NPY_F_CONTIGUOUS))
+ if (!PyArray_CHKFLAGS(obj, NPY_ARRAY_F_CONTIGUOUS))
{
if (trace_conversion)
std::cerr
--
1.8.1.5
From c8f965f135bfea0355f0ee011da40109f5dc7824 Mon Sep 17 00:00:00 2001
From: Christoph Koke <koke at kip.uni-heidelberg.de>
Date: Tue, 1 Oct 2013 16:33:43 +0200
Subject: [PATCH 2/2] added compatibility fixes for numpy 1.6.2
---
pyublas/include/pyublas/numpy.hpp | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/pyublas/include/pyublas/numpy.hpp b/pyublas/include/pyublas/numpy.hpp
index 7de5178..5518834 100644
--- a/pyublas/include/pyublas/numpy.hpp
+++ b/pyublas/include/pyublas/numpy.hpp
@@ -46,6 +46,26 @@
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>
+#if NPY_FEATURE_VERSION < 0x00000007
+
+#define NPY_ARRAY_ALIGNED NPY_ALIGNED
+#define NPY_ARRAY_NOTSWAPPED NPY_NOTSWAPPED
+#define NPY_ARRAY_C_CONTIGUOUS NPY_C_CONTIGUOUS
+#define NPY_ARRAY_F_CONTIGUOUS NPY_F_CONTIGUOUS
+#define NPY_ARRAY_CARRAY NPY_CARRAY
+#define NPY_ARRAY_FARRAY NPY_FARRAY
+
+namespace
+{
+ inline int PyArray_SetBaseObject(PyArrayObject * obj, PyObject * base)
+ {
+ PyArray_BASE(obj) = base;
+ return 0;
+ }
+}
+
+#endif
+
namespace
{
static struct pyublas_array_importer
--
1.8.1.5
_______________________________________________
Pyublas mailing list
Pyublas at tiker.net
http://lists.tiker.net/listinfo/pyublas
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 827 bytes
Desc: not available
URL: <http://lists.tiker.net/pipermail/pyublas/attachments/20131127/6ee13f9b/attachment-0001.sig>