Skip to content

Commit 8c4b516

Browse files
Ensure returned arrays are not writeable
1 parent d38df9b commit 8c4b516

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Removed:
2323
Changed:
2424

2525
- `AsyncioDispatcher cleanup tasks atexit <../../pull/138>`_
26+
- `Ensure returned numpy arrays are not writeable <../../pull/164>`_
2627

2728
Fixed:
2829

softioc/device.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,12 @@ def _value_to_epics(self, value):
467467
# common class of bug, at the cost of duplicated code and data, here we
468468
# ensure a copy is taken of the value.
469469
assert len(value) <= self._nelm, 'Value too long for waveform'
470-
return numpy.copy(value)
470+
471+
value = numpy.copy(value)
472+
# As we return a reference to the numpy array, ensure it cannot be
473+
# modified under our noses
474+
value.flags.writeable = False
475+
return value
471476

472477
def _epics_to_value(self, value):
473478
if self._dtype.char == 'S':

tests/test_records.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,19 @@ def test_record_wrapper_str():
340340
# If we never receive R it probably means an assert failed
341341
select_and_recv(parent_conn, "R")
342342

343+
def test_waveform_values_not_modifiable():
344+
"""Test that arrays returned from waveform records are not modifiable"""
345+
346+
wi = builder.WaveformIn("WI", [1, 2, 3])
347+
wo = builder.WaveformOut("WO", [1, 2, 3])
348+
349+
with pytest.raises(ValueError):
350+
wi.get()[0] = 5
351+
352+
with pytest.raises(ValueError):
353+
wo.get()[0] = 5
354+
355+
343356
def validate_fixture_names(params):
344357
"""Provide nice names for the out_records fixture in TestValidate class"""
345358
return params[0].__name__

0 commit comments

Comments
 (0)