Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix byte behavior #693

Merged
merged 4 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"bool": ["bool", "boolean"],
"int": [
"int8",
"byte",
"octet",
"uint8",
"char",
"int16",
Expand All @@ -74,7 +74,7 @@
ros_primitive_types = [
"bool",
"boolean",
"byte",
"octet",
"char",
"int8",
"uint8",
Expand Down Expand Up @@ -223,6 +223,11 @@ def _from_inst(inst, rostype):
if (not bson_only_mode) and (rostype in type_map.get("float")):
if math.isnan(inst) or math.isinf(inst):
return None

# JSON does not support byte array. They are converted to int
if (not bson_only_mode) and (rostype == "octet"):
return int.from_bytes(inst, "little")

return inst

# Check if it's a list or tuple
Expand Down Expand Up @@ -326,6 +331,10 @@ def _to_primitive_inst(msg, rostype, roottype, stack):
# fix that by casting the int to the expected float
msg = float(msg)

# Convert to byte
if rostype == "octet" and isinstance(msg, int):
return bytes([msg])

msgtype = type(msg)
if msgtype in primitive_types and rostype in type_map[msgtype.__name__]:
return msg
Expand Down
2 changes: 0 additions & 2 deletions rosbridge_library/test/internal/test_message_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ def test_int_primitives(self):
self.assertEqual(c._to_primitive_inst(msg, rostype, rostype, []), msg)
self.assertEqual(c._to_inst(msg, rostype, rostype), msg)

@unittest.skip
def test_byte_primitives(self):
# Test raw primitives
for msg in range(0, 200):
Expand Down Expand Up @@ -172,7 +171,6 @@ def test_unsigned_int_base_msgs(self):
self.assertRaises(Exception, self.do_primitive_test, int64, "std_msgs/UInt16")
self.assertRaises(Exception, self.do_primitive_test, int64, "std_msgs/UInt32")

@unittest.skip
def test_byte_base_msg(self):
int8s = range(0, 256)
for int8 in int8s:
Expand Down