This is a post about Python Construct Library
THE CODE
These are the definitions of my constructs:
from construct import *
AttributeHandleValuePair = "attribute_handle_value_pair" / Struct(
"handle" / Int16ul,
"value" / Bytes(this._.length - 2)
)
AttReadByTypeResponse = "read_by_type_response" / Struct(
"length" / Int8ul,
"attribute_data_list" / AttributeHandleValuePair[2]
)
PROBLEM
Attempting to execute the following command:
AttReadByTypeResponse.sizeof(dict(length=4, attribute_data_list=[dict(handle=1, value=2), dict(handle=3, value=4)])
I get the following error:
SizeofError: cannot calculate size, key not found in context
sizeof -> read_by_type_response -> attribute_data_list -> attribute_handle_value_pair -> value
WHAT I FOUND
The size of the field valuefor each attribute_handle_value_pairis inferred from the field of lengthits parent. I think the method sizeof()tries to calculate the size first attribute_handle_value_pair, while the field length read_by_type_responseis still undefined, so it cannot calculate its size.
I tried changing the length of the field valueto a static value and it worked fine.
MY QUESTION
How can I calculate sizeof()for a construct that depends on its parent construct?
? , ?