Skip to content
Snippets Groups Projects
Commit e8afb0c2 authored by Jan David Mol's avatar Jan David Mol
Browse files

L2SS-909: Do not allow writes of smaller arrays

parent ac25d4e9
No related branches found
No related tags found
1 merge request!399L2SS-909: Do not allow writes of smaller arrays
from operator import mul from operator import mul
from functools import reduce from functools import reduce
import numpy
from tango.server import attribute from tango.server import attribute
from tango import AttrWriteType, AttReqType from tango import AttrWriteType, AttReqType
...@@ -40,6 +41,21 @@ class attribute_io(object): ...@@ -40,6 +41,21 @@ class attribute_io(object):
def cached_write_function(self, value): def cached_write_function(self, value):
""" Writes the given value to the device, and updates the cache. """ """ Writes the given value to the device, and updates the cache. """
# flexible array sizes are not supported by all clients. make sure we only write arrays of maximum size.
if self.attribute_wrapper.write_max_shape and self.attribute_wrapper.shape != ():
if isinstance(value, numpy.ndarray):
value_shape = value.shape
else:
if len(value) > 0 and isinstance(value[0], list):
# nested list
value_shape = (len(value), len(value[0]))
else:
# straight list
value_shape = (len(value),)
if value_shape != self.attribute_wrapper.shape:
raise ValueError(f"Tried writing an array of shape {value_shape} into an attribute of shape {self.attribute_wrapper.shape}")
self.write_function(value) self.write_function(value)
self.cached_value = value self.cached_value = value
...@@ -77,20 +93,30 @@ class attribute_wrapper(attribute): ...@@ -77,20 +93,30 @@ class attribute_wrapper(attribute):
max_dim_x = 1 max_dim_x = 1
max_dim_y = 0 max_dim_y = 0
dtype = datatype dtype = datatype
shape = ()
elif len(dims) == 1: elif len(dims) == 1:
max_dim_x = dims[0] max_dim_x = dims[0]
max_dim_y = 0 max_dim_y = 0
dtype = (datatype,) dtype = (datatype,)
shape = (max_dim_x,)
elif len(dims) == 2: elif len(dims) == 2:
max_dim_x = dims[1] max_dim_x = dims[1]
max_dim_y = dims[0] max_dim_y = dims[0]
dtype = ((datatype,),) dtype = ((datatype,),)
shape = (max_dim_y, max_dim_x)
else: else:
# >2D arrays collapse into the X and Y dimensions. The Y (major) dimension mirrors the first dimension given, the # >2D arrays collapse into the X and Y dimensions. The Y (major) dimension mirrors the first dimension given, the
# rest collapses into the X (minor) dimension. # rest collapses into the X (minor) dimension.
max_dim_x = reduce(mul, dims[1:]) max_dim_x = reduce(mul, dims[1:])
max_dim_y = dims[0] max_dim_y = dims[0]
dtype = ((datatype,),) dtype = ((datatype,),)
shape = (max_dim_y, max_dim_x)
# actual shape of the data as it is read/written
self.shape = shape
# force maximum shape to be written (do not accept writes with shapes smaller than the maximum for each dimension)
self.write_max_shape = True
if access == AttrWriteType.READ_WRITE: if access == AttrWriteType.READ_WRITE:
""" If the attribute is of READ_WRITE type, assign the write and read functions to it""" """ If the attribute is of READ_WRITE type, assign the write and read functions to it"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment