+ def __init__(self, definition_ptr, scope):
+ self._d = definition_ptr
+ self._s = scope
+ if not scope in _scopes:
+ ValueError("Invalid scope provided")
+
+ @property
+ def name(self):
+ """Return the name of a field or None on error."""
+ return _bt_ctf_field_name(self._d)
+
+ @property
+ def type(self):
+ """Return the type of a field or -1 if unknown."""
+ return _bt_ctf_field_type(_bt_ctf_get_decl_from_def(self._d))
+
+ @property
+ def declaration(self):
+ """Return the associated Definition object."""
+ return _create_field_declaration(
+ _bt_ctf_get_decl_from_def(self._d), self.name, self.scope)
+
+ def _get_enum_str(self):
+ """
+ Return the string matching the current enumeration.
+ Return None on error.
+ """
+ return _bt_ctf_get_enum_str(self._d)
+
+ def _get_array_element_at(self, index):
+ """
+ Return the array's element at position index.
+ Return None on error
+ """
+ array_ptr = _bt_python_get_array_from_def(self._d)
+ if array_ptr is None:
+ return None
+
+ definition_ptr = _bt_array_index(array_ptr, index)
+ if definition_ptr is None:
+ return None
+ return _Definition(definition_ptr, self.scope)
+
+ def _get_sequence_len(self):
+ """
+ Return the len of a sequence or a negative
+ value on error.
+ """
+ seq = _bt_python_get_sequence_from_def(self._d)
+ return _bt_sequence_len(seq)
+
+ def _get_sequence_element_at(self, index):
+ """
+ Return the sequence's element at position index,
+ otherwise return None
+ """
+ seq = _bt_python_get_sequence_from_def(self._d)
+ if seq is not None:
+ definition_ptr = _bt_sequence_index(seq, index)
+ if definition_ptr is not None:
+ return _Definition(definition_ptr, self.scope)
+ return None
+
+ def _get_uint64(self):
+ """
+ Return the value associated with the field.
+ If the field does not exist or is not of the type requested,
+ the value returned is undefined. To check if an error occured,
+ use the field_error() function after accessing a field.
+ """
+ return _bt_ctf_get_uint64(self._d)
+
+ def _get_int64(self):
+ """
+ Return the value associated with the field.
+ If the field does not exist or is not of the type requested,
+ the value returned is undefined. To check if an error occured,
+ use the field_error() function after accessing a field.
+ """
+ return _bt_ctf_get_int64(self._d)
+
+ def _get_char_array(self):
+ """
+ Return the value associated with the field.
+ If the field does not exist or is not of the type requested,
+ the value returned is undefined. To check if an error occurred,
+ use the field_error() function after accessing a field.
+ """
+ return _bt_ctf_get_char_array(self._d)
+
+ def _get_str(self):
+ """
+ Return the value associated with the field.
+ If the field does not exist or is not of the type requested,
+ the value returned is undefined. To check if an error occurred,
+ use the field_error() function after accessing a field.
+ """
+ return _bt_ctf_get_string(self._d)
+
+ def _get_float(self):
+ """
+ Return the value associated with the field.
+ If the field does not exist or is not of the type requested,
+ the value returned is undefined. To check if an error occurred,
+ use the field_error() function after accessing a field.
+ """
+ return _bt_ctf_get_float(self._d)
+
+ def _get_variant(self):
+ """
+ Return the variant's selected field.
+ If the field does not exist or is not of the type requested,
+ the value returned is undefined. To check if an error occurred,
+ use the field_error() function after accessing a field.
+ """
+ return _bt_ctf_get_variant(self._d)
+
+ def _get_struct_field_count(self):
+ """
+ Return the number of fields contained in the structure.
+ If the field does not exist or is not of the type requested,
+ the value returned is undefined.
+ """
+ return _bt_ctf_get_struct_field_count(self._d)
+
+ def _get_struct_field_at(self, i):
+ """
+ Return the structure's field at position i.
+ If the field does not exist or is not of the type requested,
+ the value returned is undefined. To check if an error occurred,
+ use the field_error() function after accessing a field.
+ """
+ return _bt_ctf_get_struct_field_index(self._d, i)
+
+ @property
+ def value(self):
+ """
+ Return the value associated with the field according to its type.
+ Return None on error.
+ """
+ id = self.type
+ value = None
+
+ if id == CTFTypeId.STRING:
+ value = self._get_str()
+ elif id == CTFTypeId.ARRAY:
+ element_decl = self.declaration.element_declaration
+ if ((element_decl.type == CTFTypeId.INTEGER
+ and element_decl.length == 8)
+ and (element_decl.encoding == CTFStringEncoding.ASCII or element_decl.encoding == CTFStringEncoding.UTF8)):
+ value = _bt_python_get_array_string(self._d)
+ else:
+ value = []
+ for i in range(self.declaration.length):
+ element = self._get_array_element_at(i)
+ value.append(element.value)
+ elif id == CTFTypeId.INTEGER:
+ if self.declaration.signedness == 0:
+ value = self._get_uint64()
+ else:
+ value = self._get_int64()
+ elif id == CTFTypeId.ENUM:
+ value = self._get_enum_str()
+ elif id == CTFTypeId.SEQUENCE:
+ element_decl = self.declaration.element_declaration
+ if ((element_decl.type == CTFTypeId.INTEGER
+ and element_decl.length == 8)
+ and (element_decl.encoding == CTFStringEncoding.ASCII or element_decl.encoding == CTFStringEncoding.UTF8)):
+ value = _bt_python_get_sequence_string(self._d)
+ else:
+ seq_len = self._get_sequence_len()
+ value = []
+ for i in range(seq_len):
+ evDef = self._get_sequence_element_at(i)
+ value.append(evDef.value)
+ elif id == CTFTypeId.FLOAT:
+ value = self._get_float()
+ elif id == CTFTypeId.VARIANT:
+ variant = _Definition.__new__(_Definition)
+ variant._d = self._get_variant()
+ value = variant.value
+ elif id == CTFTypeId.STRUCT:
+ value = {}
+ for i in range(self._get_struct_field_count()):
+ member = _Definition(self._get_struct_field_at(i), self.scope)
+ value[member.name] = member.value
+
+ if field_error():
+ raise FieldError(
+ "Error occurred while accessing field {} of type {}".format(
+ self.name,
+ CTFTypeId.type_name(id)))
+ return value
+
+ @property
+ def scope(self):
+ """Return the scope of a field or None on error."""
+ return self._s