Move to kernel style SPDX license identifiers
[babeltrace.git] / src / bindings / python / bt2 / bt2 / value.py
index 2710d90330c8fa0849e055713d569fc8cf9f591f..77d1487979d4228d90187fe29e0978864ac1115e 100644 (file)
@@ -1,24 +1,6 @@
-# The MIT License (MIT)
+# SPDX-License-Identifier: MIT
 #
 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
 
 from bt2 import native_bt, object, utils
 import collections.abc
@@ -29,7 +11,7 @@ import abc
 import bt2
 
 
-def _create_from_ptr(ptr):
+def _create_from_ptr_template(ptr, object_map):
     if ptr is None:
         return
 
@@ -37,19 +19,35 @@ def _create_from_ptr(ptr):
     # 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)
+        _Value._put_ref(ptr)
         return
 
     typeid = native_bt.value_get_type(ptr)
-    return _TYPE_TO_OBJ[typeid]._create_from_ptr(ptr)
+    return object_map[typeid]._create_from_ptr(ptr)
 
 
-def _create_from_ptr_and_get_ref(ptr):
+def _create_from_ptr(ptr):
+    return _create_from_ptr_template(ptr, _TYPE_TO_OBJ)
+
+
+def _create_from_const_ptr(ptr):
+    return _create_from_ptr_template(ptr, _TYPE_TO_CONST_OBJ)
+
+
+def _create_from_ptr_and_get_ref_template(ptr, object_map):
     if ptr is None or ptr == native_bt.value_null:
         return
 
     typeid = native_bt.value_get_type(ptr)
-    return _TYPE_TO_OBJ[typeid]._create_from_ptr_and_get_ref(ptr)
+    return object_map[typeid]._create_from_ptr_and_get_ref(ptr)
+
+
+def _create_from_ptr_and_get_ref(ptr):
+    return _create_from_ptr_and_get_ref_template(ptr, _TYPE_TO_OBJ)
+
+
+def _create_from_const_ptr_and_get_ref(ptr):
+    return _create_from_ptr_and_get_ref_template(ptr, _TYPE_TO_CONST_OBJ)
 
 
 def create_value(value):
@@ -78,27 +76,39 @@ def create_value(value):
     if isinstance(value, collections.abc.Mapping):
         return MapValue(value)
 
-    raise TypeError("cannot create value object from '{}' object".format(value.__class__.__name__))
+    raise TypeError(
+        "cannot create value object from '{}' object".format(value.__class__.__name__)
+    )
 
 
-class _Value(object._SharedObject, metaclass=abc.ABCMeta):
+class _ValueConst(object._SharedObject, metaclass=abc.ABCMeta):
     _get_ref = staticmethod(native_bt.value_get_ref)
     _put_ref = staticmethod(native_bt.value_put_ref)
+    _create_value_from_ptr = staticmethod(_create_from_const_ptr)
+    _create_value_from_ptr_and_get_ref = staticmethod(
+        _create_from_const_ptr_and_get_ref
+    )
 
     def __ne__(self, other):
         return not (self == other)
 
     def _check_create_status(self, ptr):
         if ptr is None:
-            raise bt2.CreationError(
-                'cannot create {} value object'.format(self._NAME.lower()))
+            raise bt2._MemoryError(
+                'cannot create {} value object'.format(self._NAME.lower())
+            )
+
+
+class _Value(_ValueConst):
+    _create_value_from_ptr = staticmethod(_create_from_ptr)
+    _create_value_from_ptr_and_get_ref = staticmethod(_create_from_ptr_and_get_ref)
 
 
 @functools.total_ordering
-class _NumericValue(_Value):
+class _NumericValueConst(_ValueConst):
     @staticmethod
     def _extract_value(other):
-        if isinstance(other, BoolValue) or isinstance(other, bool):
+        if isinstance(other, _BoolValueConst) or isinstance(other, bool):
             return bool(other)
 
         if isinstance(other, numbers.Integral):
@@ -110,7 +120,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)
@@ -127,7 +139,7 @@ class _NumericValue(_Value):
     def __eq__(self, other):
         try:
             return self._value == self._extract_value(other)
-        except:
+        except Exception:
             return False
 
     def __rmod__(self, other):
@@ -191,7 +203,11 @@ class _NumericValue(_Value):
         return self._extract_value(base) ** self._value
 
 
-class _IntegralValue(_NumericValue, numbers.Integral):
+class _NumericValue(_NumericValueConst, _Value):
+    pass
+
+
+class _IntegralValueConst(_NumericValueConst, numbers.Integral):
     def __lshift__(self, other):
         return self._value << self._extract_value(other)
 
