diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/dlpack.c | 6 | ||||
-rw-r--r-- | numpy/core/tests/test_dlpack.py | 8 |
2 files changed, 13 insertions, 1 deletions
diff --git a/numpy/core/src/multiarray/dlpack.c b/numpy/core/src/multiarray/dlpack.c index 21930b0ef..f061a6bf9 100644 --- a/numpy/core/src/multiarray/dlpack.c +++ b/numpy/core/src/multiarray/dlpack.c @@ -131,6 +131,12 @@ array_dlpack(PyArrayObject *self, return NULL; } + if ( !(PyArray_FLAGS(self) & NPY_ARRAY_WRITEABLE)) { + PyErr_SetString(PyExc_TypeError, "NumPy currently only supports " + "dlpack for writeable arrays"); + return NULL; + } + npy_intp itemsize = PyArray_ITEMSIZE(self); int ndim = PyArray_NDIM(self); npy_intp *strides = PyArray_STRIDES(self); diff --git a/numpy/core/tests/test_dlpack.py b/numpy/core/tests/test_dlpack.py index 06fc042ec..f848b2008 100644 --- a/numpy/core/tests/test_dlpack.py +++ b/numpy/core/tests/test_dlpack.py @@ -78,7 +78,7 @@ class TestDLPack: y4 = x[1] assert_array_equal(y4, np._from_dlpack(y4)) - y5 = np.diagonal(x) + y5 = np.diagonal(x).copy() assert_array_equal(y5, np._from_dlpack(y5)) @pytest.mark.parametrize("ndim", range(33)) @@ -101,3 +101,9 @@ class TestDLPack: def test_dlpack_destructor_exception(self): with pytest.raises(RuntimeError): self.dlpack_deleter_exception() + + def test_readonly(self): + x = np.arange(5) + x.flags.writeable = False + with pytest.raises(TypeError): + x.__dlpack__() |