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

4.2.x: Reading past the end of stream no longer raises ReadError #325

Closed EricGallimore closed 2 months ago

EricGallimore commented 3 months ago

In earlier versions, reading past the end of a BitStream raised a ReadError.

Now, it seems that it raises a ValueError. Example:

  File "/home/eric/ros_workspace/src/ros_acomms/ros_acomms/src/acomms_codecs/ros_packet_codec.py", line 147, in decode_payload
    id = payload_bits.read('uint:8')
  File "/home/eric/.local/lib/python3.8/site-packages/bitstring/bitstream.py", line 349, in read
    val = dtype.read_fn(self, self._pos)
  File "/home/eric/.local/lib/python3.8/site-packages/bitstring/dtypes.py", line 297, in read_fn
    return self.get_fn(bs[start:start + length])
  File "/home/eric/.local/lib/python3.8/site-packages/bitstring/dtypes.py", line 285, in allowed_length_checked_get_fn
    return get_fn(bs)
  File "/home/eric/.local/lib/python3.8/site-packages/bitstring/bits.py", line 661, in _getuint
    raise bitstring.InterpretError("Cannot interpret a zero length bitstring as an integer.")
ValueError: Cannot interpret a zero length bitstring as an integer.

From the documentation, ReadError is the intended behavior:

Reads from current bit position pos in the bitstring according the format string and returns a single result. If not enough bits are available then a ReadError is raised.

scott-griffiths commented 3 months ago

Hi, thanks for the bug report.

Yes that looks like an unintended change. I'll fix it in the next point release.

Thanks again.

scott-griffiths commented 2 months ago

bitstring 4.2.2 is now released. 🎉