Skip to content

Commit 783ded9

Browse files
Merge pull request #577 from IntelPython/check-invalid-capsule
Added tests for constructors with invalid capsules
2 parents fdb3919 + 9597299 commit 783ded9

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-5
lines changed

dpctl/_sycl_event.pyx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ cdef class _SyclEvent:
8888
"""
8989

9090
def __dealloc__(self):
91-
DPCTLEvent_Wait(self._event_ref)
92-
DPCTLEvent_Delete(self._event_ref)
91+
if (self._event_ref):
92+
DPCTLEvent_Wait(self._event_ref)
93+
DPCTLEvent_Delete(self._event_ref)
94+
self._event_ref = NULL
9395
self.args = None
9496

9597

dpctl/tests/_helper.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,15 @@ def has_cpu(backend="opencl"):
1111

1212
def has_sycl_platforms():
1313
return bool(len(dpctl.get_platforms()))
14+
15+
16+
def create_invalid_capsule():
17+
"""Creates an invalid capsule for the purpose of testing dpctl
18+
constructors that accept capsules.
19+
"""
20+
import ctypes
21+
22+
ctor = ctypes.pythonapi.PyCapsule_New
23+
ctor.restype = ctypes.py_object
24+
ctor.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_void_p]
25+
return ctor(id(ctor), b"invalid", 0)

dpctl/tests/test_sycl_context.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
""" Defines unit test cases for the SyclContxt class.
17+
""" Defines unit test cases for the :class:`dpctl.SyclContext` class.
1818
"""
1919

2020
import pytest
2121

2222
import dpctl
2323

24+
from ._helper import create_invalid_capsule
25+
2426
list_of_valid_filter_selectors = [
2527
"opencl",
2628
"opencl:gpu",
@@ -210,3 +212,16 @@ def test_cpython_api():
210212
r2 = ctx.addressof_ref()
211213
r1 = get_context_ref_fn(ctx)
212214
assert r1 == r2
215+
216+
217+
def test_invalid_capsule():
218+
cap = create_invalid_capsule()
219+
with pytest.raises(ValueError):
220+
dpctl.SyclContext(cap)
221+
222+
223+
def test_multi_device_different_platforms():
224+
devs = dpctl.get_devices() # all devices
225+
if len(devs) > 1:
226+
with pytest.raises(ValueError):
227+
dpctl.SyclContext(devs)

dpctl/tests/test_sycl_event.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import dpctl.program as dpctl_prog
2626
from dpctl import event_status_type as esty
2727

28-
from ._helper import has_cpu
28+
from ._helper import create_invalid_capsule, has_cpu
2929

3030

3131
def produce_event(profiling=False):
@@ -223,6 +223,12 @@ def test_event_capsule():
223223
del cap2
224224

225225

226+
def test_event_invalid_capsule():
227+
cap = create_invalid_capsule()
228+
with pytest.raises(TypeError):
229+
dpctl.SyclEvent(cap)
230+
231+
226232
def test_addressof_ref():
227233
ev = dpctl.SyclEvent()
228234
ref = ev.addressof_ref()

dpctl/tests/test_sycl_queue.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import dpctl
2323

24+
from ._helper import create_invalid_capsule
25+
2426
list_of_standard_selectors = [
2527
dpctl.select_accelerator_device,
2628
dpctl.select_cpu_device,
@@ -359,6 +361,8 @@ def test_context_not_equals():
359361
ctx_cpu = cpuQ.get_sycl_context()
360362
assert ctx_cpu != ctx_gpu
361363
assert hash(ctx_cpu) != hash(ctx_gpu)
364+
assert gpuQ != cpuQ
365+
assert hash(cpuQ) != hash(gpuQ)
362366

363367

364368
def test_context_equals():
@@ -497,11 +501,34 @@ def test_constructor_many_arg():
497501
dpctl.SyclQueue(ctx)
498502

499503

504+
def test_constructor_inconsistent_ctx_dev():
505+
try:
506+
q = dpctl.SyclQueue("cpu")
507+
except dpctl.SyclQueueCreationError:
508+
pytest.skip("Failed to create CPU queue")
509+
cpuD = q.sycl_device
510+
n_eu = cpuD.max_compute_units
511+
n_half = n_eu // 2
512+
try:
513+
d0, d1 = cpuD.create_sub_devices(partition=[n_half, n_eu - n_half])
514+
except Exception:
515+
pytest.skip("Could not create CPU sub-devices")
516+
ctx = dpctl.SyclContext(d0)
517+
with pytest.raises(dpctl.SyclQueueCreationError):
518+
dpctl.SyclQueue(ctx, d1)
519+
520+
521+
def test_constructor_invalid_capsule():
522+
cap = create_invalid_capsule()
523+
with pytest.raises(TypeError):
524+
dpctl.SyclQueue(cap)
525+
526+
500527
def test_queue_wait():
501528
try:
502529
q = dpctl.SyclQueue()
503530
except dpctl.SyclQueueCreationError:
504-
pytest.skip("Failed to create device with supported filter")
531+
pytest.skip("Failed to create default queue")
505532
q.wait()
506533

507534

0 commit comments

Comments
 (0)