Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add method ndarray::set_flag and corresponding test #224

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/boost/python/numpy/ndarray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 flag, bool on);

/// @brief Reverse the dimensions of the array.
ndarray transpose() const;

Expand Down
8 changes: 8 additions & 0 deletions src/numpy/ndarray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PyArrayObject*>(this->ptr()), x);
else
PyArray_CLEARFLAGS(reinterpret_cast<PyArrayObject*>(this->ptr()), x);
}

ndarray ndarray::transpose() const
{
return ndarray(python::detail::new_reference
Expand Down
5 changes: 5 additions & 0 deletions test/numpy/ndarray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
10 changes: 10 additions & 0 deletions test/numpy/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()