From 8ddb5a8794de5a72f366f16cd4f16624f4964e23 Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Mon, 9 Sep 2024 08:19:21 +0900 Subject: [PATCH] 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. --- src/nb_ndarray.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/nb_ndarray.cpp b/src/nb_ndarray.cpp index df04f3de..58ec12b8 100644 --- a/src/nb_ndarray.cpp +++ b/src/nb_ndarray.cpp @@ -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; } }