From 4e68394b2fabde325ae434211f64f3d2045ab1f4 Mon Sep 17 00:00:00 2001 From: Francis Deslauriers Date: Fri, 2 Mar 2018 13:56:15 -0500 Subject: [PATCH] Fix: Python value field on enum type should be str The current implementation returns the integer value of the numeration but Babeltrace 1.X was returning the text label. To ensure compatibility with Babeltrace 1.X, when getting the value of an enum field, return a string containing the corresponding label for this value. This commit also adds a test for this behaviour. Signed-off-by: Francis Deslauriers --- .../babeltrace/reader_field_definition.py | 8 ++++++++ .../python/babeltrace/test_reader_event.py | 19 ++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/bindings/python/babeltrace/babeltrace/reader_field_definition.py b/bindings/python/babeltrace/babeltrace/reader_field_definition.py index 278ca00c0..b674f561b 100644 --- a/bindings/python/babeltrace/babeltrace/reader_field_definition.py +++ b/bindings/python/babeltrace/babeltrace/reader_field_definition.py @@ -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: diff --git a/tests/bindings/python/babeltrace/test_reader_event.py b/tests/bindings/python/babeltrace/test_reader_event.py index 1ea1ce51e..8e800a412 100644 --- a/tests/bindings/python/babeltrace/test_reader_event.py +++ b/tests/bindings/python/babeltrace/test_reader_event.py @@ -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() @@ -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) @@ -96,7 +100,8 @@ 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): @@ -104,6 +109,7 @@ def tearDown(self): 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 @@ -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'] @@ -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()