8000 BUG: Set writeable flag for writeable dlpacks. · numpy/numpy@a36442b · GitHub
[go: up one dir, main page]

Skip to content

Commit a36442b

Browse files
author
karl
committed
BUG: Set writeable flag for writeable dlpacks.
Explicitly set the writeable flag in from_dlpack as the inverse of the dlpack read_only flag. Previously it was not actually being set. Additionally, update the readonly logic such that legacy unversioned DLPacks are never writeable, for compatibility with old behavior. Fixes #28599
1 parent 9389862 commit a36442b

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
`from_dlpack` can return writeable arrays
2+
-------------------------------------------
3+
4+
A user can now write back into underlying data through the array returned from
5+
`from_dlpack` when using a modern DLPack 1.0 producer.
6+
7+
The ``DLPACK_FLAG_BITMASK_READ_ONLY`` flag must be available and unset in the
8+
DLPack. For compatibility, the old behavior of always returning readonly
9+
arrays is retained when processing older DLPacks without this flag.

numpy/_core/_add_newdocs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,8 +1663,8 @@
16631663
from_dlpack(x, /, *, device=None, copy=None)
16641664
16651665
Create a NumPy array from an object implementing the ``__dlpack__``
1666-
protocol. Generally, the returned NumPy array is a read-only view
1667-
of the input object. See [1]_ and [2]_ for more details.
1666+
protocol. Generally, the returned NumPy array is a view of the input
1667+
object. See [1]_ and [2]_ for more details.
16681668
16691669
Parameters
16701670
----------

numpy/_core/src/multiarray/dlpack.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ from_dlpack(PyObject *NPY_UNUSED(self),
601601
return NULL;
602602
}
603603
dl_tensor = managed->dl_tensor;
604-
readonly = 0;
604+
readonly = 1;
605605
}
606606

607607
const int ndim = dl_tensor.ndim;
@@ -702,14 +702,13 @@ from_dlpack(PyObject *NPY_UNUSED(self),
702702
}
703703

704704
PyObject *ret = PyArray_NewFromDescr(&PyArray_Type, descr, ndim, shape,
705-
dl_tensor.strides != NULL ? strides : NULL, data, 0, NULL);
705+
dl_tensor.strides != NULL ? strides : NULL, data, readonly ? 0 :
706+
NPY_ARRAY_WRITEABLE, NULL);
707+
706708
if (ret == NULL) {
707709
Py_DECREF(capsule);
708710
return NULL;
709711
}
710-
if (readonly) {
711-
PyArray_CLEARFLAGS((PyArrayObject *)ret, NPY_ARRAY_WRITEABLE);
712-
}
713712

714713
PyObject *new_capsule;
715714
if (versioned) {

numpy/_core/tests/test_dlpack.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ def test_readonly(self):
144144
y = np.from_dlpack(x)
145145
assert not y.flags.writeable
146146

147+
def test_writeable(self):
148+
x_new, x_old = new_and_old_dlpack()
149+
150+
# new dlpacks respect writeability
151+
y = np.from_dlpack(x_new)
152+
assert y.flags.writeable
153+
154+
# old dlpacks are not writeable for backwards compatibility
155+
y = np.from_dlpack(x_old)
156+
assert not y.flags.writeable
157+
147158
def test_ndim0(self):
148159
x = np.array(1.0)
149160
y = np.from_dlpack(x)

0 commit comments

Comments
 (0)
0