Allow register.Value.assign() to automatically extend/truncate arrays to avoid issues like https://forum.uavcan.org/t/control-and-debug-kotleta20-esc-with-yakut/1176/3\?u\=pavel.kirienko (#162)
diff --git a/pyuavcan/VERSION b/pyuavcan/VERSION
index 6085e94..23aa839 100644
--- a/pyuavcan/VERSION
+++ b/pyuavcan/VERSION
@@ -1 +1 @@
-1.2.1
+1.2.2
diff --git a/pyuavcan/application/register/_value.py b/pyuavcan/application/register/_value.py
index d2c63e2..6112b57 100644
--- a/pyuavcan/application/register/_value.py
+++ b/pyuavcan/application/register/_value.py
@@ -285,14 +285,18 @@
if to.unstructured and s.string:
return Value(unstructured=Unstructured(s.string.value))
- opt_to, opt_s = _get_option_name(to), _get_option_name(s)
- val_to: numpy.ndarray = get_attribute(to, opt_to).value
- val_s: numpy.ndarray = get_attribute(s, opt_s).value
- if len(val_to) != len(val_s):
- return None # Dimensionality mismatch.
+ if s.string or s.unstructured:
+ return None
+
+ val_s: numpy.ndarray = get_attribute(
+ s,
+ _get_option_name(s),
+ ).value.copy()
+ val_s.resize(
+ get_attribute(to, _get_option_name(to)).value.size,
+ refcheck=False,
+ )
# At this point it is known that both values are of the same dimension.
- if opt_to == opt_s: # Also same scalar type -- no further checks needed.
- return s
# fmt: off
if to.bit: return Value(bit=Bit([x != 0 for x in val_s]))
if to.real16: return Value(real16=Real16(val_s))
@@ -395,10 +399,6 @@
assert _once(q(string=String("A")), Unstructured(b"B")).string.value.tobytes().decode() == "B"
assert list(_once(q(natural16=Natural16([1, 2])), Natural64([1, 2])).natural16.value) == [1, 2]
- # Dimensionality mismatch.
- with pytest.raises(ValueConversionError):
- _once(q(integer16=Integer16([1, 2, 3])), Integer16([1, 2]))
-
assert list(_once(q(bit=Bit([False, False])), Integer32([-1, 0])).bit.value) == [True, False]
assert list(_once(q(integer8=Integer8([0, 1])), Real64([3.3, 6.4])).integer8.value) == [3, 6]
assert list(_once(q(integer16=Integer16([0, 1])), Real32([3.3, 6.4])).integer16.value) == [3, 6]