Skip to content

Commit 84346b8

Browse files
Return a list of strings regardless of input
1 parent 01308e0 commit 84346b8

File tree

2 files changed

+20
-47
lines changed

2 files changed

+20
-47
lines changed

softioc/device.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,10 @@ def _value_to_epics(self, value):
430430
return numpy.copy(value)
431431

432432
def _epics_to_value(self, value):
433-
return value
433+
if self._dtype.char == 'S':
434+
return [_string_at(s, 40) for s in value]
435+
else:
436+
return value
434437

435438
def _value_to_dbr(self, value):
436439
return self._dbf_type_, len(value), value.ctypes.data, value

tests/test_record_values.py

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -196,65 +196,43 @@ def record_values_names(fixture_value):
196196
"wIn_byte_string_array",
197197
builder.WaveformIn,
198198
[b"AB123", b"CD456", b"EF789"],
199-
numpy.array(
200-
["AB123", "CD456", "EF789"], dtype=NUMPY_DTYPE_STRING
201-
),
202-
numpy.ndarray,
199+
["AB123", "CD456", "EF789"],
200+
list,
203201
),
204202
(
205203
"wOut_byte_string_array",
206204
builder.WaveformOut,
207205
[b"12AB", b"34CD", b"56EF"],
208-
numpy.array(
209-
["12AB", "34CD", "56EF"], dtype=NUMPY_DTYPE_STRING
210-
),
211-
numpy.ndarray,
206+
["12AB", "34CD", "56EF"],
207+
list,
212208
),
213209
(
214210
"wIn_unicode_string_array",
215211
builder.WaveformIn,
216212
["12€½", "34¾²", "56¹³"],
217-
numpy.array(
218-
[
219-
b'12\xe2\x82\xac\xc2\xbd',
220-
b'34\xc2\xbe\xc2\xb2',
221-
b'56\xc2\xb9\xc2\xb3'
222-
],
223-
dtype=NUMPY_DTYPE_STRING
224-
),
225-
numpy.ndarray,
213+
["12€½", "34¾²", "56¹³"],
214+
list,
226215
),
227216
(
228217
"wOut_unicode_string_array",
229218
builder.WaveformOut,
230219
["12€½", "34¾²", "56¹³"],
231-
numpy.array(
232-
[
233-
b'12\xe2\x82\xac\xc2\xbd',
234-
b'34\xc2\xbe\xc2\xb2',
235-
b'56\xc2\xb9\xc2\xb3'
236-
],
237-
dtype=NUMPY_DTYPE_STRING
238-
),
239-
numpy.ndarray,
220+
["12€½", "34¾²", "56¹³"],
221+
list,
240222
),
241223
(
242224
"wIn_string_array",
243225
builder.WaveformIn,
244226
["123abc", "456def", "7890ghi"],
245-
numpy.array(
246-
["123abc", "456def", "7890ghi"], dtype=NUMPY_DTYPE_STRING
247-
),
248-
numpy.ndarray,
227+
["123abc", "456def", "7890ghi"],
228+
list,
249229
),
250230
(
251231
"wOut_string_array",
252232
builder.WaveformOut,
253233
["123abc", "456def", "7890ghi"],
254-
numpy.array(
255-
["123abc", "456def", "7890ghi"], dtype=NUMPY_DTYPE_STRING
256-
),
257-
numpy.ndarray,
234+
["123abc", "456def", "7890ghi"],
235+
list,
258236
),
259237
(
260238
"longStringIn_str",
@@ -553,7 +531,7 @@ def is_valid(configuration):
553531

554532
if (
555533
creation_func in [builder.WaveformOut, builder.WaveformIn]
556-
and expected_value.dtype
534+
and hasattr(expected_value, 'dtype')
557535
and expected_value.dtype in [numpy.float64, numpy.int32]
558536
):
559537
log(
@@ -562,19 +540,11 @@ def is_valid(configuration):
562540
"scalar. Therefore we skip this check.")
563541
continue
564542

565-
# caget on a waveform of strings will return unicode. Have to
566-
# convert it manually to binary.
543+
# caget on a waveform of strings will return a numpy array.
544+
# Must extract it out to a list to match .get()
567545
if isinstance(rec_val, numpy.ndarray) and len(rec_val) > 1 \
568546
and rec_val.dtype.char in ["S", "U"]:
569-
result = numpy.empty(len(rec_val), NUMPY_DTYPE_STRING)
570-
for n, s in enumerate(rec_val):
571-
if isinstance(s, str):
572-
result[n] = s.encode('UTF-8', errors= 'ignore')
573-
else:
574-
result[n] = s
575-
rec_val = result
576-
# caget won't retrieve the full length 40 buffer
577-
rec_val = rec_val.astype(NUMPY_DTYPE_STRING)
547+
rec_val = [s for s in rec_val]
578548

579549
record_value_asserts(
580550
creation_func, rec_val, expected_value, expected_type

0 commit comments

Comments
 (0)