Skip to content

Commit

Permalink
incorporate feedback by @WKarel
Browse files Browse the repository at this point in the history
  • Loading branch information
wjakob committed Jun 14, 2023
1 parent f3e0ef1 commit 6f0c3fe
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
3 changes: 2 additions & 1 deletion docs/ndarray.rst
Expand Up @@ -353,7 +353,8 @@ interpreted as follows:
ndarray's ``owner`` field to a method's ``self`` argument. It fails with an
error if there is already a different owner.

- :cpp:enumerator:`rv_policy::move` is unsupported.
- :cpp:enumerator:`rv_policy::move` is unsupported and demoted to
:cpp:enumerator:`rv_policy::copy`.

Limitations
-----------
Expand Down
5 changes: 5 additions & 0 deletions include/nanobind/ndarray.h
Expand Up @@ -298,10 +298,15 @@ template <typename... Args> class ndarray {
dlpack::dtype dtype = nanobind::dtype<Scalar>(),
int32_t device_type = device::cpu::value,
int32_t device_id = 0) {

if (strides.size() != 0 && strides.size() != shape.size())
detail::fail("ndarray(): shape and strides have incompatible size!");

m_handle = detail::ndarray_create(
(void *) value, shape.size(), shape.begin(), owner.ptr(),
(strides.size() == 0) ? nullptr : strides.begin(), &dtype,
std::is_const_v<Scalar>, device_type, device_id);

m_dltensor = *detail::ndarray_inc_ref(m_handle);
}

Expand Down
9 changes: 2 additions & 7 deletions src/nb_ndarray.cpp
Expand Up @@ -614,7 +614,7 @@ PyObject *ndarray_wrap(ndarray_handle *th, int framework,
bool copy;
switch (policy) {
case rv_policy::reference_internal:
if (cleanup->self() != th->owner) {
if (cleanup && cleanup->self() != th->owner) {
if (th->owner) {
PyErr_SetString(PyExc_RuntimeError,
"nanobind::detail::ndarray_wrap(): "
Expand All @@ -633,15 +633,10 @@ PyObject *ndarray_wrap(ndarray_handle *th, int framework,
break;

case rv_policy::copy:
case rv_policy::move:
copy = true;
break;

case rv_policy::move:
PyErr_SetString(PyExc_RuntimeError,
"nanobind::detail::ndarray_wrap(): rv_policy::move "
"is not supported!");
return nullptr;

default:
copy = false;
break;
Expand Down

0 comments on commit 6f0c3fe

Please sign in to comment.