From e95e8a05fc4910fea50ead2790b912cdd4c7edbb Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 20 Aug 2026 11:28:41 +0200 Subject: [PATCH] Tolerate spec-mandated BufferError in test_dunder_dlpack test_dunder_dlpack pins the requested dl_device to kDLCPU while drawing copy from {True, False, None}. For a library whose array is not already on kDLCPU (e.g. a SYCL/GPU device reported as kDLOneAPI), the copy=False case forces a cross-device transfer. The __dlpack__ specification requires that a no-copy cross-device transfer be refused with a BufferError, so the test was failing on spec-compliant behavior. Compare the requested dl_device against x.__dlpack_device__() and accept a BufferError when copy is False and the devices differ; all other exceptions (and the copy=None/True paths, which must copy and succeed) are still treated as failures. Fixes #456 --- array_api_tests/test_dlpack.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/array_api_tests/test_dlpack.py b/array_api_tests/test_dlpack.py index 7fec19f1..c3856eea 100644 --- a/array_api_tests/test_dlpack.py +++ b/array_api_tests/test_dlpack.py @@ -59,6 +59,17 @@ def test_dunder_dlpack(x, copy_kw, max_version_kw, dl_device_kw, data): try: x.__dlpack__(**copy_kw, **max_version_kw, **dl_device_kw) # apparently, we cannot do anything with the DLPack capsule from python + except BufferError as exc: + copy = copy_kw["copy"] if copy_kw else None + dl_device = dl_device_kw["dl_device"] if dl_device_kw else None + + cross_device = dl_device is not None and dl_device != x.__dlpack_device__() + if copy is False and cross_device: + # a no-copy cross-device transfer must be refused with a BufferError + return + + ph.add_note(exc, repro_snippet) + raise except Exception as exc: ph.add_note(exc, repro_snippet) raise