Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions numba_cuda/numba/cuda/api_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions numba_cuda/numba/cuda/cudadrv/devicearray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions numba_cuda/numba/cuda/tests/cudadrv/test_cuda_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down