Skip to content

Commit

Permalink
Adding a slot_type message field to python msg classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mlautman authored and Mike Lautman committed Nov 13, 2018
1 parent 189b16a commit 9454002
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
24 changes: 23 additions & 1 deletion rosidl_generator_py/resource/_msg.py.em
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,31 @@ class @(spec.base_type.type)(metaclass=Metaclass):
'_@(field.name)',
@[end for]@
]

_slot_types = [
@[for field in spec.fields]@
@[ if field.type.is_primitive_type() ]@
'@(field.type)',
@[ else ]@
@[ if field.type.is_array ]@
@[ if field.type.array_size ]@
@[ if field.type.is_upper_bound ]@
'@(field.type.pkg_name)/@(field.type.type)[<=@(field.type.array_size)]',
@[ else ]@
'@(field.type.pkg_name)/@(field.type.type)[@(field.type.array_size)]',
@[ end if ]@
@[ else ]@
'@(field.type.pkg_name)/@(field.type.type)[]',
@[ end if ]@
@[ else ]@
'@(field.type.pkg_name)/@(field.type.type)',
@[ end if ]@
@[ end if ]@
@[end for]@
]

@
@[if len(spec.fields) > 0]@

def __init__(self, **kwargs):
assert all('_' + key in self.__slots__ for key in kwargs.keys()), \
'Invalid arguments passed to constructor: %s' % \
Expand Down
42 changes: 42 additions & 0 deletions rosidl_generator_py/test/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,45 @@ def test_out_of_range():
setattr(b, 'unbounded_uint64_values', [2**64])
with pytest.raises(AssertionError):
setattr(b, 'unbounded_uint64_values', [-1])


def test_slot_attributes():
a = Nested()
assert hasattr(a, '_slot_types')
assert hasattr(a, '__slots__')
nested_slot_types = getattr(a, '_slot_types')
nested_slots = getattr(a, '__slots__')
assert len(nested_slot_types) == len(nested_slots)
expected_nested_slot_types = [
'rosidl_generator_py/Primitives',
'rosidl_generator_py/Primitives[2]',
'rosidl_generator_py/Primitives[<=3]',
'rosidl_generator_py/Primitives[]',
]
assert len(nested_slot_types) == len(expected_nested_slot_types)

for i, expected_nested_slot_type in enumerate(expected_nested_slot_types):
assert nested_slot_types[i] == expected_nested_slot_type

b = StringArrays()
assert hasattr(b, '_slot_types')
assert hasattr(b, '__slots__')
string_slot_types = getattr(b, '_slot_types')
string_slots = getattr(b, '__slots__')
assert len(string_slot_types) == len(string_slots)
expected_string_slot_types = [
'string<=5[3]',
'string<=5[<=10]',
'string<=5[]',
'string[]',
'string[3]',
'string[<=10]',
'string[]',
'string[3]',
'string[<=10]',
'string[]',
'string[]',
]
assert len(string_slot_types) == len(expected_string_slot_types)
for i, slot_type in enumerate(expected_string_slot_types):
assert string_slot_types[i] == slot_type

0 comments on commit 9454002

Please sign in to comment.