Skip to content

Commit 6473984

Browse files
committed
Use f-strings instead of format
1 parent 1748f9b commit 6473984

File tree

15 files changed

+87
-114
lines changed

15 files changed

+87
-114
lines changed

dpctl/_sycl_context.pyx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,11 @@ cdef class SyclContext(_SyclContext):
321321
)
322322
elif (ret == -6):
323323
raise TypeError(
324-
"Input capsule {} contains a null pointer or could not be"
325-
" renamed".format(arg)
324+
f"Input capsule {arg} contains a null pointer or could not "
325+
"be renamed"
326326
)
327327
raise ValueError(
328-
"Unrecognized error code ({}) encountered.".format(ret)
328+
f"Unrecognized error code ({ret}) encountered."
329329
)
330330

331331
cdef bool equals(self, SyclContext ctxt):
@@ -471,12 +471,10 @@ cdef class SyclContext(_SyclContext):
471471
"""
472472
cdef size_t n = self.device_count
473473
if n == 1:
474-
return ("<dpctl." + self.__name__ + " at {}>".format(hex(id(self))))
474+
return f"<dpctl.{self.__name__} at {hex(id(self))}>"
475475
else:
476476
return (
477-
"<dpctl."
478-
+ self.__name__
479-
+ " for {} devices at {}>".format(n, hex(id(self)))
477+
f"<dpctl.{self.__name__} for {n} devices at {hex(id(self))}>"
480478
)
481479

482480
def _get_capsule(self):

dpctl/_sycl_device.pyx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ cdef class SyclDevice(_SyclDevice):
410410
if ret == -1:
411411
raise SyclDeviceCreationError(
412412
"Could not create a SyclDevice with the selector string "
413-
"'{selector_string}'".format(selector_string=arg)
413+
f"'{arg}'"
414414
)
415415
elif isinstance(arg, _SyclDevice):
416416
ret = self._init_from__SyclDevice(arg)
@@ -1577,7 +1577,7 @@ cdef class SyclDevice(_SyclDevice):
15771577
+ ", "
15781578
+ " "
15791579
+ self.name
1580-
+ "] at {}>".format(hex(id(self)))
1580+
+ f"] at {hex(id(self))}>"
15811581
)
15821582

15831583
def __hash__(self):
@@ -1651,7 +1651,7 @@ cdef class SyclDevice(_SyclDevice):
16511651
counts_buff = <size_t *> malloc((<size_t> ncounts) * sizeof(size_t))
16521652
if counts_buff is NULL:
16531653
raise MemoryError(
1654-
"Allocation of counts array of size {} failed.".format(ncounts)
1654+
f"Allocation of counts array of size {ncounts} failed."
16551655
)
16561656
for i in range(ncounts):
16571657
counts_buff[i] = counts[i]
@@ -1714,9 +1714,8 @@ cdef class SyclDevice(_SyclDevice):
17141714
try:
17151715
sd = cpu_d.create_sub_devices(partition="numa")
17161716
print(
1717-
"{0} sub-devices were created with respective "
1718-
"#EUs being {1}".format(
1719-
len(sd), [d.max_compute_units for d in sd]
1717+
f"{len(sd)} sub-devices were created with respective "
1718+
f"#EUs being {[d.max_compute_units for d in sd]}"
17201719
)
17211720
)
17221721
except Exception:
@@ -1785,9 +1784,7 @@ cdef class SyclDevice(_SyclDevice):
17851784
)
17861785
else:
17871786
raise ValueError(
1788-
"Partition affinity domain {} is not understood.".format(
1789-
partition
1790-
)
1787+
f"Partition affinity domain {partition} is not understood."
17911788
)
17921789
return self.create_sub_devices_by_affinity(domain_type)
17931790
elif isinstance(partition, collections.abc.Sized) and isinstance(

dpctl/_sycl_event.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,15 @@ cdef class SyclEvent(_SyclEvent):
201201
raise ValueError("Event failed to be created.")
202202
elif (ret == -2):
203203
raise TypeError(
204-
"Input capsule {} contains a null pointer or could not be"
205-
" renamed".format(arg)
204+
f"Input capsule {arg} contains a null pointer or could not "
205+
"be renamed"
206206
)
207207
elif (ret == -3):
208208
raise ValueError(
209209
"Internal Error: Could not create a copy of a sycl event."
210210
)
211211
raise ValueError(
212-
"Unrecognized error code ({}) encountered.".format(ret)
212+
f"Unrecognized error code ({ret}) encountered."
213213
)
214214

215215
cdef DPCTLSyclEventRef get_event_ref(self):

dpctl/_sycl_platform.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ cdef class SyclPlatform(_SyclPlatform):
181181
+ ", "
182182
+ self.vendor
183183
+ ", "
184-
+ self.version + "] at {}>".format(hex(id(self)))
184+
+ self.version + f"] at {hex(id(self))}>"
185185
)
186186

187187
def __cinit__(self, arg=None):

dpctl/_sycl_queue.pyx

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -431,14 +431,12 @@ cdef int _parse_queue_properties(object prop) except *:
431431
res = res | _queue_property_type._DEFAULT_PROPERTY
432432
else:
433433
raise ValueError(
434-
(
435-
"queue property '{}' is not understood, "
436-
"expecting 'in_order', 'enable_profiling', or 'default'"
437-
).format(prop)
434+
f"queue property '{prop}' is not understood, expecting "
435+
"'in_order', 'enable_profiling', or 'default'"
438436
)
439437
else:
440438
raise ValueError(
441-
"queue property '{}' is not understood.".format(prop)
439+
f"queue property '{prop}' is not understood."
442440
)
443441
return res
444442

@@ -648,7 +646,7 @@ cdef class SyclQueue(_SyclQueue):
648646
if len(args) > 2:
649647
raise TypeError(
650648
"SyclQueue constructor takes 0, 1, or 2 positinal arguments, "
651-
"but {} were given.".format(len(args))
649+
f"but {len(args)} were given."
652650
)
653651
props = _parse_queue_properties(
654652
kwargs.pop("property", _queue_property_type._DEFAULT_PROPERTY)
@@ -676,56 +674,55 @@ cdef class SyclQueue(_SyclQueue):
676674
status = self._init_queue_from_capsule(arg)
677675
else:
678676
raise TypeError(
679-
"Positional argument {} is not a filter string or a "
680-
"SyclDevice".format(arg)
677+
f"Positional argument {arg} is not a filter string or a "
678+
"SyclDevice"
681679
)
682680
else:
683681
ctx, dev = args
684682
if not isinstance(ctx, SyclContext):
685683
raise TypeError(
686684
"SyclQueue constructor with two positional arguments "
687-
"expected SyclContext as its first argument, but got {}."
688-
.format(type(ctx))
685+
f"expected SyclContext as its first argument, but got "
686+
f"{type(ctx)}."
689687
)
690688
if not isinstance(dev, SyclDevice):
691689
raise TypeError(
692690
"SyclQueue constructor with two positional arguments "
693-
"expected SyclDevice as its second argument, but got {}."
694-
.format(type(dev))
691+
"expected SyclDevice as its second argument, but got "
692+
f"{type(dev)}."
695693
)
696694
status = self._init_queue_from_context_and_device(
697695
<SyclContext>ctx, <SyclDevice>dev, props
698696
)
699697
if status < 0:
700698
if status == -1:
701699
raise SyclQueueCreationError(
702-
"Device filter selector string '{}' is not understood."
703-
.format(arg)
700+
f"Device filter selector string '{arg}' is not understood."
704701
)
705702
elif status == -2 or status == -8:
706703
default_dev_error = (
707704
"Default SYCL Device could not be created."
708705
)
709706
raise SyclQueueCreationError(
710707
default_dev_error if (len_args == 0) else
711-
"SYCL Device '{}' could not be created.".format(arg)
708+
f"SYCL Device '{arg}' could not be created."
712709
)
713710
elif status == -3 or status == -7:
714711
raise SyclQueueCreationError(
715712
"SYCL Context could not be created " +
716713
"by default constructor" if len_args == 0 else
717-
"from '{}'.".format(arg)
714+
f"from '{arg}'."
718715
)
719716
elif status == -4 or status == -6:
720717
if len_args == 2:
721718
arg = args
722719
raise SyclQueueCreationError(
723-
"SYCL Queue failed to be created from '{}'.".format(arg)
720+
f"SYCL Queue failed to be created from '{arg}'."
724721
)
725722
elif status == -5:
726723
raise TypeError(
727-
"Input capsule {} contains a null pointer or could not "
728-
"be renamed".format(arg)
724+
f"Input capsule {arg} contains a null pointer or could not "
725+
"be renamed"
729726
)
730727

731728
cdef int _init_queue_from__SyclQueue(self, _SyclQueue other):
@@ -1546,12 +1543,10 @@ cdef class SyclQueue(_SyclQueue):
15461543
if en_prof:
15471544
prop.append("enable_profiling")
15481545
return (
1549-
"<dpctl."
1550-
+ self.__name__
1551-
+ " at {}, property={}>".format(hex(id(self)), prop)
1546+
f"<dpctl.{self.__name__} at {hex(id(self))}, property={prop}>"
15521547
)
15531548
else:
1554-
return "<dpctl." + self.__name__ + " at {}>".format(hex(id(self)))
1549+
return f"<dpctl.{self.__name__} at {hex(id(self))}>"
15551550

15561551
def __hash__(self):
15571552
"""

