diff --git a/cuda_core/cuda/core/_memoryview.pyx b/cuda_core/cuda/core/_memoryview.pyx index 94824e1c4d..0e1df726c0 100644 --- a/cuda_core/cuda/core/_memoryview.pyx +++ b/cuda_core/cuda/core/_memoryview.pyx @@ -1096,6 +1096,8 @@ cpdef StridedMemoryView view_as_cai(obj, stream_ptr, view=None): buf.exporting_obj = obj buf.metadata = cai_data buf.dl_tensor = NULL + # Validate shape/strides/typestr eagerly so constructor paths fail fast. + buf.get_layout() buf.ptr, buf.readonly = cai_data["data"] buf.is_device_accessible = True if buf.ptr != 0: @@ -1138,6 +1140,8 @@ cpdef StridedMemoryView view_as_array_interface(obj, view=None): buf.exporting_obj = obj buf.metadata = data buf.dl_tensor = NULL + # Validate shape/strides/typestr eagerly so constructor paths fail fast. + buf.get_layout() buf.ptr, buf.readonly = data["data"] buf.is_device_accessible = False buf.device_id = handle_return(driver.cuCtxGetDevice()) diff --git a/cuda_core/tests/test_utils.py b/cuda_core/tests/test_utils.py index 9e79f48313..e7ebb5bb52 100644 --- a/cuda_core/tests/test_utils.py +++ b/cuda_core/tests/test_utils.py @@ -582,10 +582,53 @@ def test_from_array_interface_unsupported_strides(init_cuda): # Create an array with strides that aren't a multiple of itemsize x = np.array([(1, 2.0), (3, 4.0)], dtype=[("a", "i4"), ("b", "f8")]) b = x["b"] - smv = StridedMemoryView.from_array_interface(b) with pytest.raises(ValueError, match="strides must be divisible by itemsize"): - # TODO: ideally this would raise on construction - smv.strides # noqa: B018 + StridedMemoryView.from_array_interface(b) + + +def _make_cuda_array_interface_obj(*, shape, strides, typestr="