Skip to content

Commit c47fff2

Browse files
committed
TST: add a test for __dlpack__ dunder
1 parent 15f2794 commit c47fff2

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

array_api_tests/test_dlpack.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,38 @@ def test_from_dlpack(x, copy_kw, data):
7676
except Exception as exc:
7777
ph.add_note(exc, repro_snippet)
7878
raise
79+
80+
81+
@given(
82+
x=hh.arrays(dtype=hh.all_dtypes, shape=hh.shapes(min_dims=1, max_side=2)),
83+
copy_kw=hh.kwargs(
84+
copy=st.booleans() | st.none()
85+
),
86+
max_version_kw=hh.kwargs(
87+
max_version=st.tuples( # XXX: limits for versions: is (-111, 42) okay?
88+
st.integers(),
89+
st.integers()
90+
)
91+
),
92+
dl_device_kw=hh.kwargs(
93+
dl_device=st.tuples( # XXX: the 2023.12 standard only mandates ... kDLCPU ?
94+
st.just(DLPackDeviceEnum.CPU.value),
95+
st.just(0)
96+
)
97+
),
98+
data=st.data()
99+
)
100+
def test_dunder_dlpack(x, copy_kw, max_version_kw, dl_device_kw, data):
101+
102+
repro_snippet = ph.format_snippet(
103+
f"x.__dlpack__ with {copy_kw = }, {max_version_kw = } and {dl_device_kw = }"
104+
)
105+
106+
try:
107+
capsule = x.__dlpack__(**copy_kw, **max_version_kw, **dl_device_kw)
108+
109+
# XXX: bug or feature? np.from_dlpack(x.__dlpack__(max_version=(0, 1))) -> AttributeError
110+
y = xp.from_dlpack(capsule)
111+
except Exception as exc:
112+
ph.add_note(exc, repro_snippet)
113+
raise

0 commit comments

Comments
 (0)