dpctl/memory/_memory.pyx

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ def _to_memory(unsigned char[::1] b, str usm_kind):
146146
res = MemoryUSMHost(len(b))
147147
else:
148148
raise ValueError(
149-
"Unrecognized usm_kind={} stored in the "
150-
"pickle".format(usm_kind)
149+
f"Unrecognized usm_kind={usm_kind} stored in the pickle"
151150
)
152151
res.copy_from_host(b)
153152

@@ -206,9 +205,8 @@ cdef class _Memory:
206205
p = DPCTLmalloc_device(nbytes, QRef)
207206
else:
208207
raise RuntimeError(
209-
"Pointer type '{}' is not recognized".format(
210-
ptr_type.decode("UTF-8")
211-
)
208+
f"Pointer type '{ptr_type.decode('UTF-8')}' is not "
209+
"recognized"
212210
)
213211

214212
if (p):
@@ -250,13 +248,13 @@ cdef class _Memory:
250248
self.refobj = other
251249
else:
252250
raise ValueError(
253-
"Argument {} does not correctly expose"
254-
"`__sycl_usm_array_interface__`.".format(other)
251+
f"Argument {other} does not correctly expose"
252+
"`__sycl_usm_array_interface__`."
255253
)
256254
else:
257255
raise ValueError(
258-
"Argument {} does not expose "
259-
"`__sycl_usm_array_interface__`.".format(other)
256+
f"Argument {other} does not expose "
257+
"`__sycl_usm_array_interface__`."
260258
)
261259

