From a3df00da0eb68adc00b657763b41d0eecf8211db Mon Sep 17 00:00:00 2001 From: Hans Dembinski Date: Wed, 1 Aug 2018 23:11:40 +0200 Subject: [PATCH 1/2] add method ndarray::set_flag and corresponding test --- include/boost/python/numpy/ndarray.hpp | 3 +++ src/numpy/ndarray.cpp | 8 ++++++++ test/numpy/ndarray.cpp | 5 +++++ test/numpy/ndarray.py | 10 ++++++++++ 4 files changed, 26 insertions(+) diff --git a/include/boost/python/numpy/ndarray.hpp b/include/boost/python/numpy/ndarray.hpp index 2cb3b509f8..c443446aea 100644 --- a/include/boost/python/numpy/ndarray.hpp +++ b/include/boost/python/numpy/ndarray.hpp @@ -121,6 +121,9 @@ class BOOST_NUMPY_DECL ndarray : public object /// @brief Return the array flags. bitflag get_flags() const; + /// @brief Set array flag. This can destroy the internal consistency, use with care! + void set_flag(bitflag, bool on); + /// @brief Reverse the dimensions of the array. ndarray transpose() const; diff --git a/src/numpy/ndarray.cpp b/src/numpy/ndarray.cpp index af09ecc338..c0b1188ee1 100644 --- a/src/numpy/ndarray.cpp +++ b/src/numpy/ndarray.cpp @@ -210,6 +210,14 @@ ndarray::bitflag ndarray::get_flags() const return numpy::detail::numpy_to_bitflag(get_struct()->flags); } +void ndarray::set_flag(ndarray::bitflag flag, bool on) { + const int x = numpy::detail::bitflag_to_numpy(flag); + if (on) + PyArray_ENABLEFLAGS(reinterpret_cast(this->ptr()), x); + else + PyArray_CLEARFLAGS(reinterpret_cast(this->ptr()), x); +} + ndarray ndarray::transpose() const { return ndarray(python::detail::new_reference diff --git a/test/numpy/ndarray.cpp b/test/numpy/ndarray.cpp index 75a1010435..4df27548c3 100644 --- a/test/numpy/ndarray.cpp +++ b/test/numpy/ndarray.cpp @@ -34,6 +34,9 @@ np::ndarray reshape(np::ndarray arr,p::tuple tup) { return arr.reshape(tup);} Py_intptr_t shape_index(np::ndarray arr,int k) { return arr.shape(k); } Py_intptr_t strides_index(np::ndarray arr,int k) { return arr.strides(k); } +bool is_writeable(np::ndarray arr) { return arr.get_flags() & np::ndarray::WRITEABLE; } +void set_writeable(np::ndarray arr, bool on) { arr.set_flag(np::ndarray::WRITEABLE, on); } + BOOST_PYTHON_MODULE(ndarray_ext) { np::initialize(); @@ -48,4 +51,6 @@ BOOST_PYTHON_MODULE(ndarray_ext) p::def("reshape", reshape); p::def("shape_index", shape_index); p::def("strides_index", strides_index); + p::def("is_writeable", is_writeable); + p::def("set_writeable", set_writeable); } diff --git a/test/numpy/ndarray.py b/test/numpy/ndarray.py index 2acc384a52..986ae6a614 100644 --- a/test/numpy/ndarray.py +++ b/test/numpy/ndarray.py @@ -107,6 +107,16 @@ def strides_check(i): except IndexError: pass + def testArrayFlags(self): + a = numpy.arange(24) + old_flags = a.flags + self.assertTrue(a.flags.writeable) + self.assertTrue(ndarray_ext.is_writeable(a)) + ndarray_ext.set_writeable(a, False) + self.assertFalse(a.flags.writeable) + self.assertFalse(ndarray_ext.is_writeable(a)) + ndarray_ext.set_writeable(a, True) + self.assertEqual(old_flags, a.flags) if __name__=="__main__": unittest.main() From 11a895075d7869e9eff6f381028bbef044b0d7d3 Mon Sep 17 00:00:00 2001 From: Hans Dembinski Date: Wed, 1 Aug 2018 23:14:07 +0200 Subject: [PATCH 2/2] missing arg, added for consistency --- include/boost/python/numpy/ndarray.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/boost/python/numpy/ndarray.hpp b/include/boost/python/numpy/ndarray.hpp index c443446aea..2be0a25a74 100644 --- a/include/boost/python/numpy/ndarray.hpp +++ b/include/boost/python/numpy/ndarray.hpp @@ -122,7 +122,7 @@ class BOOST_NUMPY_DECL ndarray : public object bitflag get_flags() const; /// @brief Set array flag. This can destroy the internal consistency, use with care! - void set_flag(bitflag, bool on); + void set_flag(bitflag flag, bool on); /// @brief Reverse the dimensions of the array. ndarray transpose() const;