Skip to content

Commit

Permalink
Allow integers in conversion to float array messages (#777)
Browse files Browse the repository at this point in the history
Fixes #764 by using `ndarray`/`array` assignment operations that accept ints as well as floats, rather than directly assigning the incoming list value, which failed at the msg/idl generated python code's validation step.
  • Loading branch information
jtbandes authored Aug 16, 2022
1 parent 71fd72b commit 4de99bc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,13 @@ def _to_list_inst(msg, rostype, roottype, inst, stack):
return []

# Special mappings for numeric types https://design.ros2.org/articles/idl_interface_definition.html
if isinstance(inst, array.array) or isinstance(inst, np.ndarray):
return msg
if isinstance(inst, array.array):
del inst[:]
inst.extend(msg) # accepts both ints and floats which may come from json
return inst
if isinstance(inst, np.ndarray):
inst[:] = msg # accepts both ints and floats which may come from json
return inst

# Remove the list indicators from the rostype
try:
Expand Down
10 changes: 10 additions & 0 deletions rosbridge_library/test/internal/test_message_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,20 @@ def test_float32_msg(rostype, data):
ret = test_float32_msg(rostype, floats)
np.testing.assert_array_equal(ret, np.array(floats))

# From List[int]
ints = list(map(int, range(0, 256)))
ret = test_float32_msg(rostype, ints)
np.testing.assert_array_equal(ret, np.array(ints))

for msgtype in ["TestFloat32BoundedArray"]:
rostype = "rosbridge_test_msgs/" + msgtype

# From List[float]
floats = list(map(float, range(0, 16)))
ret = test_float32_msg(rostype, floats)
np.testing.assert_array_equal(ret, np.array(floats))

# From List[int]
ints = list(map(int, range(0, 16)))
ret = test_float32_msg(rostype, ints)
np.testing.assert_array_equal(ret, np.array(ints))

0 comments on commit 4de99bc

Please sign in to comment.