class FieldInstance(_Generic[_ValueType]):
A field instance, the result of reading a field from a StructInstance
.
You can obtain the value of the field by calling the FieldInstance.read
method. The returned value will be of the natural type produced by the type of the field.
>>> value = struct_instance.field("Length").read() >>> value 48
If you are using a type checker such as mypy or an IDE, you may want to annotate the produced type:
>>> value : int = struct_instance.field("Length").read()
However, the annotation will silently be wrong should you mistake the value produced by the field.
FieldInstance
also provides the FieldInstance.assert_int
, FieldInstance.assert_float
, etc. methods to check the produced type at runtime, raising if there is a mistake, and allowing to drop the type annotation on the left side:
>>> value = struct_instance.field("Length").assert_int().read()
The FieldInstance.read_int
, FieldInstance.read_float
, etc. methods are a shortcut equivalent to calling assert followed by read.
>>> value = struct_instance.field("Length").read_int()
You can also always get the value of the field as bytes with the FieldInstance.read_bytes
method.
When the field is of pointer type, the FieldInstance.deref
family of functions allows to produce the pointed value without explicitly going through the intermediate PointerInstance
.
Lastly, get the information about the field in its structure (name, type, offset) with the FieldInstance.info
property, or link back to the StructInstance
of origin with FieldInstance.parent_instance
.
Method | __eq__ |
Compares the instance for equality with an object. |
Method | __format__ |
Formats the value according to the specified options. |
Method | __hash__ |
Returns the hash for this value. |
Method | __init__ |
Initializes a new field instance from the struct instance of origin and the selected field. |
Method | __ne__ |
Compares the instance for equality with an object. |
Method | __str__ |
Returns the nicely printable string representation of this instance. |
Method | assert |
Asserts to the type system that this field produces an array value. |
Method | assert |
Asserts to the type system that this field produces a boolean value. |
Method | assert |
Asserts to the type system that this field produces a EnumerationInstance value. |
Method | assert |
Asserts to the type system that this field produces a float value. |
Method | assert |
Asserts to the type system that this field produces an integer value. |
Method | assert |
Asserts to the type system that this field produces a pointer value. |
Method | assert |
Asserts to the type system that this field produces a StructInstance value. |
Method | deref |
If this field produces a pointer, dereferences the field and get the value pointed to by the pointer. |
Method | deref |
Dereferences the value of this field after asserting to the type system that this field produces a pointer to an array value. |
Method | deref |
Dereferences the value of this field after asserting to the type system that this field produces a pointer to a boolean value. |
Method | deref |
If this field produces a pointer, dereferences the field to get the value pointed to by the pointer, interpreted as bytes. |
Method | deref |
Dereferences the value of this field after asserting to the type system that this field produces a pointer to an enum value. |
Method | deref |
Dereferences the value of this field after asserting to the type system that this field produces a pointer to a float value. |
Method | deref |
Dereferences the value of this field after asserting to the type system that this field produces a pointer to an integer value. |
Method | deref |
Dereferences the value of this field after asserting to the type system that this field produces a pointer to a pointer value. |
Method | deref |
If this field produces a pointer, dereferences the field to get the value pointed to by the pointed, interpreted as a null-terminated string. |
Method | deref |
Dereferences the value of this field after asserting to the type system that this field produces a pointer to a struct value. |
Method | format |
Undocumented |
Method | read |
Read the value of the field. |
Method | read |
Reads the value of this field after asserting to the type system that this field produces an array value. |
Method | read |
Reads the value of this field after asserting to the type system that this field produces a boolean value. |
Method | read |
Read the value of this field as bytes. |
Method | read |
Reads the value of this field after asserting to the type system that this field produces an enum value. |
Method | read |
Reads the value of this field after asserting to the type system that this field produces a float value. |
Method | read |
Reads the value of this field after asserting to the type system that this field produces an integer value. |
Method | read |
Reads the value of this field after asserting to the type system that this field produces a pointer value. |
Method | read |
Reads the value of this field after asserting to the type system that this field produces a struct value. |
Class Variable |
|
Undocumented |
Class Variable |
|
Undocumented |
Property | info |
The information (offset, name) about the field associated with this instance. |
Property | parent |
The struct instance that this instance originates from. |
Instance Variable | _field |
Undocumented |
Instance Variable | _struct |
Undocumented |
Compares the instance for equality with an object.
- if the object is of type bytes, it will be considered equal to this instance if it is equal to the underlying bytes that this instance is made of.
- two
FieldInstance
s are considered equal if they have the same underlyingStruct
,Field
and bytes. - if the object is neither bytes nor
FieldInstance
, then it will never be considered equal.
Note
- The context where this struct was spawned at is never taken into consideration for the purpose of equality. This means that two
FieldInstance
s at different contexts with the same type and bytes will be considered equal. - Similarly, the source from which the instance was read is never taken into consideration for the purpose of equality. This means that two
FieldInstance
s coming from different addresses in memory or different registers can compare equal if they have the same type and bytes.
Parameters | |
o:object | Undocumented |
Returns | |
bool | Undocumented |
Formats the value according to the specified options.
The options are expected to be in the format described by reven2.util.parse_colon_separated_key_values
.
The options will be passed as argument to format
.
See StructInstance.__format__
for examples.
Parameters | |
formatstr | Undocumented |
Returns | |
str | Undocumented |
Initializes a new field instance from the struct instance of origin and the selected field.
Warnings
At the moment, this object is not meant to be constructed directly. Use StructInstance.field
instead.
Parameters | |
structStructInstance | Undocumented |
field:RegularField | Undocumented |
Compares the instance for equality with an object.
- if the object is of type bytes, it will be considered equal to this instance if it is equal to the underlying bytes that this instance is made of.
- two
FieldInstance
s are considered equal if they have the same underlyingStruct
,Field
and bytes. - if the object is neither bytes nor
FieldInstance
, then it will never be considered equal.
Note
- The context where this struct was spawned at is never taken into consideration for the purpose of equality. This means that two
FieldInstance
s at different contexts with the same type and bytes will be considered equal. - Similarly, the source from which the instance was read is never taken into consideration for the purpose of equality. This means that two
FieldInstance
s coming from different addresses in memory or different registers can compare equal if they have the same type and bytes.
Parameters | |
o:object | Undocumented |
Returns | |
bool | Undocumented |
Returns the nicely printable string representation of this instance.
Returns | |
str | Undocumented |
Asserts to the type system that this field produces an array value.
Information
Returns | |
FieldInstance[ | this field instance, with the additional typing information that its produced value is an array. |
Raises | |
ValueError | if, according to its runtime type, this field does not produce an array value. |
Asserts to the type system that this field produces a boolean value.
Information
Returns | |
FieldInstance[ | this field instance, with the additional typing information that its produced value is a bool. |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a boolean value. |
Asserts to the type system that this field produces a EnumerationInstance
value.
Information
Returns | |
FieldInstance[ | this field instance, with the additional typing information that its produced value is an enumeration. |
Raises | |
ValueError | if, according to its runtime type, this field does not produce an enumeration value. |
Asserts to the type system that this field produces a float value.
Information
Returns | |
FieldInstance[ | this field instance, with the additional typing information that its produced value is a float. |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a float value. |
Asserts to the type system that this field produces an integer value.
Information
Returns | |
FieldInstance[ | this field instance, with the additional typing information that its produced value is an int. |
Raises | |
ValueError | if, according to its runtime type, this field does not produce an integer value. |
Asserts to the type system that this field produces a pointer value.
Information
Returns | |
FieldInstance[ | this field instance, with the additional typing information that its produced value is a pointer. |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a pointer value. |
Asserts to the type system that this field produces a StructInstance
value.
Information
Returns | |
FieldInstance[ | this field instance, with the additional typing information that its produced value is a struct. |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a struct value. |
If this field produces a pointer, dereferences the field and get the value pointed to by the pointer.
Information
Returns | |
SubPointerValueType | the value pointed to by this field, of the type that is produced by the type pointed to by the pointer. |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a pointer value. |
Dereferences the value of this field after asserting to the type system that this field produces a pointer to an array value.
Calling this method is equivalent to self.assert_ptr().read().assert_array().deref().
Information
Returns | |
ArrayInstance | the same value as returned by FieldInstance.deref , with the additional typing information that the value is an ArrayInstance . |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a pointer, or does not produce a pointer to an array value. |
Dereferences the value of this field after asserting to the type system that this field produces a pointer to a boolean value.
Calling this method is equivalent to self.assert_ptr().read().assert_bool().deref().
Information
Returns | |
bool | the same value as returned by FieldInstance.deref , with the additional typing information that the value is a bool. |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a pointer, or does not produce a pointer to a boolean value. |
If this field produces a pointer, dereferences the field to get the value pointed to by the pointer, interpreted as bytes.
Information
Returns | |
bytes | the value pointed to by this field, as bytes. |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a pointer value. |
Dereferences the value of this field after asserting to the type system that this field produces a pointer to an enum value.
Calling this method is equivalent to self.assert_ptr().read().assert_enum().deref().
Information
Returns | |
EnumerationInstance | the same value as returned by FieldInstance.deref , with the additional typing information that the value is an EnumerationInstance . |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a pointer, or does not produce a pointer to an enum value. |
Dereferences the value of this field after asserting to the type system that this field produces a pointer to a float value.
Calling this method is equivalent to self.assert_ptr().read().assert_float().deref().
Information
Returns | |
float | the same value as returned by FieldInstance.deref , with the additional typing information that the value is a float. |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a pointer, or does not produce a pointer to a float value. |
Dereferences the value of this field after asserting to the type system that this field produces a pointer to an integer value.
Calling this method is equivalent to self.assert_ptr().read().assert_int().deref().
Information
Returns | |
int | the same value as returned by FieldInstance.deref , with the additional typing information that the value is an int. |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a pointer, or does not produce a pointer to an integer value. |
Dereferences the value of this field after asserting to the type system that this field produces a pointer to a pointer value.
Calling this method is equivalent to self.assert_ptr().read().assert_ptr().deref().
Information
Returns | |
PointerInstance | the same value as returned by FieldInstance.deref , with the additional typing information that the value is a PointerInstance . |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a pointer, or does not produce a pointer to a pointer value. |
If this field produces a pointer, dereferences the field to get the value pointed to by the pointed, interpreted as a null-terminated string.
Information
Parameters | |
cstring:_CString | allows to specify the encoding and maximum size of the string to read. |
Returns | |
str | the value pointed to by this field, as a string. |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a pointer value. |
EncodingError | if the bytes pointed to by the field contain errors wrt to the specified encoding. |
Dereferences the value of this field after asserting to the type system that this field produces a pointer to a struct value.
Calling this method is equivalent to self.assert_ptr().read().assert_struct().deref().
Information
Returns | |
StructInstance | the same value as returned by FieldInstance.deref , with the additional typing information that the value is a StructInstance . |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a pointer, or does not produce a pointer to a struct value. |
Undocumented
Parameters | |
maxint | Undocumented |
currentint | Undocumented |
Returns | |
str | Undocumented |
Read the value of the field.
Warning
Unlike what happens in Context.read
, reading a pointer will return a PointerInstance
rather than an address.
Information
Returns | |
_ValueType | the value of this field, of the type that is produced by the type of this field. |
Reads the value of this field after asserting to the type system that this field produces an array value.
Calling this method is equivalent to self.assert_array().read().
Information
Returns | |
ArrayInstance | the same value as returned by FieldInstance.read , with the additional typing information that the value is an ArrayInstance . |
Raises | |
ValueError | if, according to its runtime type, this field does not produce an array value. |
Reads the value of this field after asserting to the type system that this field produces a boolean value.
Calling this method is equivalent to self.assert_bool().read().
Information
Returns | |
bool | the same value as returned by FieldInstance.read , with the additional typing information that the value is a bool. |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a boolean value. |
Read the value of this field as bytes.
Information
Returns | |
bytes | a buffer of bytes of size equal to the size of the type of this field. |
Reads the value of this field after asserting to the type system that this field produces an enum value.
Calling this method is equivalent to self.assert_enum().read().
Information
Returns | |
EnumerationInstance | the same value as returned by FieldInstance.read , with the additional typing information that the value is a EnumerationInstance . |
Raises | |
ValueError | if, according to its runtime type, this field does not produce an enum value. |
Reads the value of this field after asserting to the type system that this field produces a float value.
Calling this method is equivalent to self.assert_float().read().
Information
Returns | |
float | the same value as returned by FieldInstance.read , with the additional typing information that the value is a float. |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a float value. |
Reads the value of this field after asserting to the type system that this field produces an integer value.
Calling this method is equivalent to self.assert_int().read().
Information
Returns | |
int | the same value as returned by FieldInstance.read , with the additional typing information that the value is a int. |
Raises | |
ValueError | if, according to its runtime type, this field does not produce an integer value. |
Reads the value of this field after asserting to the type system that this field produces a pointer value.
Calling this method is equivalent to self.assert_ptr().read().
Information
Returns | |
PointerInstance | the same value as returned by FieldInstance.read , with the additional typing information that the value is a PointerInstance . |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a pointer value. |
Reads the value of this field after asserting to the type system that this field produces a struct value.
Calling this method is equivalent to self.assert_struct().read().
Information
Returns | |
StructInstance | the same value as returned by FieldInstance.read , with the additional typing information that the value is a StructInstance . |
Raises | |
ValueError | if, according to its runtime type, this field does not produce a struct value. |