From 5015aa8e94ac854267fcd383bef9b8678bc2443d Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Fri, 26 Jan 2024 12:12:41 -0800 Subject: [PATCH] Resolves gh-1512 dtype is passed to np.asarray when dpt.asarray is called with a Python scalar as input This guarantees the expected OverflowError is thrown --- dpctl/tensor/_ctors.py | 6 +----- dpctl/tests/test_tensor_asarray.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/dpctl/tensor/_ctors.py b/dpctl/tensor/_ctors.py index ba16f0f1fc..5c5c7279db 100644 --- a/dpctl/tensor/_ctors.py +++ b/dpctl/tensor/_ctors.py @@ -632,17 +632,13 @@ def asarray( usm_type=usm_type, order=order, ) - - raise NotImplementedError( - "Converting Python sequences is not implemented" - ) if copy is False: raise ValueError( f"Converting {type(obj)} to usm_ndarray requires a copy" ) # obj is a scalar, create 0d array return _asarray_from_numpy_ndarray( - np.asarray(obj), + np.asarray(obj, dtype=dtype), dtype=dtype, usm_type=usm_type, sycl_queue=sycl_queue, diff --git a/dpctl/tests/test_tensor_asarray.py b/dpctl/tests/test_tensor_asarray.py index e73c35ce26..8278bd34eb 100644 --- a/dpctl/tests/test_tensor_asarray.py +++ b/dpctl/tests/test_tensor_asarray.py @@ -164,6 +164,21 @@ def test_asarray_input_validation(): with pytest.raises(ValueError): # sequence is not rectangular dpt.asarray([[1], 2]) + with pytest.raises(OverflowError): + # Python int too large for type + dpt.asarray(-9223372036854775809, dtype="i4") + with pytest.raises(ValueError): + # buffer to usm_ndarray requires a copy + dpt.asarray(memoryview(np.arange(5)), copy=False) + with pytest.raises(ValueError): + # Numpy array to usm_ndarray requires a copy + dpt.asarray(np.arange(5), copy=False) + with pytest.raises(ValueError): + # Python sequence to usm_ndarray requires a copy + dpt.asarray([1, 2, 3], copy=False) + with pytest.raises(ValueError): + # Python scalar to usm_ndarray requires a copy + dpt.asarray(5, copy=False) def test_asarray_input_validation2():