bt2: field.py: add index check in `selected_option_index` setter of `_VariantField`
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Thu, 5 Sep 2019 22:12:44 +0000 (18:12 -0400)
committerFrancis Deslauriers <francis.deslauriers@efficios.com>
Tue, 10 Sep 2019 01:05:12 +0000 (21:05 -0400)
Also, add corresponding test case.

Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Change-Id: Ia0b585adba02d71008f9f63298eb017db4a4fabb
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2006
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
src/bindings/python/bt2/bt2/field.py
tests/bindings/python/bt2/test_field.py

index 7d5fdd83e1726a992d8dcb72e797cf63db0a3bfd..56f94f0fb33b8fd4ed33db31059e5849742c8420 100644 (file)
@@ -568,6 +568,9 @@ class _VariantField(_ContainerField, _Field):
 
     @selected_option_index.setter
     def selected_option_index(self, index):
+        if index < 0 or index >= len(self):
+            raise IndexError('{} field object index is out of range'.format(self._NAME))
+
         native_bt.field_variant_select_option_field_by_index(self._ptr, index)
 
     @property
index f478515fdfec23f10731ee22047d4a8e230ec3b5..5a338801245dfad89ac374b0c93d7e6af528a5d9 100644 (file)
@@ -1970,6 +1970,14 @@ class VariantFieldTestCase(unittest.TestCase):
         self._def.selected_option_index = 2
         self.assertEqual(self._def.selected_option_index, 2)
 
+    def test_selected_option_index_above_range(self):
+        with self.assertRaises(IndexError):
+            self._def.selected_option_index = 4
+
+    def test_selected_option_index_below_range(self):
+        with self.assertRaises(IndexError):
+            self._def.selected_option_index = -1
+
     def test_selected_option(self):
         self._def.selected_option_index = 2
         self._def.value = -17.34
This page took 0.025823 seconds and 4 git commands to generate.