You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would expect to be able to have a conditional SubstructureField, but that doesn't seem to actually work.
Example:
fromsuitcase.structureimportStructurefromsuitcase.fieldsimportSubstructureField, ConditionalField, UBInt8classSubstruct1(Structure):
cond=UBInt8()
classSubstruct2(Structure):
f1=UBInt8()
f2=UBInt8()
classSubstruct3(Structure):
f31=UBInt8()
f32=UBInt8()
classStruct(Structure):
header=SubstructureField(Substruct1)
s2=ConditionalField(SubstructureField(Substruct2),
condition=lambdam: m.header.cond==0)
s3=ConditionalField(SubstructureField(Substruct3),
condition=lambdam: m.header.cond==0)
print("If the field(s) are not present...")
print(Struct.from_data('\x01'))
print("\nIf the field(s) are present...")
print(Struct.from_data('\x00\x11\x22\x33\x44'))
Output:
If the field(s) are not present...
Struct (
header=Substruct1 (
cond=1,
),
s2=<ConditionalField: not included>,
s3=<ConditionalField: not included>,
)
If the field(s) are present...
Traceback (most recent call last):
File "condsubstruct.py", line 31, in <module>
print(Struct.from_data('\x00\x11\x22\x33\x44'))
File "/home/mike/.envs/suitcase/lib/python2.7/site-packages/suitcase/structure.py", line 247, in from_data
m.unpack(data)
File "/home/mike/.envs/suitcase/lib/python2.7/site-packages/suitcase/structure.py", line 300, in unpack
return self._packer.unpack(data, trailing)
File "/home/mike/.envs/suitcase/lib/python2.7/site-packages/suitcase/structure.py", line 61, in unpack
self.unpack_stream(stream)
File "/home/mike/.envs/suitcase/lib/python2.7/site-packages/suitcase/structure.py", line 138, in unpack_stream
(name, length, len(data)))
suitcase.exceptions.SuitcaseParseError: While attempting to parse field 's2' we tried to read None bytes but we were only able to read 4.
If I had to guess, it might have something to do with SubstructureField returning None from bytes_required. But I'm not sure. I might dig into this more later tonight.
The text was updated successfully, but these errors were encountered:
I did some more digging, and it looks like the problem is that Packer doesn't see ConditionalField as equivalent to s SubstructureField, and so it assumes it to be greedy.
I have a working fix, but haven't written the unit tests for it yet.
mikewadsten
added a commit
to mikewadsten/python-suitcase
that referenced
this issue
Nov 19, 2015
I would expect to be able to have a conditional SubstructureField, but that doesn't seem to actually work.
Example:
Output:
If I had to guess, it might have something to do with
SubstructureField
returningNone
frombytes_required
. But I'm not sure. I might dig into this more later tonight.The text was updated successfully, but these errors were encountered: