From fd7517037b0e108d8acef539ce4d33ae3bde02fc Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Fri, 20 Jan 2017 13:50:07 -0500 Subject: [PATCH] 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. --- include/pybind11/numpy.h | 2 +- tests/test_numpy_array.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index f2c4d9ce..ea9914a4 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -536,7 +536,7 @@ protected: void check_writeable() const { if (!writeable()) - throw std::runtime_error("array is not writeable"); + throw std::domain_error("array is not writeable"); } static std::vector default_strides(const std::vector& shape, size_t itemsize) { diff --git a/tests/test_numpy_array.py b/tests/test_numpy_array.py index 0094d761..bcc15d2e 100644 --- a/tests/test_numpy_array.py +++ b/tests/test_numpy_array.py @@ -92,7 +92,7 @@ def test_mutate_readonly(arr): from pybind11_tests.array import mutate_data, mutate_data_t, mutate_at_t arr.flags.writeable = False 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) assert str(excinfo.value) == 'array is not writeable'