Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7f3361f

Browse filesBrowse files
authored
Merge pull request #28632 from charris/backport-28600
BUG: Set writeable flag for writeable dlpacks.
2 parents 55d385d + 9dadd45 commit 7f3361f
Copy full SHA for 7f3361f

File tree

Expand file treeCollapse file tree

3 files changed

+17
-7
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+17
-7
lines changed

‎numpy/_core/_add_newdocs.py

Copy file name to clipboardExpand all lines: numpy/_core/_add_newdocs.py
+2-2Lines 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

Copy file name to clipboardExpand all lines: numpy/_core/src/multiarray/dlpack.c
+4-5Lines 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

Copy file name to clipboardExpand all lines: numpy/_core/tests/test_dlpack.py
+11Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.