X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=bindings%2Fpython%2Fbt2%2Fbt2%2Fvalue.py;fp=bindings%2Fpython%2Fbt2%2Fbt2%2Fvalue.py;h=1a5612da49ae0bb5baf1011067513b7b40163746;hb=10a19b49e7f5db098afd319478f375f925d88f44;hp=73366fdf24766cc798fc61ef92913d64ef276b89;hpb=78288f581343ec033cc38898777fe43a02380719;p=babeltrace.git diff --git a/bindings/python/bt2/bt2/value.py b/bindings/python/bt2/bt2/value.py index 73366fdf..1a5612da 100644 --- a/bindings/python/bt2/bt2/value.py +++ b/bindings/python/bt2/bt2/value.py @@ -32,18 +32,7 @@ import bt2 def _handle_status(status, obj_name): if status >= 0: return - - if status == native_bt.VALUE_STATUS_FROZEN: - raise bt2.Frozen('{} value object is frozen'.format(obj_name)) - elif status == native_bt.VALUE_STATUS_INVAL: - # In practice, this should never happen, because arguments - # should always be validated in this Python module before - # calling the native functions. - raise ValueError('unexpected invalid argument') else: - # In practice, this should never happen, because arguments - # should always be validated in this Python module before - # calling the native functions. raise RuntimeError('unexpected error') @@ -55,6 +44,14 @@ def _create_from_ptr(ptr): return _TYPE_TO_OBJ[typeid]._create_from_ptr(ptr) +def _create_from_ptr_and_get_ref(ptr): + 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) + + def create_value(value): if value is None: # null value object @@ -70,7 +67,7 @@ def create_value(value): return IntegerValue(value) if isinstance(value, float): - return FloatValue(value) + return RealValue(value) if isinstance(value, str): return StringValue(value) @@ -89,6 +86,9 @@ def create_value(value): class _Value(object._SharedObject, metaclass=abc.ABCMeta): + _get_ref = native_bt.value_get_ref + _put_ref = native_bt.value_put_ref + def __eq__(self, other): if other is None: # self is never the null value object @@ -119,28 +119,12 @@ class _Value(object._SharedObject, metaclass=abc.ABCMeta): def _check_create_status(self, ptr): if ptr is None: - raise bt2.CreationError('cannot create {} value object'.format(self._NAME.lower())) - - def _is_frozen(self): - return native_bt.value_is_frozen(self._ptr) - - def _freeze(self): - status = native_bt.value_freeze(self._ptr) - self._handle_status(status) - - -class _BasicCopy: - def __copy__(self): - return self.__class__(self._value) - - def __deepcopy__(self, memo): - copy = self.__copy__() - memo[id(self)] = copy - return copy + raise bt2.CreationError( + 'cannot create {} value object'.format(self._NAME.lower())) @functools.total_ordering -class _NumericValue(_Value, _BasicCopy): +class _NumericValue(_Value): @staticmethod def _extract_value(other): if isinstance(other, _NumericValue): @@ -340,7 +324,7 @@ class _RealValue(_NumericValue, numbers.Real): pass -class BoolValue(_Value, _BasicCopy): +class BoolValue(_Value): _NAME = 'Boolean' def __init__(self, value=None): @@ -373,13 +357,11 @@ class BoolValue(_Value, _BasicCopy): @property def _value(self): - status, value = native_bt.value_bool_get(self._ptr) - assert(status == native_bt.VALUE_STATUS_OK) - return value > 0 + value = native_bt.value_bool_get(self._ptr) + return value != 0 def _set_value(self, value): - status = native_bt.value_bool_set(self._ptr, self._value_to_bool(value)) - self._handle_status(status) + native_bt.value_bool_set(self._ptr, self._value_to_bool(value)) value = property(fset=_set_value) @@ -406,26 +388,23 @@ class IntegerValue(_IntegralValue): @property def _value(self): - status, value = native_bt.value_integer_get(self._ptr) - assert(status == native_bt.VALUE_STATUS_OK) - return value + return native_bt.value_integer_get(self._ptr) def _set_value(self, value): - status = native_bt.value_integer_set(self._ptr, self._value_to_int(value)) - self._handle_status(status) + native_bt.value_integer_set(self._ptr, self._value_to_int(value)) value = property(fset=_set_value) -class FloatValue(_RealValue): - _NAME = 'Floating point number' +class RealValue(_RealValue): + _NAME = 'Real number' def __init__(self, value=None): if value is None: - ptr = native_bt.value_float_create() + ptr = native_bt.value_real_create() else: value = self._value_to_float(value) - ptr = native_bt.value_float_create_init(value) + ptr = native_bt.value_real_create_init(value) self._check_create_status(ptr) super().__init__(ptr) @@ -438,20 +417,16 @@ class FloatValue(_RealValue): @property def _value(self): - status, value = native_bt.value_float_get(self._ptr) - assert(status == native_bt.VALUE_STATUS_OK) - return value + return native_bt.value_real_get(self._ptr) def _set_value(self, value): - value = self._value_to_float(value) - status = native_bt.value_float_set(self._ptr, value) - self._handle_status(status) + native_bt.value_real_set(self._ptr, self._value_to_float(value)) value = property(fset=_set_value) @functools.total_ordering -class StringValue(_BasicCopy, collections.abc.Sequence, _Value): +class StringValue(collections.abc.Sequence, _Value): _NAME = 'String' def __init__(self, value=None): @@ -472,9 +447,7 @@ class StringValue(_BasicCopy, collections.abc.Sequence, _Value): @property def _value(self): - status, value = native_bt.value_string_get(self._ptr) - assert(status == native_bt.VALUE_STATUS_OK) - return value + 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)) @@ -498,7 +471,7 @@ class StringValue(_BasicCopy, collections.abc.Sequence, _Value): return bool(self._value) def __repr__(self): - repr(self._value) + return repr(self._value) def __str__(self): return self._value @@ -520,19 +493,6 @@ class _Container: def __bool__(self): return len(self) != 0 - def __copy__(self): - return self.__class__(self) - - def __deepcopy__(self, memo): - ptr = native_bt.value_copy(self._ptr) - - if ptr is None: - raise RuntimeError('unexpected error: cannot deep-copy {} value object'.format(self._NAME)) - - copy = self.__class__._create_from_ptr(ptr) - memo[id(self)] = copy - return copy - def __delitem__(self, index): raise NotImplementedError @@ -566,7 +526,7 @@ class ArrayValue(_Container, collections.abc.MutableSequence, _Value): return def __len__(self): - size = native_bt.value_array_size(self._ptr) + size = native_bt.value_array_get_size(self._ptr) assert(size >= 0) return size @@ -582,9 +542,9 @@ class ArrayValue(_Container, collections.abc.MutableSequence, _Value): def __getitem__(self, index): self._check_index(index) - ptr = native_bt.value_array_get(self._ptr, index) + ptr = native_bt.value_array_borrow_element_by_index(self._ptr, index) assert(ptr) - return _create_from_ptr(ptr) + return _create_from_ptr_and_get_ref(ptr) def __setitem__(self, index, value): self._check_index(index) @@ -595,7 +555,8 @@ class ArrayValue(_Container, collections.abc.MutableSequence, _Value): else: ptr = value._ptr - status = native_bt.value_array_set(self._ptr, index, ptr) + status = native_bt.value_array_set_element_by_index( + self._ptr, index, ptr) self._handle_status(status) def append(self, value): @@ -606,7 +567,7 @@ class ArrayValue(_Container, collections.abc.MutableSequence, _Value): else: ptr = value._ptr - status = native_bt.value_array_append(self._ptr, ptr) + status = native_bt.value_array_append_element(self._ptr, ptr) self._handle_status(status) def __iadd__(self, iterable): @@ -628,7 +589,7 @@ class _MapValueKeyIterator(collections.abc.Iterator): def __init__(self, map_obj): self._map_obj = map_obj self._at = 0 - keys_ptr = native_bt.value_map_get_keys_private(map_obj._ptr) + keys_ptr = native_bt.value_map_get_keys(map_obj._ptr) if keys_ptr is None: raise RuntimeError('unexpected error: cannot get map value object keys') @@ -685,13 +646,13 @@ class MapValue(_Container, collections.abc.MutableMapping, _Value): return def __len__(self): - size = native_bt.value_map_size(self._ptr) + size = native_bt.value_map_get_size(self._ptr) assert(size >= 0) return size def __contains__(self, key): self._check_key_type(key) - return native_bt.value_map_has_key(self._ptr, key) + return native_bt.value_map_has_entry(self._ptr, key) def _check_key_type(self, key): utils._check_str(key) @@ -702,9 +663,9 @@ class MapValue(_Container, collections.abc.MutableMapping, _Value): def __getitem__(self, key): self._check_key(key) - ptr = native_bt.value_map_get(self._ptr, key) + ptr = native_bt.value_map_borrow_entry_value(self._ptr, key) assert(ptr) - return _create_from_ptr(ptr) + return _create_from_ptr_and_get_ref(ptr) def __iter__(self): return _MapValueKeyIterator(self) @@ -718,7 +679,7 @@ class MapValue(_Container, collections.abc.MutableMapping, _Value): else: ptr = value._ptr - status = native_bt.value_map_insert(self._ptr, key, ptr) + status = native_bt.value_map_insert_entry(self._ptr, key, ptr) self._handle_status(status) def __repr__(self): @@ -729,7 +690,7 @@ class MapValue(_Container, collections.abc.MutableMapping, _Value): _TYPE_TO_OBJ = { native_bt.VALUE_TYPE_BOOL: BoolValue, native_bt.VALUE_TYPE_INTEGER: IntegerValue, - native_bt.VALUE_TYPE_REAL: FloatValue, + native_bt.VALUE_TYPE_REAL: RealValue, native_bt.VALUE_TYPE_STRING: StringValue, native_bt.VALUE_TYPE_ARRAY: ArrayValue, native_bt.VALUE_TYPE_MAP: MapValue,