summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/release/upcoming_changes/19083.new_feature.rst6
-rw-r--r--numpy/core/src/multiarray/dlpack.c8
2 files changed, 13 insertions, 1 deletions
diff --git a/doc/release/upcoming_changes/19083.new_feature.rst b/doc/release/upcoming_changes/19083.new_feature.rst
new file mode 100644
index 000000000..92f00c0d6
--- /dev/null
+++ b/doc/release/upcoming_changes/19083.new_feature.rst
@@ -0,0 +1,6 @@
+Add NEP 47-compatible dlpack support
+------------------------------------
+
+Add a ``ndarray.__dlpack__()`` method which returns a ``dlpack`` C structure
+wrapped in a ``PyCapsule``. Also add a ``np._from_dlpack(obj)`` function, where
+``obj`` supports ``__dlpack__()``, and returns an ``ndarray``.
diff --git a/numpy/core/src/multiarray/dlpack.c b/numpy/core/src/multiarray/dlpack.c
index f061a6bf9..b0eaa7786 100644
--- a/numpy/core/src/multiarray/dlpack.c
+++ b/numpy/core/src/multiarray/dlpack.c
@@ -206,6 +206,12 @@ array_dlpack(PyArrayObject *self,
if (data == NULL) {
return NULL;
}
+ if ((char *)PyArray_DATA(self) - data != 0) {
+ PyErr_SetString(PyExc_TypeError,
+ "Offsets not clearly supported by this "
+ "version of DLPack.");
+ return NULL;
+ }
DLManagedTensor *managed = PyMem_Malloc(sizeof(DLManagedTensor));
if (managed == NULL) {
@@ -238,7 +244,7 @@ array_dlpack(PyArrayObject *self,
if (PyArray_SIZE(self) != 1 && !PyArray_IS_C_CONTIGUOUS(self)) {
managed->dl_tensor.strides = managed_strides;
}
- managed->dl_tensor.byte_offset = (char *)PyArray_DATA(self) - data;
+ managed->dl_tensor.byte_offset = 0;
managed->manager_ctx = self;
managed->deleter = array_dlpack_deleter;