@@ -226,11 +242,26 @@ class _IntegralValue(_NumericValue, numbers.Integral):
         return ~self._value
 
 
-class _RealValue(_NumericValue, numbers.Real):
+class _IntegralValue(_IntegralValueConst, _NumericValue):
     pass
 
 
-class BoolValue(_IntegralValue):
+class _BoolValueConst(_IntegralValueConst):
+    _NAME = 'Const boolean'
+
+    def __bool__(self):
+        return self._value
+
+    def __repr__(self):
+        return repr(self._value)
+
+    @property
+    def _value(self):
+        value = native_bt.value_bool_get(self._ptr)
+        return value != 0
+
+
+class BoolValue(_BoolValueConst, _IntegralValue):
     _NAME = 'Boolean'
 
     def __init__(self, value=None):
@@ -242,33 +273,33 @@ class BoolValue(_IntegralValue):
         self._check_create_status(ptr)
         super().__init__(ptr)
 
-    def __bool__(self):
-        return self._value
-
-    def __repr__(self):
-        return repr(self._value)
-
-    def _value_to_bool(self, value):
-        if isinstance(value, BoolValue):
+    @classmethod
+    def _value_to_bool(cls, value):
+        if isinstance(value, _BoolValueConst):
             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', 'BoolValue', or '_BoolValueConst' object".format(
+                    value.__class__
+                )
+            )
 
         return value
 
-    @property
-    def _value(self):
-        value = native_bt.value_bool_get(self._ptr)
-        return value != 0
-
     def _set_value(self, value):
         native_bt.value_bool_set(self._ptr, self._value_to_bool(value))
 
     value = property(fset=_set_value)
 
 
-class _IntegerValue(_IntegralValue):
+class _IntegerValueConst(_IntegralValueConst):
+    @property
+    def _value(self):
+        return self._get_value(self._ptr)
+
+
+class _IntegerValue(_IntegerValueConst, _IntegralValue):
     def __init__(self, value=None):
         if value is None:
             ptr = self._create_default_value()
@@ -278,41 +309,56 @@ class _IntegerValue(_IntegralValue):
         self._check_create_status(ptr)
         super().__init__(ptr)
 
-    def _value_to_int(self, value):
+    @classmethod
+    def _value_to_int(cls, value):
         if not isinstance(value, numbers.Integral):
             raise TypeError('expecting an integral number object')
 
         value = int(value)
-        self._check_int_range(value)
+        cls._check_int_range(value)
         return value
 
-    @property
-    def _value(self):
-        return self._get_value(self._ptr)
-
     def _prop_set_value(self, value):
         self._set_value(self._ptr, self._value_to_int(value))
 
     value = property(fset=_prop_set_value)
 
 
-class UnsignedIntegerValue(_IntegerValue):
+class _UnsignedIntegerValueConst(_IntegerValueConst):
+    _NAME = 'Const unsigned integer'
+    _get_value = staticmethod(native_bt.value_integer_unsigned_get)
+
+
+class UnsignedIntegerValue(_UnsignedIntegerValueConst, _IntegerValue):
+    _NAME = 'Unsigned integer'
     _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)
+
+
+class _SignedIntegerValueConst(_IntegerValueConst):
+    _NAME = 'Const signed integer'
+    _get_value = staticmethod(native_bt.value_integer_signed_get)
 
 
-class SignedIntegerValue(_IntegerValue):
+class SignedIntegerValue(_SignedIntegerValueConst, _IntegerValue):
+    _NAME = 'Signed integer'
     _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)
 
 
-class RealValue(_RealValue):
+class _RealValueConst(_NumericValueConst, numbers.Real):
+    _NAME = 'Const real number'
+
+    @property
+    def _value(self):
+        return native_bt.value_real_get(self._ptr)
+
+
+class RealValue(_RealValueConst, _NumericValue):
     _NAME = 'Real number'
 
     def __init__(self, value=None):
@@ -325,16 +371,13 @@ class RealValue(_RealValue):
         self._check_create_status(ptr)
         super().__init__(ptr)
 
-    def _value_to_float(self, value):
+    @classmethod
+    def _value_to_float(cls, value):
         if not isinstance(value, numbers.Real):
             raise TypeError("expecting a real number object")
 
         return float(value)
 
-    @property
-    def _value(self):
-        return native_bt.value_real_get(self._ptr)
-
     def _set_value(self, value):
         native_bt.value_real_set(self._ptr, self._value_to_float(value))
 
