lib: strictly type function return status enumerations
[babeltrace.git] / src / bindings / python / bt2 / bt2 / value.py
index 7f003c2bba69bf8f7d6678da3b04114168e1b211..b6a5a4a1efc1c588b491836e162137e61daf4c77 100644 (file)
@@ -29,13 +29,6 @@ import abc
 import bt2
 
 
-def _handle_status(status, obj_name):
-    if status >= 0:
-        return
-    else:
-        raise RuntimeError('unexpected error')
-
-
 def _create_from_ptr(ptr):
     if ptr is None or ptr == native_bt.value_null:
         return
@@ -63,24 +56,20 @@ def create_value(value):
     if isinstance(value, bool):
         return BoolValue(value)
 
-    if isinstance(value, int):
+    if isinstance(value, numbers.Integral):
         return SignedIntegerValue(value)
 
-    if isinstance(value, float):
+    if isinstance(value, numbers.Real):
         return RealValue(value)
 
     if isinstance(value, str):
         return StringValue(value)
 
-    try:
-        return MapValue(value)
-    except:
-        pass
-
-    try:
+    if isinstance(value, collections.abc.Sequence):
         return ArrayValue(value)
-    except:
-        pass
+
+    if isinstance(value, collections.abc.Mapping):
+        return MapValue(value)
 
     raise TypeError("cannot create value object from '{}' object".format(value.__class__.__name__))
 
@@ -92,9 +81,6 @@ class _Value(object._SharedObject, metaclass=abc.ABCMeta):
     def __ne__(self, other):
         return not (self == other)
 
-    def _handle_status(self, status):
-        _handle_status(status, self._NAME)
-
     def _check_create_status(self, ptr):
         if ptr is None:
             raise bt2.CreationError(
@@ -197,34 +183,6 @@ class _NumericValue(_Value):
     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 _IntegralValue(_NumericValue, numbers.Integral):
     def __lshift__(self, other):
@@ -260,32 +218,12 @@ class _IntegralValue(_NumericValue, 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
-
 
 class _RealValue(_NumericValue, numbers.Real):
     pass
 
 
-class BoolValue(_Value):
+class BoolValue(_IntegralValue):
     _NAME = 'Boolean'
 
     def __init__(self, value=None):
@@ -297,12 +235,6 @@ class BoolValue(_Value):
         self._check_create_status(ptr)
         super().__init__(ptr)
 
-    def __eq__(self, other):
-        try:
-            return self._value == self._value_to_bool(other)
-        except:
-            return False
-
     def __bool__(self):
         return self._value
 
@@ -340,8 +272,8 @@ class _IntegerValue(_IntegralValue):
         super().__init__(ptr)
 
     def _value_to_int(self, value):
-        if not isinstance(value, numbers.Real):
-            raise TypeError('expecting a number object')
+        if not isinstance(value, numbers.Integral):
+            raise TypeError('expecting an integral number object')
 
         value = int(value)
         self._check_int_range(value)
@@ -428,7 +360,7 @@ class StringValue(collections.abc.Sequence, _Value):
 
     def _set_value(self, value):
         status = native_bt.value_string_set(self._ptr, self._value_to_str(value))
-        self._handle_status(status)
+        utils._handle_func_status(status)
 
     value = property(fset=_set_value)
 
@@ -531,7 +463,7 @@ class ArrayValue(_Container, collections.abc.MutableSequence, _Value):
 
         status = native_bt.value_array_set_element_by_index(
             self._ptr, index, ptr)
-        self._handle_status(status)
+        utils._handle_func_status(status)
 
     def append(self, value):
         value = create_value(value)
@@ -542,7 +474,7 @@ class ArrayValue(_Container, collections.abc.MutableSequence, _Value):
             ptr = value._ptr
 
         status = native_bt.value_array_append_element(self._ptr, ptr)
-        self._handle_status(status)
+        utils._handle_func_status(status)
 
     def __iadd__(self, iterable):
         # Python will raise a TypeError if there's anything wrong with
@@ -648,7 +580,7 @@ class MapValue(_Container, collections.abc.MutableMapping, _Value):
             ptr = value._ptr
 
         status = native_bt.value_map_insert_entry(self._ptr, key, ptr)
-        self._handle_status(status)
+        utils._handle_func_status(status)
 
     def __repr__(self):
         items = ['{}: {}'.format(repr(k), repr(v)) for k, v in self.items()]
This page took 0.027367 seconds and 4 git commands to generate.