X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=bindings%2Fpython%2Fbt2%2Fbt2%2Fvalue.py;h=d8b8332eecd87bd06942ac3a839b0a96c42b5eeb;hb=68b66a256a54d32992dfefeaad11eea88b7df234;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..d8b8332e 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 @@ -67,10 +64,10 @@ def create_value(value): return BoolValue(value) if isinstance(value, int): - return IntegerValue(value) + return SignedIntegerValue(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 = staticmethod(native_bt.value_get_ref) + _put_ref = staticmethod(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,25 +357,21 @@ 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) -class IntegerValue(_IntegralValue): - _NAME = 'Integer' - +class _IntegerValue(_IntegralValue): def __init__(self, value=None): if value is None: - ptr = native_bt.value_integer_create() + ptr = self._create_default_value() else: - ptr = native_bt.value_integer_create_init(self._value_to_int(value)) + ptr = self._create_value(self._value_to_int(value)) self._check_create_status(ptr) super().__init__(ptr) @@ -401,31 +381,44 @@ class IntegerValue(_IntegralValue): raise TypeError('expecting a number object') value = int(value) - utils._check_int64(value) + self._check_int_range(value) return value @property def _value(self): - status, value = native_bt.value_integer_get(self._ptr) - assert(status == native_bt.VALUE_STATUS_OK) - return value + return self._get_value(self._ptr) - def _set_value(self, value): - status = native_bt.value_integer_set(self._ptr, self._value_to_int(value)) - self._handle_status(status) + def _prop_set_value(self, value): + self._set_value(self._ptr, self._value_to_int(value)) + + value = property(fset=_prop_set_value) - value = property(fset=_set_value) +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) -class FloatValue(_RealValue): - _NAME = 'Floating point number' + +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) + + +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 +431,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 +461,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 +485,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 +507,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 +540,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 +556,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 +569,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 +581,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 +603,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 +660,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 +677,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 +693,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): @@ -728,8 +703,9 @@ 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_UNSIGNED_INTEGER: UnsignedIntegerValue, + native_bt.VALUE_TYPE_SIGNED_INTEGER: SignedIntegerValue, + native_bt.VALUE_TYPE_REAL: RealValue, native_bt.VALUE_TYPE_STRING: StringValue, native_bt.VALUE_TYPE_ARRAY: ArrayValue, native_bt.VALUE_TYPE_MAP: MapValue,