262260
def __dealloc__(self):
@@ -350,12 +348,8 @@ cdef class _Memory:
350348

351349
def __repr__(self):
352350
return (
353-
"<SYCL(TM) USM-{} allocation of {} bytes at {}>"
354-
.format(
355-
self.get_usm_type(),
356-
self.nbytes,
357-
hex(<object>(<size_t>self._memory_ptr))
358-
)
351+
f"<SYCL(TM) USM-{self.get_usm_type()} allocation of {self.nbytes} "
352+
f"bytes at {hex(<object>(<size_t>self._memory_ptr))}>"
359353
)
360354

361355
def __len__(self):
@@ -488,8 +482,8 @@ cdef class _Memory:
488482
host_buf = obj
489483
elif (<Py_ssize_t>len(host_buf) < self.nbytes):
490484
raise ValueError(
491-
"Destination object is too small to accommodate {} bytes"
492-
.format(self.nbytes)
485+
f"Destination object is too small to accommodate {self.nbytes} "
486+
"bytes"
493487
)
494488
# call kernel to copy from
495489
ERef = DPCTLQueue_Memcpy(
@@ -514,8 +508,8 @@ cdef class _Memory:
514508

515509
if (buf_len > self.nbytes):
516510
raise ValueError(
517-
"Source object is too large to be accommodated in {} bytes "
518-
"buffer".format(self.nbytes)
511+
"Source object is too large to be accommodated in "
512+
f"{self.nbytes} bytes buffer"
519513
)
520514
# call kernel to copy from
521515
ERef = DPCTLQueue_Memcpy(
@@ -551,7 +545,7 @@ cdef class _Memory:
551545
if (src_buf.nbytes > self.nbytes):
552546
raise ValueError(
553547
"Source object is too large to "
554-
"be accommondated in {} bytes buffer".format(self.nbytes)
548+
f"be accommondated in {self.nbytes} bytes buffer"
555549
)
556550

557551
src_queue = src_buf.queue
@@ -788,13 +782,13 @@ cdef class MemoryUSMShared(_Memory):
788782
self.copy_from_device(other)
789783
else:
790784
raise ValueError(
791-
"USM pointer in the argument {} is not a "
785+
f"USM pointer in the argument {other} is not a "
792786
"USM shared pointer. "
793787
"Zero-copy operation is not possible with "
794788
"copy=False. "
795789
"Either use copy=True, or use a constructor "
796-
"appropriate for "
797-
"type '{}'".format(other, self.get_usm_type())
790+
f"appropriate for "
791+
f"type '{self.get_usm_type()}'"
798792
)
799793

800794
def __getbuffer__(self, Py_buffer *buffer, int flags):
@@ -840,13 +834,11 @@ cdef class MemoryUSMHost(_Memory):
840834
self.copy_from_device(other)
841835
else:
842836
raise ValueError(
843-
"USM pointer in the argument {} is "
837+
f"USM pointer in the argument {other} is "
844838
"not a USM host pointer. "
845839
"Zero-copy operation is not possible with copy=False. "
846840
"Either use copy=True, or use a constructor "
847-
"appropriate for type '{}'".format(
848-
other, self.get_usm_type()
849-
)
841+
f"appropriate for type '{self.get_usm_type()}'"
850842
)
851843

852844
def __getbuffer__(self, Py_buffer *buffer, int flags):
@@ -892,13 +884,11 @@ cdef class MemoryUSMDevice(_Memory):
892884
self.copy_from_device(other)
893885
else:
894886
raise ValueError(
895-
"USM pointer in the argument {} is not "
887+
f"USM pointer in the argument {other} is not "
896888
"a USM device pointer. "
897889
"Zero-copy operation is not possible with copy=False. "
898890
"Either use copy=True, or use a constructor "
899-
"appropriate for type '{}'".format(
900-
other, self.get_usm_type()
901-
)
891+
f"appropriate for type '{self.get_usm_type()}'"
902892
)
903893

904894

dpctl/memory/_sycl_usm_array_interface_utils.pxi

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,8 @@ cdef class _USMBufferData:
165165

166166
if ary_version != 1:
167167
raise ValueError(("__sycl_usm_array_interface__ is malformed:"
168-
" dict('version': {}) is unexpected."
169-
" The only recognized version is 1.").format(
170-
ary_version))
168+
f" dict('version': {ary_version}) is unexpected."
169+
" The only recognized version is 1."))
171170
if not ary_data_tuple or len(ary_data_tuple) != 2:
172171
raise ValueError("__sycl_usm_array_interface__ is malformed:"
173172
" 'data' field is required, and must be a tuple"
@@ -181,10 +180,9 @@ cdef class _USMBufferData:
181180
if (QRef is NULL):
182181
raise ValueError("__sycl_usm_array_interface__ is malformed:"
183182
" 'data' field is not consistent with 'syclobj'"
184-
" field, the pointer {} is not bound to"
185-
" SyclContext derived from"
186-
" dict('syclobj': {}).".format(
187-
hex(arr_data_ptr), ary_syclobj))
183+
f" field, the pointer {hex(arr_data_ptr)} is not "
184+
"bound to SyclContext derived from "
185+
f"dict('syclobj': {ary_syclobj}).")
188186
# shape must be present
189187
if ary_shape is None or not (
190188
isinstance(ary_shape, collections.abc.Sized) and
@@ -204,7 +202,7 @@ cdef class _USMBufferData:
204202
except TypeError as e:
205203
raise ValueError(
206204
"__sycl_usm_array_interface__ is malformed:"
207-
" dict('typestr': {}) is unexpected. ".format(ary_typestr)
205+
f" dict('typestr': {ary_typestr}) is unexpected."
208206
) from e
209207

210208
if (ary_strides is None or (

dpctl/tests/test_sycl_device.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ def test_filter_string(valid_filter):
8383
dev_id = device.filter_string
8484
assert (
8585
dpctl.SyclDevice(dev_id) == device
86-
), "Reconstructed device is different, ({}, {})".format(
87-
valid_filter, dev_id
88-
)
86+
), f"Reconstructed device is different, ({valid_filter}, {dev_id})"
8987

9088

9189
def test_filter_string_property():

0 commit comments

Comments
 (0)