scott-griffiths / bitstring

A Python module to help you manage your bits
https://bitstring.readthedocs.io/en/stable/index.html
MIT License
401 stars 67 forks source link

Unpack decoding error #319

Closed RomoloPoliti-INAF closed 3 months ago

RomoloPoliti-INAF commented 3 months ago

I'm decoding a hex string: data= '00000000633c2042000d5bdf633c20450007a86a00000001034c00cc000000a8183803e8000003c3000028c100000000034c00000000000000000000' format_bit='bytes:2,uint:8,uint:6,uint:2,'+5'uint:32,'+2 'uint:16,'+'uint:32,'+2'uint:4,'+4'uint:1,'+2'uint:2,'+'uint:16,'+2'uint:32,'+8*'uint:16,' dp = BitStream(hex=data[0:120]).unpack(fmt=format_bit)

If I use bitstring version 4.1.4, the string will be decoded correctly.

If I use version 4.2.0 I obtain the error:


ValueError Traceback (most recent call last) File ~/Documents/Progetti/JANUS/Software/hk_getter/venv/lib/python3.12/site-packages/bitstring/bits.py:1186, in Bits._readlist(self, fmt, pos, kwargs) 1185 try: -> 1186 name, length = utils.parse_name_length_token(t, kwargs) 1187 except ValueError:

File ~/Documents/Progetti/JANUS/Software/hk_getter/venv/lib/python3.12/site-packages/bitstring/utils.py:94, in parse_name_length_token(fmt, **kwargs) 93 else: ---> 94 raise ValueError(f"Can't parse 'name[:]length' token '{fmt}'.") 95 return name, length

ValueError: Can't parse 'name[:]length' token ''.

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last) Cell In[63], line 1 ----> 1 dp = BitStream(hex=data[0:120]).unpack(fmt=format_bit)

File ~/Documents/Progetti/JANUS/Software/hk_getter/venv/lib/python3.12/site-packages/bitstring/bits.py:1169, in Bits.unpack(self, fmt, kwargs) 1154 def unpack(self, fmt: Union[str, List[Union[str, int]]], kwargs) -> List[Union[int, float, str, Bits, bool, bytes, None]]: 1155 """Interpret the whole bitstring using fmt and return list. 1156 1157 fmt -- A single string or a list of strings with comma separated tokens (...) 1167 1168 """ -> 1169 return self._readlist(fmt, 0, **kwargs)[0]

File ~/Documents/Progetti/JANUS/Software/hk_getter/venv/lib/python3.12/site-packages/bitstring/bits.py:1188, in Bits._readlist(self, fmt, pos, kwargs) 1186 name, length = utils.parse_name_length_token(t, kwargs) 1187 except ValueError: -> 1188 dtype_list.append(Dtype('bits', int(t))) 1189 else: 1190 dtype_list.append(Dtype(name, length))

ValueError: invalid literal for int() with base 10: ''


Thank you for your work and support

Romolo

scott-griffiths commented 3 months ago

Thank you for the bug report.

I believe the issue is that the format string ends in a comma, so the new logic is expecting another value that isn't there. It should be fairly simple to fix though, so I'll add it to my list for the 4.2.1 release.

Cheers.

scott-griffiths commented 3 months ago

This should be working again the 4.2.1 release.