How to use the bitstring.Error function in bitstring

To help you get started, we’ve selected a few bitstring examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github scott-griffiths / bitstring / test / test_bits.py View on Github external
def testCreationFromUintWithOffset(self):
        self.assertRaises(bitstring.Error, Bits, uint=12, length=8, offset=1)
github scott-griffiths / bitstring / bitstring / bits.py View on Github external
continue
                value, pos = self._readtoken(name, pos, length)
                lst.append(value)
            return lst, pos
        stretchy_token = False
        bits_after_stretchy_token = 0
        for token in tokens:
            name, length, _ = token
            if length in kwargs:
                length = kwargs[length]
            if name in kwargs and length is None:
                # Default 'uint'.
                length = kwargs[name]
            if stretchy_token:
                if name in ('se', 'ue', 'sie', 'uie'):
                    raise bitstring.Error("It's not possible to parse a variable"
                                "length token after a 'filler' token.")
                else:
                    bits_after_stretchy_token += length
            if length is None and name not in ('se', 'ue', 'sie', 'uie'):
                if stretchy_token:
                    raise bitstring.Error("It's not possible to have more than "
                                "one 'filler' token.")
                stretchy_token = token
        bits_left = self.len - pos
        return_values = []
        for token in tokens:
            name, length, _ = token
            if token is stretchy_token:
                # Set length to the remaining bits
                length = max(bits_left - bits_after_stretchy_token, 0)
            if length in kwargs:
github scott-griffiths / bitstring / bitstring / bits.py View on Github external
for token in tokens:
            name, length, _ = token
            if length in kwargs:
                length = kwargs[length]
            if name in kwargs and length is None:
                # Default 'uint'.
                length = kwargs[name]
            if stretchy_token:
                if name in ('se', 'ue', 'sie', 'uie'):
                    raise bitstring.Error("It's not possible to parse a variable"
                                "length token after a 'filler' token.")
                else:
                    bits_after_stretchy_token += length
            if length is None and name not in ('se', 'ue', 'sie', 'uie'):
                if stretchy_token:
                    raise bitstring.Error("It's not possible to have more than "
                                "one 'filler' token.")
                stretchy_token = token
        bits_left = self.len - pos
        return_values = []
        for token in tokens:
            name, length, _ = token
            if token is stretchy_token:
                # Set length to the remaining bits
                length = max(bits_left - bits_after_stretchy_token, 0)
            if length in kwargs:
                length = kwargs[length]
            if name in kwargs and length is None:
                # Default 'uint'
                length = kwargs[name]
            if length is not None:
                bits_left -= length
github RedisDesktop / rdm-native-value-formatters / python-binary / formatter.py View on Github external
def format(self, value):
        try:
            return bitstring.BitArray(value).bin
        except bitstring.Error as e:
            return self.process_error('Cannot format value: {}'.format(e))
github scott-griffiths / bitstring / bitstring / bitarray.py View on Github external
def ror(self, bits, start=None, end=None):
        """Rotate bits to the right in-place.

        bits -- The number of bits to rotate by.
        start -- Start of slice to rotate. Defaults to 0.
        end -- End of slice to rotate. Defaults to self.len.

        Raises ValueError if bits < 0.

        """
        if not self.len:
            raise bitstring.Error("Cannot rotate an empty bitstring.")
        if bits < 0:
            raise ValueError("Cannot rotate right by negative amount.")
        start, end = self._validate_slice(start, end)
        bits %= (end - start)
        if not bits:
            return
        rhs = self[end - bits:end]
        del self[end - bits:end]
        self.insert(rhs, start)
github scott-griffiths / bitstring / bitstring / bitarray.py View on Github external
def rol(self, bits, start=None, end=None):
        """Rotate bits to the left in-place.

        bits -- The number of bits to rotate by.
        start -- Start of slice to rotate. Defaults to 0.
        end -- End of slice to rotate. Defaults to self.len.

        Raises ValueError if bits < 0.

        """
        if not self.len:
            raise bitstring.Error("Cannot rotate an empty bitstring.")
        if bits < 0:
            raise ValueError("Cannot rotate left by negative amount.")
        start, end = self._validate_slice(start, end)
        bits %= (end - start)
        if not bits:
            return
        lhs = self[start:start + bits]
        del self[start:start + bits]
        self.insert(lhs, end - bits)