Skip to content

Commit

Permalink
ndarray: improve error message
Browse files Browse the repository at this point in the history
The ``.copy()`` step fails when attempting to copy an nd-array that does
not have a framework identifier set. That's okay, but the error message
should be more informative.
  • Loading branch information
wjakob committed Sep 9, 2024
1 parent 87711b0 commit 8ddb5a8
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/nb_ndarray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -766,12 +766,19 @@ PyObject *ndarray_wrap(ndarray_handle *th, ndarray_framework framework,
}

if (copy) {
try {
o = o.attr("copy")();
} catch (std::exception &e) {
if (package.is_valid()) {
try {
o = o.attr("copy")();
} catch (std::exception &e) {
PyErr_Format(PyExc_RuntimeError,
"nanobind::detail::ndarray_wrap(): copy failed: %s",
e.what());
return nullptr;
}
} else {
PyErr_Format(PyExc_RuntimeError,
"nanobind::detail::ndarray_wrap(): copy failed: %s",
e.what());
"nanobind::detail::ndarray_wrap(): copy failed, must "
"specify a target framework!");
return nullptr;
}
}
Expand Down

0 comments on commit 8ddb5a8

Please sign in to comment.