Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commita9a9bf8

Browse files
authored
Merge pull request#28600 from karl3wm/writeable-from-dlpack
BUG: Set writeable flag for writeable dlpacks.
2 parents2e3cf31 +155f32b commita9a9bf8

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

‎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 aread-onlyview
1667-
of the inputobject. 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
returnNULL;
602602
}
603603
dl_tensor=managed->dl_tensor;
604-
readonly=0;
604+
readonly=1;
605605
}
606606

607607
constintndim=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
returnNULL;
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
assertnoty.flags.writeable
146146

147+
deftest_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+
asserty.flags.writeable
153+
154+
# old dlpacks are not writeable for backwards compatibility
155+
y=np.from_dlpack(x_old)
156+
assertnoty.flags.writeable
157+
147158
deftest_ndim0(self):
148159
x=np.array(1.0)
149160
y=np.from_dlpack(x)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp