Commit fd751703 by Jason Rhinelander Committed by Wenzel Jakob

Change array's writeable exception to a ValueError

Numpy raises ValueError when attempting to modify an array, while
py::array is raising a RuntimeError.  This changes the exception to a
std::domain_error, which gets mapped to the expected ValueError in
python.
parent f86dddf7
...@@ -536,7 +536,7 @@ protected: ...@@ -536,7 +536,7 @@ protected:
void check_writeable() const { void check_writeable() const {
if (!writeable()) if (!writeable())
throw std::runtime_error("array is not writeable"); throw std::domain_error("array is not writeable");
} }
static std::vector<size_t> default_strides(const std::vector<size_t>& shape, size_t itemsize) { static std::vector<size_t> default_strides(const std::vector<size_t>& shape, size_t itemsize) {
......
...@@ -92,7 +92,7 @@ def test_mutate_readonly(arr): ...@@ -92,7 +92,7 @@ def test_mutate_readonly(arr):
from pybind11_tests.array import mutate_data, mutate_data_t, mutate_at_t from pybind11_tests.array import mutate_data, mutate_data_t, mutate_at_t
arr.flags.writeable = False arr.flags.writeable = False
for func, args in (mutate_data, ()), (mutate_data_t, ()), (mutate_at_t, (0, 0)): for func, args in (mutate_data, ()), (mutate_data_t, ()), (mutate_at_t, (0, 0)):
with pytest.raises(RuntimeError) as excinfo: with pytest.raises(ValueError) as excinfo:
func(arr, *args) func(arr, *args)
assert str(excinfo.value) == 'array is not writeable' assert str(excinfo.value) == 'array is not writeable'
......
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