Skip to content

Commit 8ddb5a8

Browse files
committed
ndarray: improve error message
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.
1 parent 87711b0 commit 8ddb5a8

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/nb_ndarray.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -766,12 +766,19 @@ PyObject *ndarray_wrap(ndarray_handle *th, ndarray_framework framework,
766766
}
767767

768768
if (copy) {
769-
try {
770-
o = o.attr("copy")();
771-
} catch (std::exception &e) {
769+
if (package.is_valid()) {
770+
try {
771+
o = o.attr("copy")();
772+
} catch (std::exception &e) {
773+
PyErr_Format(PyExc_RuntimeError,
774+
"nanobind::detail::ndarray_wrap(): copy failed: %s",
775+
e.what());
776+
return nullptr;
777+
}
778+
} else {
772779
PyErr_Format(PyExc_RuntimeError,
773-
"nanobind::detail::ndarray_wrap(): copy failed: %s",
774-
e.what());
780+
"nanobind::detail::ndarray_wrap(): copy failed, must "
781+
"specify a target framework!");
775782
return nullptr;
776783
}
777784
}

0 commit comments

Comments
 (0)