Skip to content

Commit 36e1eec

Browse files
Added SyclContextCreationError
Used it in SyclContext constructor. Modified tests.
1 parent 45ee558 commit 36e1eec

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

dpctl/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"""
3333
__author__ = "Intel Corp."
3434

35-
from dpctl._sycl_context import SyclContext
35+
from dpctl._sycl_context import SyclContext, SyclContextCreationError
3636
from dpctl._sycl_device import (
3737
SyclDevice,
3838
SyclDeviceCreationError,
@@ -77,6 +77,7 @@
7777

7878
__all__ = [
7979
"SyclContext",
80+
"SyclContextCreationError",
8081
]
8182
__all__ += [
8283
"SyclDevice",

dpctl/_sycl_context.pyx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,25 @@ from ._backend cimport ( # noqa: E211
4949
)
5050
from ._sycl_device cimport SyclDevice
5151
from ._sycl_queue cimport default_async_error_handler
52+
from ._sycl_device import SyclDeviceCreationError
5253

5354
__all__ = [
5455
"SyclContext",
56+
"SyclContextCreationError",
5557
]
5658

5759
_logger = logging.getLogger(__name__)
5860

61+
62+
cdef class SyclContextCreationError(Exception):
63+
"""
64+
A SyclContextCreationError exception is raised when
65+
SyclContext could not created.
66+
67+
"""
68+
pass
69+
70+
5971
cdef void _context_capsule_deleter(object o):
6072
cdef DPCTLSyclContextRef CRef = NULL
6173
if pycapsule.PyCapsule_IsValid(o, "SyclContextRef"):
@@ -283,11 +295,17 @@ cdef class SyclContext(_SyclContext):
283295
):
284296
ret = self._init_context_from_devices(arg, 0)
285297
else:
286-
dev = SyclDevice(arg)
298+
try:
299+
dev = SyclDevice(arg)
300+
except SyclDeviceCreationError as e:
301+
raise SyclContextCreationError(
302+
"SyclContext failed to be created because "
303+
f"SyclDevice could not be created from the argument {arg}"
304+
) from e
287305
ret = self._init_context_from_one_device(<SyclDevice> dev, 0)
288306
if (ret < 0):
289307
if (ret == -1):
290-
raise ValueError("Context failed to be created.")
308+
raise SyclContextCreationError("Context failed to be created.")
291309
elif (ret == -2):
292310
raise TypeError(
293311
"List of devices to create context from must be non-empty."

dpctl/_sycl_device.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,16 @@ __all__ = [
101101

102102
cdef class SyclDeviceCreationError(Exception):
103103
"""
104-
A DeviceCreationError exception is raised when
105-
device could not created.
104+
A SyclDeviceCreationError exception is raised when
105+
SyclDevice instance could not created.
106106
107107
"""
108108
pass
109109

110110

111111
cdef class SyclSubDeviceCreationError(Exception):
112112
"""
113-
A SubDeviceCreationError exception is raised when
113+
A SyclSubDeviceCreationError exception is raised when
114114
sub-devices were not created.
115115
116116
"""

dpctl/tests/test_sycl_context.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_ctxt_creation_from_filter(valid_filter):
5050
"""
5151
try:
5252
dpctl.SyclContext(valid_filter)
53-
except ValueError:
53+
except dpctl.SyclContextCreationError:
5454
pytest.skip("Failed to create context with supported filter")
5555

5656

@@ -70,11 +70,11 @@ def test_context_not_equals():
7070
"""
7171
try:
7272
ctx_gpu = dpctl.SyclContext("gpu")
73-
except ValueError:
73+
except dpctl.SyclContextCreationError:
7474
pytest.skip()
7575
try:
7676
ctx_cpu = dpctl.SyclContext("cpu")
77-
except ValueError:
77+
except dpctl.SyclContextCreationError:
7878
pytest.skip()
7979
assert ctx_cpu != ctx_gpu
8080
assert hash(ctx_cpu) != hash(ctx_gpu)
@@ -93,7 +93,7 @@ def test_context_equals():
9393
try:
9494
ctx1 = dpctl.SyclContext("gpu")
9595
ctx0 = dpctl.SyclContext("gpu")
96-
except ValueError:
96+
except dpctl.SyclContextCreationError:
9797
pytest.skip()
9898
assert ctx0 == ctx1
9999
assert hash(ctx0) == hash(ctx1)
@@ -118,7 +118,7 @@ def test_repr():
118118
def test_context_can_be_used_in_queue(valid_filter):
119119
try:
120120
ctx = dpctl.SyclContext(valid_filter)
121-
except ValueError:
121+
except dpctl.SyclContextCreationError:
122122
pytest.skip()
123123
devs = ctx.get_devices()
124124
assert len(devs) == ctx.device_count
@@ -129,7 +129,7 @@ def test_context_can_be_used_in_queue(valid_filter):
129129
def test_context_can_be_used_in_queue2(valid_filter):
130130
try:
131131
d = dpctl.SyclDevice(valid_filter)
132-
except ValueError:
132+
except dpctl.SyclContextCreationError:
133133
pytest.skip()
134134
if d.default_selector_score < 0:
135135
# skip test for devices rejected by default selector
@@ -141,7 +141,7 @@ def test_context_can_be_used_in_queue2(valid_filter):
141141
def test_context_multi_device():
142142
try:
143143
d = dpctl.SyclDevice("cpu")
144-
except ValueError:
144+
except dpctl.SyclContextCreationError:
145145
pytest.skip()
146146
if d.default_selector_score < 0:
147147
pytest.skip()
@@ -244,5 +244,5 @@ def test_invalid_capsule():
244244
def test_multi_device_different_platforms():
245245
devs = dpctl.get_devices() # all devices
246246
if len(devs) > 1:
247-
with pytest.raises(ValueError):
247+
with pytest.raises(dpctl.SyclContextCreationError):
248248
dpctl.SyclContext(devs)

0 commit comments

Comments
 (0)