Skip to content

Commit

Permalink
Ensure returned arrays are not writeable
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderWells-diamond committed Jul 12, 2024
1 parent d38df9b commit 8c4b516
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Removed:
Changed:

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

Fixed:

Expand Down
7 changes: 6 additions & 1 deletion softioc/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,12 @@ def _value_to_epics(self, value):
# common class of bug, at the cost of duplicated code and data, here we
# ensure a copy is taken of the value.
assert len(value) <= self._nelm, 'Value too long for waveform'
return numpy.copy(value)

value = numpy.copy(value)
# As we return a reference to the numpy array, ensure it cannot be
# modified under our noses
value.flags.writeable = False
return value

def _epics_to_value(self, value):
if self._dtype.char == 'S':
Expand Down
13 changes: 13 additions & 0 deletions tests/test_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,19 @@ def test_record_wrapper_str():
# If we never receive R it probably means an assert failed
select_and_recv(parent_conn, "R")

def test_waveform_values_not_modifiable():
"""Test that arrays returned from waveform records are not modifiable"""

wi = builder.WaveformIn("WI", [1, 2, 3])
wo = builder.WaveformOut("WO", [1, 2, 3])

with pytest.raises(ValueError):
wi.get()[0] = 5

with pytest.raises(ValueError):
wo.get()[0] = 5


def validate_fixture_names(params):
"""Provide nice names for the out_records fixture in TestValidate class"""
return params[0].__name__
Expand Down

0 comments on commit 8c4b516

Please sign in to comment.