Skip to content

Commit 8e555c2

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

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

‎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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,9 @@ def _epics_to_value(self, value):
473473
if self._dtype.char == 'S':
474474
return [_string_at(s, 40) for s in value]
475475
else:
476+
# As we return a reference to the numpy array, ensure it cannot be
477+
# modified under our noses
478+
value.flags.writeable = False
476479
return value
477480

478481
def _value_to_dbr(self, value):

‎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)