bt2: field.py: add index check in `selected_option_index` setter of `_VariantField`
[babeltrace.git] / src / bindings / python / bt2 / bt2 / field.py
index ecbd9715753ce3e6fcec9d4f36dff19f288abd32..56f94f0fb33b8fd4ed33db31059e5849742c8420 100644 (file)
@@ -141,7 +141,7 @@ class _NumericField(_Field):
     def _spec_eq(self, other):
         try:
             return self._value == self._extract_value(other)
-        except:
+        except Exception:
             return False
 
     def __rmod__(self, other):
@@ -393,7 +393,7 @@ class _StringField(_Field):
     def _spec_eq(self, other):
         try:
             return self._value == self._value_to_str(other)
-        except:
+        except Exception:
             return False
 
     def __lt__(self, other):
@@ -559,12 +559,18 @@ class _OptionField(_Field):
 class _VariantField(_ContainerField, _Field):
     _NAME = 'Variant'
 
+    def _count(self):
+        return len(self.cls)
+
     @property
     def selected_option_index(self):
         return native_bt.field_variant_get_selected_option_field_index(self._ptr)
 
     @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
This page took 0.034592 seconds and 4 git commands to generate.