@@ -342,20 +385,12 @@ class RealValue(_RealValue):
 
 
 @functools.total_ordering
-class StringValue(collections.abc.Sequence, _Value):
-    _NAME = 'String'
+class _StringValueConst(collections.abc.Sequence, _Value):
+    _NAME = 'Const string'
 
-    def __init__(self, value=None):
-        if value is None:
-            ptr = native_bt.value_string_create()
-        else:
-            ptr = native_bt.value_string_create_init(self._value_to_str(value))
-
-        self._check_create_status(ptr)
-        super().__init__(ptr)
-
-    def _value_to_str(self, value):
-        if isinstance(value, self.__class__):
+    @classmethod
+    def _value_to_str(cls, value):
+        if isinstance(value, _StringValueConst):
             value = value._value
 
         utils._check_str(value)
@@ -365,16 +400,10 @@ class StringValue(collections.abc.Sequence, _Value):
     def _value(self):
         return native_bt.value_string_get(self._ptr)
 
-    def _set_value(self, value):
-        status = native_bt.value_string_set(self._ptr, self._value_to_str(value))
-        utils._handle_func_status(status)
-
-    value = property(fset=_set_value)
-
     def __eq__(self, other):
         try:
             return self._value == self._value_to_str(other)
-        except:
+        except Exception:
             return False
 
     def __lt__(self, other):
@@ -395,6 +424,28 @@ class StringValue(collections.abc.Sequence, _Value):
     def __len__(self):
         return len(self._value)
 
+    def __contains__(self, item):
+        return self._value_to_str(item) in self._value
+
+
+class StringValue(_StringValueConst, _Value):
+    _NAME = 'String'
+
+    def __init__(self, value=None):
+        if value is None:
+            ptr = native_bt.value_string_create()
+        else:
+            ptr = native_bt.value_string_create_init(self._value_to_str(value))
+
+        self._check_create_status(ptr)
+        super().__init__(ptr)
+
+    def _set_value(self, value):
+        status = native_bt.value_string_set(self._ptr, self._value_to_str(value))
+        utils._handle_func_status(status)
+
+    value = property(fset=_set_value)
+
     def __iadd__(self, value):
         curvalue = self._value
         curvalue += self._value_to_str(value)
@@ -402,27 +453,22 @@ class StringValue(collections.abc.Sequence, _Value):
         return self
 
 
-class _Container:
+class _ContainerConst:
     def __bool__(self):
         return len(self) != 0
 
+
+class _Container(_ContainerConst):
     def __delitem__(self, index):
         raise NotImplementedError
 
 
-class ArrayValue(_Container, collections.abc.MutableSequence, _Value):
-    _NAME = 'Array'
-
-    def __init__(self, value=None):
-        ptr = native_bt.value_array_create()
-        self._check_create_status(ptr)
-        super().__init__(ptr)
-
-        # Python will raise a TypeError if there's anything wrong with
-        # the iterable protocol.
-        if value is not None:
-            for elem in value:
-                self.append(elem)
+class _ArrayValueConst(_ContainerConst, collections.abc.Sequence, _ValueConst):
+    _NAME = 'Const array'
+    _borrow_element_by_index = staticmethod(
+        native_bt.value_array_borrow_element_by_index_const
+    )
+    _is_const = True
 
     def __eq__(self, other):
         if not isinstance(other, collections.abc.Sequence):
@@ -439,14 +485,18 @@ class ArrayValue(_Container, collections.abc.MutableSequence, _Value):
         return True
 
     def __len__(self):
-        size = native_bt.value_array_get_size(self._ptr)
-        assert(size >= 0)
+        size = native_bt.value_array_get_length(self._ptr)
+        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)
 
@@ -455,9 +505,30 @@ 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)
-        return _create_from_ptr_and_get_ref(ptr)
+        ptr = self._borrow_element_by_index(self._ptr, index)
+        assert ptr
+        return self._create_value_from_ptr_and_get_ref(ptr)
+
+    def __repr__(self):
+        return '[{}]'.format(', '.join([repr(v) for v in self]))
+
+
+class ArrayValue(_ArrayValueConst, _Container, collections.abc.MutableSequence, _Value):
+    _NAME = 'Array'
+    _borrow_element_by_index = staticmethod(
+        native_bt.value_array_borrow_element_by_index
+    )
+
+    def __init__(self, value=None):
+        ptr = native_bt.value_array_create()
+        self._check_create_status(ptr)
+        super().__init__(ptr)
+
+        # Python will raise a TypeError if there's anything wrong with
+        # the iterable protocol.
+        if value is not None:
+            for elem in value:
+                self.append(elem)
 
     def __setitem__(self, index, value):
         self._check_index(index)
