lib: rename functions to clearly indicate API inheritance
[babeltrace.git] / src / bindings / python / bt2 / bt2 / value.py
index 81a72ffa785e7d735736e839714c30db69dfd315..9ba010869a9875382dd73716dbb75a78c62b4b57 100644 (file)
@@ -29,15 +29,15 @@ import abc
 import bt2
 
 
-def _handle_status(status, obj_name):
-    if status >= 0:
+def _create_from_ptr(ptr):
+    if ptr is None:
         return
-    else:
-        raise RuntimeError('unexpected error')
-
 
-def _create_from_ptr(ptr):
-    if ptr is None or ptr == native_bt.value_null:
+    # bt_value_null is translated to None.  However, we are given a reference
+    # to it that we are not going to manage anymore, since we don't create a
+    # Python wrapper for it.  Therefore put that reference immediately.
+    if ptr == native_bt.value_null:
+        bt2.value._Value._put_ref(ptr)
         return
 
     typeid = native_bt.value_get_type(ptr)
@@ -63,26 +63,24 @@ 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
 
-    raise TypeError("cannot create value object from '{}' object".format(value.__class__.__name__))
+    if isinstance(value, collections.abc.Mapping):
+        return MapValue(value)
+
+    raise TypeError(
+        "cannot create value object from '{}' object".format(value.__class__.__name__)
+    )
 
 
 class _Value(object._SharedObject, metaclass=abc.ABCMeta):
@@ -92,13 +90,11 @@ 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(
-                'cannot create {} value object'.format(self._NAME.lower()))
+                'cannot create {} value object'.format(self._NAME.lower())
+            )
 
 
 @functools.total_ordering
@@ -117,7 +113,9 @@ class _NumericValue(_Value):
         if isinstance(other, numbers.Complex):
             return complex(other)
 
-        raise TypeError("'{}' object is not a number object".format(other.__class__.__name__))
+        raise TypeError(
+            "'{}' object is not a number object".format(other.__class__.__name__)
+        )
 
     def __int__(self):
         return int(self._value)
@@ -260,7 +258,11 @@ class BoolValue(_IntegralValue):
             value = value._value
 
         if not isinstance(value, bool):
-            raise TypeError("'{}' object is not a 'bool' or 'BoolValue' object".format(value.__class__))
+            raise TypeError(
+                "'{}' object is not a 'bool' or 'BoolValue' object".format(
+                    value.__class__
+                )
+            )
 
         return value
 
@@ -305,18 +307,18 @@ class _IntegerValue(_IntegralValue):
 
 class UnsignedIntegerValue(_IntegerValue):
     _check_int_range = staticmethod(utils._check_uint64)
-    _create_default_value = staticmethod(native_bt.value_unsigned_integer_create)
-    _create_value = staticmethod(native_bt.value_unsigned_integer_create_init)
-    _set_value = staticmethod(native_bt.value_unsigned_integer_set)
-    _get_value = staticmethod(native_bt.value_unsigned_integer_get)
+    _create_default_value = staticmethod(native_bt.value_integer_unsigned_create)
+    _create_value = staticmethod(native_bt.value_integer_unsigned_create_init)
+    _set_value = staticmethod(native_bt.value_integer_unsigned_set)
+    _get_value = staticmethod(native_bt.value_integer_unsigned_get)
 
 
 class SignedIntegerValue(_IntegerValue):
     _check_int_range = staticmethod(utils._check_int64)
-    _create_default_value = staticmethod(native_bt.value_signed_integer_create)
-    _create_value = staticmethod(native_bt.value_signed_integer_create_init)
-    _set_value = staticmethod(native_bt.value_signed_integer_set)
-    _get_value = staticmethod(native_bt.value_signed_integer_get)
+    _create_default_value = staticmethod(native_bt.value_integer_signed_create)
+    _create_value = staticmethod(native_bt.value_integer_signed_create_init)
+    _set_value = staticmethod(native_bt.value_integer_signed_set)
+    _get_value = staticmethod(native_bt.value_integer_signed_get)
 
 
 class RealValue(_RealValue):
@@ -374,7 +376,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)
 
@@ -447,13 +449,17 @@ class ArrayValue(_Container, collections.abc.MutableSequence, _Value):
 
     def __len__(self):
         size = native_bt.value_array_get_size(self._ptr)
-        assert(size >= 0)
+        assert size >= 0
         return size
 
     def _check_index(self, index):
         # TODO: support slices also
         if not isinstance(index, numbers.Integral):
-            raise TypeError("'{}' object is not an integral number object: invalid index".format(index.__class__.__name__))
+            raise TypeError(
+                "'{}' object is not an integral number object: invalid index".format(
+                    index.__class__.__name__
+                )
+            )
 
         index = int(index)
 
@@ -463,7 +469,7 @@ class ArrayValue(_Container, collections.abc.MutableSequence, _Value):
     def __getitem__(self, index):
         self._check_index(index)
         ptr = native_bt.value_array_borrow_element_by_index(self._ptr, index)
-        assert(ptr)
+        assert ptr
         return _create_from_ptr_and_get_ref(ptr)
 
     def __setitem__(self, index, value):
@@ -475,9 +481,8 @@ class ArrayValue(_Container, collections.abc.MutableSequence, _Value):
         else:
             ptr = value._ptr
 
-        status = native_bt.value_array_set_element_by_index(
-            self._ptr, index, ptr)
-        self._handle_status(status)
+        status = native_bt.value_array_set_element_by_index(self._ptr, index, ptr)
+        utils._handle_func_status(status)
 
     def append(self, value):
         value = create_value(value)
@@ -488,7 +493,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
@@ -561,7 +566,7 @@ class MapValue(_Container, collections.abc.MutableMapping, _Value):
 
     def __len__(self):
         size = native_bt.value_map_get_size(self._ptr)
-        assert(size >= 0)
+        assert size >= 0
         return size
 
     def __contains__(self, key):
@@ -578,7 +583,7 @@ class MapValue(_Container, collections.abc.MutableMapping, _Value):
     def __getitem__(self, key):
         self._check_key(key)
         ptr = native_bt.value_map_borrow_entry_value(self._ptr, key)
-        assert(ptr)
+        assert ptr
         return _create_from_ptr_and_get_ref(ptr)
 
     def __iter__(self):
@@ -594,7 +599,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.026067 seconds and 4 git commands to generate.