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: Python value field on enum type should be str #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -79,6 +79,14 @@ def value(self):
if type(elem_ft) is bt2.IntegerFieldType:
if elem_ft.size == 8 and elem_ft.encoding != bt2.Encoding.NONE:
return bytes(x for x in self._field._value if x != 0).decode()
elif type(self._field) is bt2._EnumerationField:
interger_val = self._field._value
# Iterate over the mappings and find the label of the first
# range in which the value matches.
for enum_mapping in self._field.mappings:
if interger_val >= enum_mapping.lower and \
interger_val <= enum_mapping.upper:
return enum_mapping.name

return self._field._value
except bt2.Error:
Expand Down
19 changes: 14 additions & 5 deletions tests/bindings/python/babeltrace/test_reader_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ def setUp(self):
'seh_field' : 'another string',
'sec_field' : 68752,
'ec_field' : 89,
'ef_field' : 8476,
'ef_field_int' : 8476,
'ef_field_enum' : 18,
}

self._int_ft = bt2.IntegerFieldType(32)
self._str_ft = bt2.StringFieldType()
self._enum_ft = bt2.EnumerationFieldType(size=32)
self._enum_ft.append_mapping('A', self._values['ef_field_enum'])

self._trace = bt2.Trace()
self._trace.packet_header_field_type = bt2.StructureFieldType()
Expand Down Expand Up @@ -76,7 +79,8 @@ def setUp(self):
])
self._ec.payload_field_type = bt2.StructureFieldType()
self._ec.payload_field_type += collections.OrderedDict([
('ef_field', self._int_ft),
('ef_field_int', self._int_ft),
('ef_field_enum', self._enum_ft),
])

self._sc.add_event_class(self._ec)
Expand All @@ -96,14 +100,16 @@ def setUp(self):
self._event.stream_event_context_field['sec_field'] = self._values[
'sec_field']
self._event.context_field['ec_field'] = self._values['ec_field']
self._event.payload_field['ef_field'] = self._values['ef_field']
self._event.payload_field['ef_field_int'] = self._values['ef_field_int']
self._event.payload_field['ef_field_enum'] = self._values['ef_field_enum']
self._event.packet = self._packet

def tearDown(self):
del self._trace
del self._sc
del self._ec
del self._int_ft
del self._enum_ft
del self._str_ft
del self._clock_class
del self._cc_prio_map
Expand Down Expand Up @@ -137,7 +143,10 @@ def test_attr_datetime(self):
def test_getitem(self):
event = self._get_event()
for name, value in self._values.items():
self.assertEqual(event[name], value)
if name == 'ef_field_enum':
self.assertEqual(event[name], 'A')
else:
self.assertEqual(event[name], value)

with self.assertRaises(KeyError):
field = event['non-existant-key']
Expand Down Expand Up @@ -172,7 +181,7 @@ def test_field_list_with_scope(self):
self.assertEqual(
set(event.field_list_with_scope(
babeltrace.CTFScope.EVENT_FIELDS)),
set(['ef_field']))
set(['ef_field_int', 'ef_field_enum']))

def test_field_with_scope(self):
event = self._get_event()
Expand Down