lib: strictly type function return status enumerations
[babeltrace.git] / src / bindings / python / bt2 / bt2 / field.py
index f5b9b59645365bcfcca2079101986fa516109602..0f3e5e7ee32eb11b797f370723f52de5145ca098 100644 (file)
@@ -71,7 +71,7 @@ class _Field(object._UniqueObject):
 class _NumericField(_Field):
     @staticmethod
     def _extract_value(other):
-        if other is True or other is False:
+        if isinstance(other, bool):
             return other
 
         if isinstance(other, numbers.Integral):
@@ -99,13 +99,13 @@ class _NumericField(_Field):
             raise TypeError('unorderable types: {}() < {}()'.format(self.__class__.__name__,
                                                                     other.__class__.__name__))
 
-        return self._value < float(other)
+        return self._value < self._extract_value(other)
 
     def _spec_eq(self, other):
-        if not isinstance(other, numbers.Number):
-            return NotImplemented
-
-        return self._value == complex(other)
+        try:
+            return self._value == self._extract_value(other)
+        except:
+            return False
 
     def __rmod__(self, other):
         return self._extract_value(other) % self._value
@@ -167,34 +167,6 @@ class _NumericField(_Field):
     def __rpow__(self, base):
         return self._extract_value(base) ** self._value
 
-    def __iadd__(self, other):
-        self.value = self + other
-        return self
-
-    def __isub__(self, other):
-        self.value = self - other
-        return self
-
-    def __imul__(self, other):
-        self.value = self * other
-        return self
-
-    def __itruediv__(self, other):
-        self.value = self / other
-        return self
-
-    def __ifloordiv__(self, other):
-        self.value = self // other
-        return self
-
-    def __imod__(self, other):
-        self.value = self % other
-        return self
-
-    def __ipow__(self, other):
-        self.value = self ** other
-        return self
-
 
 class _IntegralField(_NumericField, numbers.Integral):
     def __lshift__(self, other):
@@ -230,38 +202,6 @@ class _IntegralField(_NumericField, numbers.Integral):
     def __invert__(self):
         return ~self._value
 
-    def __ilshift__(self, other):
-        self.value = self << other
-        return self
-
-    def __irshift__(self, other):
-        self.value = self >> other
-        return self
-
-    def __iand__(self, other):
-        self.value = self & other
-        return self
-
-    def __ixor__(self, other):
-        self.value = self ^ other
-        return self
-
-    def __ior__(self, other):
-        self.value = self | other
-        return self
-
-    def __lt__(self, other):
-        if not isinstance(other, numbers.Integral):
-            return super().__lt__(other);
-
-        return self._value < int(other)
-
-    def _spec_eq(self, other):
-        if not isinstance(other, numbers.Integral):
-            return super()._spec_eq(other);
-
-        return self._value == int(other)
-
 
 class _IntegerField(_IntegralField, _Field):
     pass
@@ -271,8 +211,8 @@ class _UnsignedIntegerField(_IntegerField, _Field):
     _NAME = 'Unsigned integer'
 
     def _value_to_int(self, value):
-        if not isinstance(value, numbers.Real):
-            raise TypeError('expecting a real number object')
+        if not isinstance(value, numbers.Integral):
+            raise TypeError('expecting an integral number object')
 
         value = int(value)
         utils._check_uint64(value)
@@ -294,8 +234,8 @@ class _SignedIntegerField(_IntegerField, _Field):
     _NAME = 'Signed integer'
 
     def _value_to_int(self, value):
-        if not isinstance(value, numbers.Real):
-            raise TypeError('expecting a real number object')
+        if not isinstance(value, numbers.Integral):
+            raise TypeError('expecting an integral number object')
 
         value = int(value)
         utils._check_int64(value)
@@ -339,8 +279,9 @@ class _EnumerationField(_IntegerField):
 
     @property
     def labels(self):
-        ret, labels = self._get_mapping_labels(self._ptr)
-        utils._handle_ret(ret, "cannot get label for enumeration field")
+        status, labels = self._get_mapping_labels(self._ptr)
+        utils._handle_func_status(status,
+                                  "cannot get label for enumeration field")
 
         assert labels is not None
         return labels
