summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authormattip <matti.picus@gmail.com>2021-11-04 00:11:04 +0200
committermattip <matti.picus@gmail.com>2021-11-04 00:11:04 +0200
commitc38b7f328b87d626be544c5ffb542f2b6a97adee (patch)
tree0fda3ad2cfbadf9b9cc0f1ba1dabdae1f90b2dbb /numpy
parentba8fcbe2ba0a26cd52dfa9bf40dd2e945e5b298f (diff)
downloadnumpy-c38b7f328b87d626be544c5ffb542f2b6a97adee.tar.gz
make a.__dlpack__() fail if a is readonly
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/dlpack.c6
-rw-r--r--numpy/core/tests/test_dlpack.py8
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__()