Skip to content

Commit f1a7e62

Browse files
Fix for AttributeError when combining Bits objects with BitStreams.
Bug #329.
1 parent ce684cd commit f1a7e62

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

bitstring/bits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def __new__(cls: Type[TBits], auto: Optional[Union[BitsType, int]] = None, /, le
126126

127127
@classmethod
128128
def _create_from_bitstype(cls: Type[TBits], auto: BitsType, /) -> TBits:
129-
if isinstance(auto, Bits):
129+
if isinstance(auto, cls):
130130
return auto
131131
b = super().__new__(cls)
132132
b._setauto_no_length_or_offset(auto)

release_notes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ A couple more minor bug fixes.
1111
* Initialising a bitstring from None now raises a TypeError rather than generating
1212
an empty bitstring. Bug #323.
1313
* Fixed performance regression for find/findall in some situations. Bug #326.
14+
* Fix for AttributeError bug when combining Bits with BitStream. Bug #329.
1415

1516
-------------------------
1617
April 2024: version 4.2.1

tests/test_bitstream.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4175,4 +4175,22 @@ def test_cache_with_length(self):
41754175
def test_unpack_error():
41764176
format_with_commas = ',bytes:2,,bytes:1,'
41774177
dp = BitStream(hex='010203').unpack(fmt=format_with_commas)
4178-
assert dp == [b'\x01\x02', b'\x03']
4178+
assert dp == [b'\x01\x02', b'\x03']
4179+
4180+
4181+
def test_add_pos_issue():
4182+
x = BitStream()
4183+
y = x + Bits('0xff')
4184+
assert x.pos == 0
4185+
assert y == '0xff'
4186+
z = x + bitstring.BitArray('0xff')
4187+
assert z == '0xff'
4188+
q = x + ConstBitStream('0xff')
4189+
assert q == '0xff'
4190+
4191+
xx = ConstBitStream()
4192+
yy = xx + Bits('0xff')
4193+
zz = xx + bitstring.BitArray('0xff')
4194+
qq = xx + BitStream('0xff')
4195+
assert yy == zz == qq == '0xff'
4196+

0 commit comments

Comments
 (0)