@@ -381,12 +322,10 @@ class _StringField(_Field):
 
     def _spec_eq(self, other):
         try:
-            other = self._value_to_str(other)
-        except Exception:
+            return self._value == self._value_to_str(other)
+        except:
             return False
 
-        return self._value == other
-
     def __lt__(self, other):
         return self._value < self._value_to_str(other)
 
@@ -407,8 +346,9 @@ class _StringField(_Field):
 
     def __iadd__(self, value):
         value = self._value_to_str(value)
-        ret = native_bt.field_string_append(self._ptr, value)
-        utils._handle_ret(ret, "cannot append to string field object's value")
+        status = native_bt.field_string_append(self._ptr, value)
+        utils._handle_func_status(status,
+                                  "cannot append to string field object's value")
         return self
 
 
@@ -444,22 +384,21 @@ class _StructureField(_ContainerField, collections.abc.MutableMapping):
         return iter(self.field_class)
 
     def _spec_eq(self, other):
-        try:
-            if len(self) != len(other):
-                return False
+        if not isinstance(other, collections.abc.Mapping):
+            return False
 
-            for self_key, self_value in self.items():
-                if self_key not in other:
-                    return False
+        if len(self) != len(other):
+            # early mismatch
+            return False
 
-                other_value = other[self_key]
+        for self_key in self:
+            if self_key not in other:
+                return False
 
-                if self_value != other_value:
-                    return False
+            if self[self_key] != other[self_key]:
+                return False
 
-            return True
-        except Exception:
-            return False
+        return True
 
     def _set_value(self, values):
         try:
@@ -519,8 +458,7 @@ class _VariantField(_ContainerField, _Field):
                                       self._owner_put_ref)
 
     def _spec_eq(self, other):
-        new_self = _get_leaf_field(self)
-        return new_self == other
+        return _get_leaf_field(self) == other
 
     def __bool__(self):
         raise NotImplementedError
@@ -537,8 +475,7 @@ class _VariantField(_ContainerField, _Field):
     value = property(fset=_set_value)
 
 
-class _ArrayField(_ContainerField, _Field):
-
+class _ArrayField(_ContainerField, _Field, collections.abc.MutableSequence):
     def _get_length(self):
         return native_bt.field_array_get_length(self._ptr)
 
@@ -560,10 +497,6 @@ class _ArrayField(_ContainerField, _Field):
                                       self._owner_put_ref)
 
     def __setitem__(self, index, value):
-        # we can only set numbers and strings
-        if not isinstance(value, (numbers.Number, _StringField, str)):
-            raise TypeError('expecting number or string object')
-
         # raises if index is somehow invalid
         field = self[index]
 
@@ -578,18 +511,19 @@ class _ArrayField(_ContainerField, _Field):
         raise NotImplementedError
 
     def _spec_eq(self, other):
-        try:
-            if len(self) != len(other):
-                return False
-
-            for self_field, other_field in zip(self, other):
-                if self_field != other_field:
-                    return False
+        if not isinstance(other, collections.abc.Sequence):
+            return False
 
-            return True
-        except Exception:
+        if len(self) != len(other):
+            # early mismatch
             return False
 
+        for self_elem, other_elem in zip(self, other):
+            if self_elem != other_elem:
+                return False
+
+        return True
+
     def _repr(self):
         return '[{}]'.format(', '.join([repr(v) for v in self]))
 
@@ -620,8 +554,8 @@ class _DynamicArrayField(_ArrayField, _Field):
 
     def _set_length(self, length):
         utils._check_uint64(length)
-        ret = native_bt.field_dynamic_array_set_length(self._ptr, length)
-        utils._handle_ret(ret, "cannot set dynamic array length")
+        status = native_bt.field_dynamic_array_set_length(self._ptr, length)
+        utils._handle_func_status(status, "cannot set dynamic array length")
 
     length = property(fget=_ArrayField._get_length, fset=_set_length)
 
This page took 0.027179 seconds and 4 git commands to generate.