@@ -468,8 +539,7 @@ class ArrayValue(_Container, collections.abc.MutableSequence, _Value):
         else:
             ptr = value._ptr
 
-        status = native_bt.value_array_set_element_by_index(
-            self._ptr, index, ptr)
+        status = native_bt.value_array_set_element_by_index(self._ptr, index, ptr)
         utils._handle_func_status(status)
 
     def append(self, value):
@@ -491,9 +561,6 @@ class ArrayValue(_Container, collections.abc.MutableSequence, _Value):
 
         return self
 
-    def __repr__(self):
-        return '[{}]'.format(', '.join([repr(v) for v in self]))
-
     def insert(self, value):
         raise NotImplementedError
 
@@ -518,19 +585,9 @@ class _MapValueKeyIterator(collections.abc.Iterator):
         return str(key)
 
 
-class MapValue(_Container, collections.abc.MutableMapping, _Value):
-    _NAME = 'Map'
-
-    def __init__(self, value=None):
-        ptr = native_bt.value_map_create()
-        self._check_create_status(ptr)
-        super().__init__(ptr)
-
-        # Python will raise a TypeError if there's anything wrong with
-        # the iterable/mapping protocol.
-        if value is not None:
-            for key, elem in value.items():
-                self[key] = elem
+class _MapValueConst(_ContainerConst, collections.abc.Mapping, _ValueConst):
+    _NAME = 'Const map'
+    _borrow_entry_value_ptr = staticmethod(native_bt.value_map_borrow_entry_value_const)
 
     def __ne__(self, other):
         return _Value.__ne__(self, other)
@@ -554,7 +611,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):
@@ -570,13 +627,33 @@ 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)
-        return _create_from_ptr_and_get_ref(ptr)
+        ptr = self._borrow_entry_value_ptr(self._ptr, key)
+        assert ptr
+        return self._create_value_from_ptr_and_get_ref(ptr)
 
     def __iter__(self):
         return _MapValueKeyIterator(self)
 
+    def __repr__(self):
+        items = ['{}: {}'.format(repr(k), repr(v)) for k, v in self.items()]
+        return '{{{}}}'.format(', '.join(items))
+
+
+class MapValue(_MapValueConst, _Container, collections.abc.MutableMapping, _Value):
+    _NAME = 'Map'
+    _borrow_entry_value_ptr = staticmethod(native_bt.value_map_borrow_entry_value)
+
+    def __init__(self, value=None):
+        ptr = native_bt.value_map_create()
+        self._check_create_status(ptr)
+        super().__init__(ptr)
+
+        # Python will raise a TypeError if there's anything wrong with
+        # the iterable/mapping protocol.
+        if value is not None:
+            for key, elem in value.items():
+                self[key] = elem
+
     def __setitem__(self, key, value):
         self._check_key_type(key)
         value = create_value(value)
@@ -589,10 +666,6 @@ class MapValue(_Container, collections.abc.MutableMapping, _Value):
         status = native_bt.value_map_insert_entry(self._ptr, key, ptr)
         utils._handle_func_status(status)
 
-    def __repr__(self):
-        items = ['{}: {}'.format(repr(k), repr(v)) for k, v in self.items()]
-        return '{{{}}}'.format(', '.join(items))
-
 
 _TYPE_TO_OBJ = {
     native_bt.VALUE_TYPE_BOOL: BoolValue,
@@ -603,3 +676,13 @@ _TYPE_TO_OBJ = {
     native_bt.VALUE_TYPE_ARRAY: ArrayValue,
     native_bt.VALUE_TYPE_MAP: MapValue,
 }
+
+_TYPE_TO_CONST_OBJ = {
+    native_bt.VALUE_TYPE_BOOL: _BoolValueConst,
+    native_bt.VALUE_TYPE_UNSIGNED_INTEGER: _UnsignedIntegerValueConst,
+    native_bt.VALUE_TYPE_SIGNED_INTEGER: _SignedIntegerValueConst,
+    native_bt.VALUE_TYPE_REAL: _RealValueConst,
+    native_bt.VALUE_TYPE_STRING: _StringValueConst,
+    native_bt.VALUE_TYPE_ARRAY: _ArrayValueConst,
+    native_bt.VALUE_TYPE_MAP: _MapValueConst,
+}
This page took 0.028792 seconds and 4 git commands to generate.