Skip to content

Commit

Permalink
Allow register.Value.assign() to automatically extend/truncate arrays…
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-kirienko authored Apr 1, 2021
1 parent bdc9db2 commit b87efdf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pyuavcan/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.1
1.2.2
22 changes: 11 additions & 11 deletions pyuavcan/application/register/_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,18 @@ def _do_convert(to: Value, s: Value) -> Optional[Value]:
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))
Expand Down Expand Up @@ -395,10 +399,6 @@ def _once(a: Value, b: RelaxedValue) -> Value:
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]
Expand Down

0 comments on commit b87efdf

Please sign in to comment.