def test_strides_not_multiple_of_itemsize(self): dt = np.dtype([('int', np.int32), ('char', np.int8)]) y = np.zeros((5, ), dtype=dt) z = y['int'] with pytest.raises(RuntimeError): np._from_dlpack(z)
def test_dlpack_device(self): x = np.arange(5) assert x.__dlpack_device__() == (1, 0) y = np._from_dlpack(x) assert y.__dlpack_device__() == (1, 0) z = y[::2] assert z.__dlpack_device__() == (1, 0)
def test_non_contiguous(self): x = np.arange(25).reshape((5, 5)) y1 = x[0] assert_array_equal(y1, np._from_dlpack(y1)) y2 = x[:, 0] assert_array_equal(y2, np._from_dlpack(y2)) y3 = x[1, :] assert_array_equal(y3, np._from_dlpack(y3)) y4 = x[1] assert_array_equal(y4, np._from_dlpack(y4)) y5 = np.diagonal(x).copy() assert_array_equal(y5, np._from_dlpack(y5))
def test_size1dims_arrays(self): x = np.ndarray(dtype='f8', shape=(10, 5, 1), strides=(8, 80, 4), buffer=np.ones(1000, dtype=np.uint8), order='F') y = np._from_dlpack(x) assert_array_equal(x, y)
def test_dlpack_device(self): x = np.arange(5) assert x.__dlpack_device__() == (1, 0) assert np._from_dlpack(x).__dlpack_device__() == (1, 0)
def test_higher_dims(self, ndim): shape = (1, ) * ndim x = np.zeros(shape, dtype=np.float64) assert shape == np._from_dlpack(x).shape
def test_invalid_byte_swapping(self): dt = np.dtype('=i8').newbyteorder() x = np.arange(5, dtype=dt) with pytest.raises(TypeError): np._from_dlpack(x)
def test_invalid_dtype(self): x = np.asarray(np.datetime64('2021-05-27')) with pytest.raises(TypeError): np._from_dlpack(x)
def test_dtype_passthrough(self, dtype): x = np.arange(5, dtype=dtype) y = np._from_dlpack(x) assert y.dtype == x.dtype assert_array_equal(x, y)
def test_from_dlpack_refcount(self): x = np.arange(5) y = np._from_dlpack(x) assert sys.getrefcount(x) == 3 del y assert sys.getrefcount(x) == 2
def test_ndim0(self): x = np.array(1.0) y = np._from_dlpack(x) assert_array_equal(x, y)
def from_dlpack(x: object, /) -> Array: from ._array_object import Array return Array._new(np._from_dlpack(x))