Skip to content

Commit 9b0a3cd

Browse files
authored
Resolves gh-1512 (#1513)
dtype is passed to np.asarray when dpt.asarray is called with a Python scalar as input This guarantees the expected OverflowError is thrown
1 parent 1cd0b96 commit 9b0a3cd

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

dpctl/tensor/_ctors.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -632,17 +632,13 @@ def asarray(
632632
usm_type=usm_type,
633633
order=order,
634634
)
635-
636-
raise NotImplementedError(
637-
"Converting Python sequences is not implemented"
638-
)
639635
if copy is False:
640636
raise ValueError(
641637
f"Converting {type(obj)} to usm_ndarray requires a copy"
642638
)
643639
# obj is a scalar, create 0d array
644640
return _asarray_from_numpy_ndarray(
645-
np.asarray(obj),
641+
np.asarray(obj, dtype=dtype),
646642
dtype=dtype,
647643
usm_type=usm_type,
648644
sycl_queue=sycl_queue,

dpctl/tests/test_tensor_asarray.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,21 @@ def test_asarray_input_validation():
164164
with pytest.raises(ValueError):
165165
# sequence is not rectangular
166166
dpt.asarray([[1], 2])
167+
with pytest.raises(OverflowError):
168+
# Python int too large for type
169+
dpt.asarray(-9223372036854775809, dtype="i4")
170+
with pytest.raises(ValueError):
171+
# buffer to usm_ndarray requires a copy
172+
dpt.asarray(memoryview(np.arange(5)), copy=False)
173+
with pytest.raises(ValueError):
174+
# Numpy array to usm_ndarray requires a copy
175+
dpt.asarray(np.arange(5), copy=False)
176+
with pytest.raises(ValueError):
177+
# Python sequence to usm_ndarray requires a copy
178+
dpt.asarray([1, 2, 3], copy=False)
179+
with pytest.raises(ValueError):
180+
# Python scalar to usm_ndarray requires a copy
181+
dpt.asarray(5, copy=False)
167182

168183

169184
def test_asarray_input_validation2():

0 commit comments

Comments
 (0)