From 5929d6b9baae2ed5fc4978bae6e870f6bd92dc0d Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Wed, 29 Mar 2023 16:23:27 -0500 Subject: [PATCH] Improved error handling for usm_ndarray.__setitem__ ```python import dpctl.tensor as dpt, numpy as np u, n = dpt.empty(5), np.ones(5, dtype="O") u[...] = n ``` This example used to raise a cascade of exceptions starting with cryptic "RuntimeError: Unrecogized typenum 17 encountered.", where typenum 17 corresponds to type object. After this change, the error message is crisper: ``` ValueError: Input of type can not be \ assigned to usm_ndarray because of unsupported data \ type 'object' ``` --- dpctl/tensor/_usmarray.pyx | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/dpctl/tensor/_usmarray.pyx b/dpctl/tensor/_usmarray.pyx index 570490dd8d..37f192a9dd 100644 --- a/dpctl/tensor/_usmarray.pyx +++ b/dpctl/tensor/_usmarray.pyx @@ -1110,13 +1110,19 @@ cdef class usm_ndarray: "converted to usm_ndarray" ) else: + rhs_np = np.asarray(rhs) + if type_bytesize(rhs_np.dtype.num) < 0: + raise ValueError( + f"Input of type {type(rhs)} can not be " + "assigned to usm_ndarray because of " + f"unsupported data type '{rhs_np.dtype}'" + ) try: - rhs_np = np.asarray(rhs) _copy_from_numpy_into(Xv, rhs_np) except Exception: raise ValueError( f"Input of type {type(rhs)} could not be " - "converted to usm_ndarray" + "copied into dpctl.tensor.usm_ndarray" ) return