Skip to content

Commit

Permalink
Not mandating _filename be created for each Bits.
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-griffiths committed Feb 25, 2024
1 parent 197edb7 commit d9beb0d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
8 changes: 3 additions & 5 deletions bitstring/bits.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def __init__(self, auto: Optional[Union[BitsType, int]] = None, /, length: Optio
def __new__(cls: Type[TBits], auto: Optional[Union[BitsType, int]] = None, /, length: Optional[int] = None,
offset: Optional[int] = None, pos: Optional[int] = None, **kwargs) -> TBits:
x = super().__new__(cls)
x._filename = ''
if auto is None and not kwargs:
# No initialiser so fill with zero bits up to length
if length is not None:
Expand Down Expand Up @@ -242,9 +241,8 @@ def __getitem__(self: TBits, key: Union[slice, int], /) -> Union[TBits, bool]:
"""
if isinstance(key, numbers.Integral):
return bool(self._bitstore.getindex(key))
x = self._bitstore.getslice_withstep(key)
bs = self.__class__()
bs._bitstore = x
bs = super().__new__(self.__class__)
bs._bitstore = self._bitstore.getslice_withstep(key)
return bs

def __len__(self) -> int:
Expand Down Expand Up @@ -282,7 +280,7 @@ def __str__(self) -> str:

def _repr(self, classname: str, length: int, pos: int):
pos_string = f', pos={pos}' if pos else ''
if self._filename:
if hasattr(self, '_filename') and self._filename:
return f"{classname}(filename={self._filename!r}, length={length}{pos_string})"
else:
s = self.__str__()
Expand Down
2 changes: 2 additions & 0 deletions bitstring/bitstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def setall(self, value: int, /) -> None:
self._bitarray.setall(value)

def tobytes(self) -> bytes:
if self.modified_length is not None:
return self._bitarray[:self.modified_length].tobytes()
return self._bitarray.tobytes()

def slice_to_uint(self, start: Optional[int] = None, end: Optional[int] = None) -> int:
Expand Down
9 changes: 9 additions & 0 deletions bitstring/bitstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,15 @@ def fromstring(cls: TBits, s: str, /) -> TBits:
x._bitstore.immutable = True
return x

def __getitem__(self: TBits, key: Union[slice, int], /) -> Union[TBits, bool]:
"""Return a new bitstring representing a slice of the current bitstring."""
if isinstance(key, numbers.Integral):
return bool(self._bitstore.getindex(key))
bs = super().__new__(self.__class__)
bs._bitstore = self._bitstore.getslice_withstep(key)
bs._pos = 0
return bs

pos = property(_getbitpos, _setbitpos,
doc="""The position in the bitstring in bits. Read and write.
""")
Expand Down
3 changes: 3 additions & 0 deletions tests/test_bits.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ def test_bit_operators(self):
assert (x & self.c).hex == self.c.hex
assert self.c ^ self.b[4:20] == Bits(16)
assert self.a[23:36] | self.c[3:] == self.c[3:]
y = x & self.b[4:20]
assert y == self.c
assert repr(y) == repr(self.c)

def test_addition(self):
_ = self.d + '0x1'
Expand Down
2 changes: 0 additions & 2 deletions tests/test_bitstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -1949,8 +1949,6 @@ def test_repr(self):
ConstBitStream(filename=filename, length=17),
ConstBitStream(filename=filename, length=23, offset=23102)]:
f2 = eval(f.__repr__())
assert f._filename == f2._filename

assert f2.tobytes() == f.tobytes()
a = BitStream('0b1')
assert repr(a) == "BitStream('0b1')"
Expand Down

0 comments on commit d9beb0d

Please sign in to comment.