Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _setbytes_safe(self, data, length=None, offset=0):
"""Set the data from a string."""
data = bytearray(data)
if length is None:
# Use to the end of the data
length = (len(data) - (offset // 8)) * 8 - offset
self._datastore = ByteStore(data, length, offset)
else:
if length + offset > len(data) * 8:
msg = "Not enough data present. Need {0} bits, have {1}."
raise bitstring.CreationError(msg, length + offset, len(data) * 8)
if not length:
self._datastore = ByteStore(bytearray(0))
else:
self._datastore = ByteStore(data, length, offset)
raise bitstring.CreationError("The offset keyword isn't applicable to this initialiser.")
if isinstance(s, basestring):
bs = self._converttobitstring(s)
assert bs._offset == 0
self._setbytes_unsafe(bs._datastore.rawbytes, bs.length, 0)
return
if isinstance(s, (bytes, bytearray)):
self._setbytes_unsafe(bytearray(s), len(s) * 8, 0)
return
if isinstance(s, numbers.Integral):
# Initialise with s zero bits.
if s < 0:
msg = "Can't create bitstring of negative length {0}."
raise bitstring.CreationError(msg, s)
data = bytearray((s + 7) // 8)
self._datastore = ByteStore(data, s, 0)
return
if isinstance(s, collections.Iterable):
# Evaluate each item as True or False and set bits to 1 or 0.
self._setbin_unsafe(''.join(str(int(bool(x))) for x in s))
return
raise TypeError("Cannot initialise bitstring from {0}.".format(type(s)))
def _setbytes_safe(self, data, length=None, offset=0):
"""Set the data from a string."""
data = bytearray(data)
if length is None:
# Use to the end of the data
length = (len(data) - (offset // 8)) * 8 - offset
self._datastore = ByteStore(data, length, offset)
else:
if length + offset > len(data) * 8:
msg = "Not enough data present. Need {0} bits, have {1}."
raise bitstring.CreationError(msg, length + offset, len(data) * 8)
if not length:
self._datastore = ByteStore(bytearray(0))
else:
self._datastore = ByteStore(data, length, offset)
def testBaselineMemory(self):
try:
import pympler.asizeof.asizeof as size
except ImportError:
return
# These values might be platform dependent, so don't fret too much.
self.assertEqual(size(bitstring.ConstBitStream([0])), 64)
self.assertEqual(size(bitstring.Bits([0])), 64)
self.assertEqual(size(bitstring.BitStream([0])), 64)
self.assertEqual(size(bitstring.BitArray([0])), 64)
from bitstring.bitstore import ByteStore
self.assertEqual(size(ByteStore(bytearray())), 100)
def __copy__(self):
"""Return a new copy of the BitStream."""
s_copy = BitStream()
s_copy._pos = 0
if not isinstance(self._datastore, ByteStore):
# Let them both point to the same (invariant) array.
# If either gets modified then at that point they'll be read into memory.
s_copy._datastore = self._datastore
else:
s_copy._datastore = ByteStore(self._datastore._rawarray[:],
self._datastore.bitlength,
self._datastore.offset)
return s_copy
def offsetcopy(s, newoffset):
"""Return a copy of s with the newoffset."""
assert 0 <= newoffset < 8
if not s.bitlength:
return copy.copy(s)
else:
if newoffset == s.offset % 8:
return ByteStore(s.getbyteslice(0, s.bytelength), s.bitlength, newoffset)
newdata = []
d = s._rawarray
assert newoffset != s.offset % 8
if newoffset < s.offset % 8:
# We need to shift everything left
shiftleft = s.offset % 8 - newoffset
# First deal with everything except for the final byte
for x in range(s.byteoffset, s.byteoffset + s.bytelength - 1):
newdata.append(((d[x] << shiftleft) & 0xff) +\
(d[x + 1] >> (8 - shiftleft)))
bits_in_last_byte = (s.offset + s.bitlength) % 8
if not bits_in_last_byte:
bits_in_last_byte = 8
if bits_in_last_byte > shiftleft:
newdata.append((d[s.byteoffset + s.bytelength - 1] << shiftleft) & 0xff)
else: # newoffset > s._offset % 8
def _clear(self):
"""Reset the bitstring to an empty state."""
self._datastore = ByteStore(bytearray(0))
if not bits_in_last_byte:
bits_in_last_byte = 8
if bits_in_last_byte > shiftleft:
newdata.append((d[s.byteoffset + s.bytelength - 1] << shiftleft) & 0xff)
else: # newoffset > s._offset % 8
shiftright = newoffset - s.offset % 8
newdata.append(s.getbyte(0) >> shiftright)
for x in range(1, s.bytelength):
newdata.append(((d[x - 1] << (8 - shiftright)) & 0xff) +\
(d[x] >> shiftright))
bits_in_last_byte = (s.offset + s.bitlength) % 8
if not bits_in_last_byte:
bits_in_last_byte = 8
if bits_in_last_byte + shiftright > 8:
newdata.append((d[s.byteoffset + s.bytelength - 1] << (8 - shiftright)) & 0xff)
new_s = ByteStore(bytearray(newdata), s.bitlength, newoffset)
assert new_s.offset == newoffset
return new_s
def __copy__(self):
"""Return a new copy of the BitStream."""
s_copy = BitStream()
s_copy._pos = 0
if not isinstance(self._datastore, ByteStore):
# Let them both point to the same (invariant) array.
# If either gets modified then at that point they'll be read into memory.
s_copy._datastore = self._datastore
else:
s_copy._datastore = ByteStore(self._datastore._rawarray[:],
self._datastore.bitlength,
self._datastore.offset)
return s_copy