When calling `_EnumerationFieldClassConst.mappings_for_value()` and the
result is empty (no matching mappings), we get:
File "/home/smarchi/build/babeltrace/src/bindings/python/bt2/build/build_lib/bt2/field_class.py", line 240, in mappings_for_value
return [self[label] for label in labels]
^^^^^^
TypeError: 'NoneType' object is not iterable
Here's what happens:
- When the input value matches no mappings, the library sets the array
output parameter to `NULL`.
- The SWIG argout typemap checks the array output parameter value to
choose between returning a list or `None`. So in this case, we return
the tuple `(__BT_FUNC_STATUS_OK, None)`.
- Back in the Python side, we try to iterate over `None`.
Fix this by checking the status in the typemap to see if the call
succeeded.
Add a corresponding test.
Change-Id: If1c7de435c27b9c37b67b66a7b8384480ac654e1
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/13385
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
%typemap(argout)
(bt_field_class_enumeration_mapping_label_array *labels, uint64_t *count) {
- if (*$1) {
+ if (result == __BT_FUNC_STATUS_OK) {
PyObject *py_label_list = PyList_New(*$2);
uint64_t i;
expected_labels = set(["a", "c"])
self.assertEqual(labels, expected_labels)
+ def test_find_by_value_none_found(self):
+ self._fc.add_mapping("a", self._ranges1)
+ mappings = self._fc.mappings_for_value(999999)
+ self.assertEqual(mappings, [])
+
class UnsignedEnumerationFieldClassTestCase(
_EnumerationFieldClassTestCase, _TestFieldClass, unittest.TestCase