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
{{ message }}
Repository navigation
write/read roundtrip fails for certain data combinations #164
I was working the Synthetic Packets, Fixed Length example and the final statement result = pkt.load("test.bin") failed with RuntimeError: Packet definition larger than packet length by 1 bits (packet length in file is 16552 bits, defined fields are 16553 bits).
Debugging, I found that if I comment out the DATAFLAG lines, the example works fine.
If the DATAFLAG line is given bit_length=8, it works fine.
I reduced it further: if the packet definition only has the DATAFLAG (bit_length=1) type, it works, but if there are any other entries, it does not work. I put that into a test which currently fails, based on the example code, in #165. I have not figured out the source of the problem yet. Based on the error it seems the DATAFLAG PacketField is not getting written to the file, but I have not verified that.
I think this is because packets have to be an number of bits divisible by 8 (a clean number of bytes)-- and this error is not handled very well. The fact that it works with just DATAFLAG of bit_length=1 means it's probably adding unprompted padding. The reason it needs to be a clean number of bytes is minimally because the packet length in the header is in a number of bytes.
Does this make sense to you? I think we should (a) make the example work, possibly by adding a 7-bit fill field after the DATAFLAG so the example runs, and (b) maybe consider adding a better error message when the packet definition is not a clean number of bytes.
I have changed the synthetic packet documentation page to have bit_length=8 so the examples work. I didn't hear back from you, so I will assume this settles your concern.
Sorry for the delay, I was looking at some options. Setting the bit_length=8 in the DATAFLAG field makes the example work for me.
If you want to raise an error when a packet is defined that doesn't have multiples of 8 bits, you could do this in packet_types.py:
ifdescriptionisnotNoneandnotisinstance(description, str):
raiseTypeError("description must be a str")
total_bit_length=sum([f.bit_lengthforfinfields])
iftotal_bit_length%BITS_PER_BYTE:
raiseValueError(f"The sum of bit_length values must be a multiple of {BITS_PER_BYTE}. {total_bit_length} bits defined. Consider using fill bits.")
But that does cause a failure of test_nbytes_file_too_long where you have a 12-bit packet defined. Depends on how strict you want to be with that 8-bit-multiples rule.
I reworked the test in the now-closed PR to catch that error and to check the round-trip with fill bits added, and can open a new PR if you want.
Thanks for putting together a patch for to check this explicitly.
I think your snippet needs to be adjusted to account for the PacketArray class in how it computes the total bit length. I would recommend looping over fields and checking isinstance(field, PacketArray).
In test_nbytes_file_too_long we don't have a 12 bit packet defined, we have a 12 * 8 bit packet defined because it's an array of shape (8, 1), (and that is divisible by 8).
Activity
ddasilva commentedon Oct 15, 2026
Hi @d-lamb, thanks for reporting this !
I think this is because packets have to be an number of bits divisible by 8 (a clean number of bytes)-- and this error is not handled very well. The fact that it works with just DATAFLAG of bit_length=1 means it's probably adding unprompted padding. The reason it needs to be a clean number of bytes is minimally because the packet length in the header is in a number of bytes.
Does this make sense to you? I think we should (a) make the example work, possibly by adding a 7-bit fill field after the
DATAFLAGso the example runs, and (b) maybe consider adding a better error message when the packet definition is not a clean number of bytes.ddasilva commentedon Oct 20, 2026
I have changed the synthetic packet documentation page to have
bit_length=8so the examples work. I didn't hear back from you, so I will assume this settles your concern.-Daniel
d-lamb commentedon Oct 20, 2026
Sorry for the delay, I was looking at some options. Setting the
bit_length=8in the DATAFLAG field makes the example work for me.If you want to raise an error when a packet is defined that doesn't have multiples of 8 bits, you could do this in packet_types.py:
But that does cause a failure of test_nbytes_file_too_long where you have a 12-bit packet defined. Depends on how strict you want to be with that 8-bit-multiples rule.
I reworked the test in the now-closed PR to catch that error and to check the round-trip with fill bits added, and can open a new PR if you want.
ddasilva commentedon Oct 20, 2026
Thanks for putting together a patch for to check this explicitly.
I think your snippet needs to be adjusted to account for the
PacketArrayclass in how it computes the total bit length. I would recommend looping over fields and checkingisinstance(field, PacketArray).In
test_nbytes_file_too_longwe don't have a 12 bit packet defined, we have a 12 * 8 bit packet defined because it's an array of shape (8, 1), (and that is divisible by 8).