Skip to content

Commit 005747b

Browse files
authored
Handle extra IDL slots when doing array introspection (#1031)
* ignore idl generated slots * add test for idl slots * linting
1 parent c489ed5 commit 005747b

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

rosapi/src/rosapi/objectutils.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,18 @@ def _handle_array_information(instance):
201201
fieldtypes = []
202202
fieldarraylen = []
203203
examples = []
204-
for i in range(len(instance.__slots__)):
205-
name = instance.__slots__[i]
206-
fieldnames.append(name)
204+
for slot in instance.__slots__:
205+
key = slot[1:] if slot.startswith("_") else slot
206+
if key not in instance._fields_and_field_types:
207+
continue
207208

208-
field_type, arraylen = _handle_type_and_array_len(instance, name)
209+
fieldnames.append(key)
210+
field_type, arraylen = _handle_type_and_array_len(instance, slot)
209211
fieldarraylen.append(arraylen)
210212

211-
field_instance = getattr(instance, name)
212-
fieldtypes.append(_type_name(field_type, field_instance))
213-
214-
example = _handle_example(arraylen, field_type, field_instance)
215-
examples.append(str(example))
213+
value = getattr(instance, slot)
214+
fieldtypes.append(_type_name(field_type, value))
215+
examples.append(str(_handle_example(arraylen, field_type, value)))
216216

217217
return fieldnames, fieldtypes, fieldarraylen, examples
218218

rosapi/test/test_typedefs.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ def test_handle_sequences(self):
4141
# should be None for an atomic
4242
self.assertEqual(actual_typedef, None)
4343

44+
def test_skip_private_slots_in_array_info(self):
45+
# create a fake msg with one real field ('data') and one internal slot
46+
class MockMsg:
47+
__slots__ = ["_check_fields", "_important_data"]
48+
_fields_and_field_types = {"important_data": "int32"}
49+
50+
def __init__(self):
51+
self._important_data = 123
52+
self._check_fields = None
53+
54+
inst = MockMsg()
55+
# call the private helper directly
56+
names, types, lens, examples = objectutils._handle_array_information(inst)
57+
58+
# should only see our single '_important_data' field
59+
self.assertEqual(names, ["important_data"])
60+
# raw type should be 'int32' (no array)
61+
self.assertEqual(types, ["int32"])
62+
self.assertEqual(lens, [-1])
63+
# example should be the stringified value of 123
64+
self.assertEqual(examples, ["123"])
65+
4466

4567
if __name__ == "__main__":
4668
unittest.main()

0 commit comments

Comments
 (0)