bt2: clean available `bt2` package names
[babeltrace.git] / src / bindings / python / bt2 / bt2 / field.py
index a1c2bf32c25fd0a96dfb8fe6973697f9d997bacd..c8e292b195ff65f99b22ee21e43bcd52cfc94575 100644 (file)
@@ -21,7 +21,7 @@
 # THE SOFTWARE.
 
 from bt2 import native_bt, object, utils
-import bt2.field_class
+from bt2 import field_class as bt2_field_class
 import collections.abc
 import functools
 import numbers
@@ -31,10 +31,10 @@ import bt2
 
 def _create_field_from_ptr(ptr, owner_ptr, owner_get_ref, owner_put_ref):
     field_class_ptr = native_bt.field_borrow_class_const(ptr)
-    utils._handle_ptr(field_class_ptr, "cannot get field object's class")
     typeid = native_bt.field_class_get_type(field_class_ptr)
     field = _TYPE_ID_TO_OBJ[typeid]._create_from_ptr_and_get_ref(
-        ptr, owner_ptr, owner_get_ref, owner_put_ref)
+        ptr, owner_ptr, owner_get_ref, owner_put_ref
+    )
     return field
 
 
@@ -42,6 +42,7 @@ def _create_field_from_ptr(ptr, owner_ptr, owner_get_ref, owner_put_ref):
 # currently selected field.  If `field` is of any other type, return `field`
 # directly.
 
+
 def _get_leaf_field(field):
     if not isinstance(field, _VariantField):
         return field
@@ -58,7 +59,7 @@ class _Field(object._UniqueObject):
     def field_class(self):
         field_class_ptr = native_bt.field_borrow_class_const(self._ptr)
         assert field_class_ptr is not None
-        return bt2.field_class._create_field_class_from_ptr_and_get_ref(field_class_ptr)
+        return bt2_field_class._create_field_class_from_ptr_and_get_ref(field_class_ptr)
 
     def _repr(self):
         raise NotImplementedError
@@ -83,7 +84,9 @@ class _NumericField(_Field):
         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)
@@ -96,8 +99,11 @@ class _NumericField(_Field):
 
     def __lt__(self, other):
         if not isinstance(other, numbers.Number):
-            raise TypeError('unorderable types: {}() < {}()'.format(self.__class__.__name__,
-                                                                    other.__class__.__name__))
+            raise TypeError(
+                'unorderable types: {}() < {}()'.format(
+                    self.__class__.__name__, other.__class__.__name__
+                )
+            )
 
         return self._value < self._extract_value(other)
 
@@ -221,11 +227,11 @@ class _UnsignedIntegerField(_IntegerField, _Field):
 
     @property
     def _value(self):
-        return native_bt.field_unsigned_integer_get_value(self._ptr)
+        return native_bt.field_integer_unsigned_get_value(self._ptr)
 
     def _set_value(self, value):
         value = self._value_to_int(value)
-        native_bt.field_unsigned_integer_set_value(self._ptr, value)
+        native_bt.field_integer_unsigned_set_value(self._ptr, value)
 
     value = property(fset=_set_value)
 
@@ -244,11 +250,11 @@ class _SignedIntegerField(_IntegerField, _Field):
 
     @property
     def _value(self):
-        return native_bt.field_signed_integer_get_value(self._ptr)
+        return native_bt.field_integer_signed_get_value(self._ptr)
 
     def _set_value(self, value):
         value = self._value_to_int(value)
-        native_bt.field_signed_integer_set_value(self._ptr, value)
+        native_bt.field_integer_signed_set_value(self._ptr, value)
 
     value = property(fset=_set_value)
 
@@ -280,8 +286,7 @@ class _EnumerationField(_IntegerField):
     @property
     def labels(self):
         status, labels = self._get_mapping_labels(self._ptr)
-        utils._handle_func_status(status,
-                                  "cannot get label for enumeration field")
+        utils._handle_func_status(status, "cannot get label for enumeration field")
 
         assert labels is not None
         return labels
@@ -289,12 +294,16 @@ class _EnumerationField(_IntegerField):
 
 class _UnsignedEnumerationField(_EnumerationField, _UnsignedIntegerField):
     _NAME = 'Unsigned Enumeration'
-    _get_mapping_labels = staticmethod(native_bt.field_unsigned_enumeration_get_mapping_labels)
+    _get_mapping_labels = staticmethod(
+        native_bt.field_enumeration_unsigned_get_mapping_labels
+    )
 
 
 class _SignedEnumerationField(_EnumerationField, _SignedIntegerField):
     _NAME = 'Signed Enumeration'
