diff --git a/numba_cuda/numba/cuda/api_util.py b/numba_cuda/numba/cuda/api_util.py index 1b2694af7..bc5bb8e41 100644 --- a/numba_cuda/numba/cuda/api_util.py +++ b/numba_cuda/numba/cuda/api_util.py @@ -3,6 +3,12 @@ def prepare_shape_strides_dtype(shape, strides, dtype, order): dtype = np.dtype(dtype) + if isinstance(shape, (float, np.floating)): + raise TypeError("shape must be an integer or tuple of integers") + if isinstance(shape, np.ndarray) and np.issubdtype( + shape.dtype, np.floating + ): + raise TypeError("shape must be an integer or tuple of integers") if isinstance(shape, int): shape = (shape,) if isinstance(strides, int): diff --git a/numba_cuda/numba/cuda/cudadrv/devicearray.py b/numba_cuda/numba/cuda/cudadrv/devicearray.py index 73180e43e..819d8a6e4 100644 --- a/numba_cuda/numba/cuda/cudadrv/devicearray.py +++ b/numba_cuda/numba/cuda/cudadrv/devicearray.py @@ -92,6 +92,9 @@ def __init__(self, shape, strides, dtype, stream=0, gpu_data=None): self._dummy = dummyarray.Array.from_desc( 0, shape, strides, dtype.itemsize ) + # confirm that all elements of shape are ints + if not all(isinstance(dim, (int, np.integer)) for dim in shape): + raise TypeError("all elements of shape must be ints") self.shape = tuple(shape) self.strides = tuple(strides) self.dtype = dtype diff --git a/numba_cuda/numba/cuda/tests/cudadrv/test_cuda_ndarray.py b/numba_cuda/numba/cuda/tests/cudadrv/test_cuda_ndarray.py index ebeb1e8cd..398211d6a 100644 --- a/numba_cuda/numba/cuda/tests/cudadrv/test_cuda_ndarray.py +++ b/numba_cuda/numba/cuda/tests/cudadrv/test_cuda_ndarray.py @@ -45,6 +45,39 @@ def test_devicearray_shape(self): self.assertEqual(ary.shape, dary.shape) self.assertEqual(ary.shape[1:], dary.shape[1:]) + def test_device_array_float(self): + # Ensure that a float shape raises an TypeError + with self.assertRaises(TypeError): + cuda.device_array(shape=1.23) + with self.assertRaises(TypeError): + cuda.device_array(shape=np.float64(1.23)) + with self.assertRaises(TypeError): + cuda.device_array(shape=np.array(1.23)) + + def test_device_array_float_vectors(self): + # Ensure that np.array, list or tuple inputs with + # non-ints raise an TypeError + with self.assertRaises(TypeError): + cuda.device_array(shape=np.array([1.1])) + with self.assertRaises(TypeError): + cuda.device_array(shape=[1.1]) + with self.assertRaises(TypeError): + cuda.device_array(shape=(1.1,)) + with self.assertRaises(TypeError): + cuda.device_array(shape=np.array([1.1, 2.2])) + with self.assertRaises(TypeError): + cuda.device_array(shape=[1.1, 2.2]) + with self.assertRaises(TypeError): + cuda.device_array(shape=(1.1, 2.2)) + + def test_device_array_vectors(self): + # Ensure that np.array or list of inputs with + # ints still work + dary = cuda.device_array(shape=np.array([10, 10]), dtype=np.bool) + self.assertEqual(dary.shape, (10, 10)) + dary = cuda.device_array(shape=[10, 10], dtype=np.bool) + self.assertEqual(dary.shape, (10, 10)) + def test_devicearray(self): array = np.arange(100, dtype=np.int32) original = array.copy()