Skip to content

Commit

Permalink
Fix for AttributeError when combining Bits objects with BitStreams.
Browse files Browse the repository at this point in the history
Bug #329.
  • Loading branch information
scott-griffiths committed May 10, 2024
1 parent ce684cd commit f1a7e62
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bitstring/bits.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def __new__(cls: Type[TBits], auto: Optional[Union[BitsType, int]] = None, /, le

@classmethod
def _create_from_bitstype(cls: Type[TBits], auto: BitsType, /) -> TBits:
if isinstance(auto, Bits):
if isinstance(auto, cls):
return auto
b = super().__new__(cls)
b._setauto_no_length_or_offset(auto)
Expand Down
1 change: 1 addition & 0 deletions release_notes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ A couple more minor bug fixes.
* Initialising a bitstring from None now raises a TypeError rather than generating
an empty bitstring. Bug #323.
* Fixed performance regression for find/findall in some situations. Bug #326.
* Fix for AttributeError bug when combining Bits with BitStream. Bug #329.

-------------------------
April 2024: version 4.2.1
Expand Down
20 changes: 19 additions & 1 deletion tests/test_bitstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -4175,4 +4175,22 @@ def test_cache_with_length(self):
def test_unpack_error():
format_with_commas = ',bytes:2,,bytes:1,'
dp = BitStream(hex='010203').unpack(fmt=format_with_commas)
assert dp == [b'\x01\x02', b'\x03']
assert dp == [b'\x01\x02', b'\x03']


def test_add_pos_issue():
x = BitStream()
y = x + Bits('0xff')
assert x.pos == 0
assert y == '0xff'
z = x + bitstring.BitArray('0xff')
assert z == '0xff'
q = x + ConstBitStream('0xff')
assert q == '0xff'

xx = ConstBitStream()
yy = xx + Bits('0xff')
zz = xx + bitstring.BitArray('0xff')
qq = xx + BitStream('0xff')
assert yy == zz == qq == '0xff'

0 comments on commit f1a7e62

Please sign in to comment.