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.0: Bitarray(bytes=...) constructor no longer accepts list of bytes (or integers) #318

Closed EricGallimore closed 3 months ago

EricGallimore commented 3 months ago

In version prior to 4.2.0, this works:

list_of_bytes = [0x01, 0x02]
bits = BitArray(bytes=list_of_bytes)

In 4.2.0, a TypeError is thrown. Example:

Traceback (most recent call last):
  File "/home/eric/ros_workspace/src/ros_acomms/ros_acomms/src/message_queue_node.py", line 796, in handle_get_next_packet
    messages_in_packet, miniframe_bytes, dataframe_bytes = packet_codec.encode_payload(num_miniframe_bytes,
  File "/home/eric/ros_workspace/src/ros_acomms/ros_acomms/src/acomms_codecs/ros_packet_codec.py", line 64, in encode_payload
    miniframe_bits = BitArray(bytes=self.miniframe_header)
  File "/home/eric/.local/lib/python3.8/site-packages/bitstring/bits.py", line 124, in __new__
    x._initialise(auto, length, offset, **kwargs)
  File "/home/eric/.local/lib/python3.8/site-packages/bitstring/bits.py", line 149, in _initialise
    self._setbytes_with_truncation(v, length, offset)
  File "/home/eric/.local/lib/python3.8/site-packages/bitstring/bits.py", line 622, in _setbytes_with_truncation
    return self._setbytes(data)
  File "/home/eric/.local/lib/python3.8/site-packages/bitstring/bits.py", line 616, in _setbytes
    self._bitstore = BitStore.frombytes(data)
  File "/home/eric/.local/lib/python3.8/site-packages/bitstring/bitstore.py", line 41, in frombytes
    x._bitarray.frombytes(b)
TypeError: a bytes-like object is required, not 'list'

(Converting the list of bytes/ints to bytes by calling bytes(list_of_bytes) fixes this, but this is still a change in 4.2.0 that breaks backward compatibility.)

scott-griffiths commented 3 months ago

Hi. Thanks for the bug report.

You're right, that was an unintended side effect of some refactoring. I've fixed it and added a test.

Good timing - just in time for the 4.2.1 release! Thanks again.

scott-griffiths commented 3 months ago

This should be working again the 4.2.1 release.