def _create_from_ptr(ptr):
- if ptr is None or ptr == native_bt.value_null:
+ if ptr is None:
+ return
+
+ # 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)
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):
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
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)
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
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)
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):
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):
def __len__(self):
size = native_bt.value_map_get_size(self._ptr)
- assert(size >= 0)
+ assert size >= 0
return size
def __contains__(self, key):
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):