-    _get_mapping_labels = staticmethod(native_bt.field_signed_enumeration_get_mapping_labels)
+    _get_mapping_labels = staticmethod(
+        native_bt.field_enumeration_signed_get_mapping_labels
+    )
 
 
 @functools.total_ordering
@@ -347,8 +356,9 @@ class _StringField(_Field):
     def __iadd__(self, value):
         value = self._value_to_str(value)
         status = native_bt.field_string_append(self._ptr, value)
-        utils._handle_func_status(status,
-                                  "cannot append to string field object's value")
+        utils._handle_func_status(
+            status, "cannot append to string field object's value"
+        )
         return self
 
 
@@ -415,14 +425,16 @@ class _StructureField(_ContainerField, collections.abc.MutableMapping):
 
     def __getitem__(self, key):
         utils._check_str(key)
-        field_ptr = native_bt.field_structure_borrow_member_field_by_name(self._ptr, key)
+        field_ptr = native_bt.field_structure_borrow_member_field_by_name(
+            self._ptr, key
+        )
 
         if field_ptr is None:
             raise KeyError(key)
 
-        return _create_field_from_ptr(field_ptr, self._owner_ptr,
-                                      self._owner_get_ref,
-                                      self._owner_put_ref)
+        return _create_field_from_ptr(
+            field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref
+        )
 
     def member_at_index(self, index):
         utils._check_uint64(index)
@@ -430,11 +442,13 @@ class _StructureField(_ContainerField, collections.abc.MutableMapping):
         if index >= len(self):
             raise IndexError
 
-        field_ptr = native_bt.field_structure_borrow_member_field_by_index(self._ptr, index)
+        field_ptr = native_bt.field_structure_borrow_member_field_by_index(
+            self._ptr, index
+        )
         assert field_ptr is not None
-        return _create_field_from_ptr(field_ptr, self._owner_ptr,
-                                      self._owner_get_ref,
-                                      self._owner_put_ref)
+        return _create_field_from_ptr(
+            field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref
+        )
 
 
 class _VariantField(_ContainerField, _Field):
@@ -450,12 +464,14 @@ class _VariantField(_ContainerField, _Field):
 
     @property
     def selected_option(self):
+        # TODO: Is there a way to check if the variant field has a selected_option,
+        # so we can raise an exception instead of hitting a pre-condition check?
+        # If there is something, that check should be added to selected_option_index too.
         field_ptr = native_bt.field_variant_borrow_selected_option_field(self._ptr)
-        utils._handle_ptr(field_ptr, "cannot get variant field's selected option")
 
-        return _create_field_from_ptr(field_ptr, self._owner_ptr,
-                                      self._owner_get_ref,
-                                      self._owner_put_ref)
+        return _create_field_from_ptr(
+            field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref
+        )
 
     def _spec_eq(self, other):
         return _get_leaf_field(self) == other
@@ -483,18 +499,24 @@ class _ArrayField(_ContainerField, _Field, collections.abc.MutableSequence):
 
     def __getitem__(self, index):
         if not isinstance(index, numbers.Integral):
-            raise TypeError("'{}' is not an integral number object: invalid index".format(index.__class__.__name__))
+            raise TypeError(
+                "'{}' is not an integral number object: invalid index".format(
+                    index.__class__.__name__
+                )
+            )
 
         index = int(index)
 
         if index < 0 or index >= len(self):
             raise IndexError('{} field object index is out of range'.format(self._NAME))
 
-        field_ptr = native_bt.field_array_borrow_element_field_by_index(self._ptr, index)
-        assert(field_ptr)
-        return _create_field_from_ptr(field_ptr, self._owner_ptr,
-                                      self._owner_get_ref,
-                                      self._owner_put_ref)
+        field_ptr = native_bt.field_array_borrow_element_field_by_index(
+            self._ptr, index
+        )
+        assert field_ptr
+        return _create_field_from_ptr(
+            field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref
+        )
 
     def __setitem__(self, index, value):
         # raises if index is somehow invalid
@@ -536,8 +558,7 @@ class _StaticArrayField(_ArrayField, _Field):
 
     def _set_value(self, values):
         if len(self) != len(values):
-            raise ValueError(
-                'expected length of value and array field to match')
+            raise ValueError('expected length of value and array field to match')
 
         for index, value in enumerate(values):
             if value is not None:
@@ -554,7 +575,7 @@ class _DynamicArrayField(_ArrayField, _Field):
 
     def _set_length(self, length):
         utils._check_uint64(length)
-        status = native_bt.field_dynamic_array_set_length(self._ptr, length)
+        status = native_bt.field_array_dynamic_set_length(self._ptr, length)
         utils._handle_func_status(status, "cannot set dynamic array length")
 
     length = property(fget=_ArrayField._get_length, fset=_set_length)
This page took 0.02654 seconds and 4 git commands to generate.