diff --git a/pyopencl/array.py b/pyopencl/array.py index bcc0770fa4a1446cb8a7e6c481f4ffc2ffe55f83..73391f79e44582e4598b30bd7dd6f9c912442810 100644 --- a/pyopencl/array.py +++ b/pyopencl/array.py @@ -448,6 +448,9 @@ class Array(object): size = shape shape = (shape,) + if any(dim < 0 for dim in shape): + raise ValueError("negative dimensions are not allowed") + if isinstance(size, np.integer): size = size.item() diff --git a/test/test_array.py b/test/test_array.py index 521f6719474f7a05a98a100bb9a6183018caa5df..d1e08346f7ede00e7f95f22b61bd18e53c9ef405 100644 --- a/test/test_array.py +++ b/test/test_array.py @@ -1316,6 +1316,25 @@ def test_outoforderqueue_reductions(ctx_factory): assert b1 == a.sum() and b2 == a.dot(3 - a) and b3 == 0 +def test_negative_dim_rejection(ctx_factory): + context = ctx_factory() + queue = cl.CommandQueue(context) + + with pytest.raises(ValueError): + cl_array.Array(queue, shape=-10, dtype=np.float) + + with pytest.raises(ValueError): + cl_array.Array(queue, shape=(-10,), dtype=np.float) + + for left_dim in (-1, 0, 1): + with pytest.raises(ValueError): + cl_array.Array(queue, shape=(left_dim, -1), dtype=np.float) + + for right_dim in (-1, 0, 1): + with pytest.raises(ValueError): + cl_array.Array(queue, shape=(-1, right_dim), dtype=np.float) + + if __name__ == "__main__": if len(sys.argv) > 1: exec(sys.argv[1])