scott-griffiths / bitstring

A Python module to help you manage your bits
https://bitstring.readthedocs.io/en/stable/index.html
MIT License
412 stars 68 forks source link

Can't unpack in bitstring 3.1.8!! #222

Closed lgj2006 closed 3 years ago

lgj2006 commented 3 years ago
bits = Bits(pointer)
_len, _flags, _off = bits.unpack('uint:15, uint:2, uint')
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/bitstring.py", line 2098, in _readtoken
    val = name_to_read[name](self, length, pos)
KeyError: 'uint'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "xyz.py", line 127, in __parse_pointer
    _len, _flags, _off = bits.unpack('uint:15, uint:2, uint')
  File "/usr/local/lib/python3.6/site-packages/bitstring.py", line 2377, in unpack
    return self._readlist(fmt, 0, **kwargs)[0]
  File "/usr/local/lib/python3.6/site-packages/bitstring.py", line 2450, in _readlist
    value, pos = self._readtoken(name, pos, length)
  File "/usr/local/lib/python3.6/site-packages/bitstring.py", line 2103, in _readtoken
    raise ValueError("Can't parse token {0}:{1}".format(name, length))
ValueError: Can't parse token uint:15

Environment: python 3.6.8

And I find that bitstring.name_to_read is always {}.

emanuel-schmid commented 3 years ago

I encounter about the same with python 3.8. When running this example from read the docs, https://bitstring.readthedocs.io/en/latest/constbitstream.html#bitstring.ConstBitStream:

from bitstring import ConstBitStream
bs = ConstBitStream('0x123456')
bs.peek('hex:8')
KeyError                                  Traceback (most recent call last)
~\anaconda3\envs\climada_v2\lib\site-packages\bitstring.py in _readtoken(self, name, pos, length)
   2097         try:
-> 2098             val = name_to_read[name](self, length, pos)
   2099             return val, pos + length

KeyError: 'hex'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-28-18695a3677eb> in <module>
      1 from bitstring import ConstBitStream
      2 bs = ConstBitStream('0x123456')
----> 3 bs.peek('hex:8')

~\anaconda3\envs\climada_v2\lib\site-packages\bitstring.py in peek(self, fmt)
   4013         """
   4014         pos_before = self._pos
-> 4015         value = self.read(fmt)
   4016         self._pos = pos_before
   4017         return value

~\anaconda3\envs\climada_v2\lib\site-packages\bitstring.py in read(self, fmt)
   3950         if length is None:
   3951             length = self.len - self._pos
-> 3952         value, self._pos = self._readtoken(name, self._pos, length)
   3953         return value
   3954 

~\anaconda3\envs\climada_v2\lib\site-packages\bitstring.py in _readtoken(self, name, pos, length)
   2101             if name == 'pad':
   2102                 return None, pos + length
-> 2103             raise ValueError("Can't parse token {0}:{1}".format(name, length))
   2104         except TypeError:
   2105             # This is for the 'ue', 'se' and 'bool' tokens. They will also return the new pos.

ValueError: Can't parse token hex:8
scott-griffiths commented 3 years ago

Thanks. Not sure yet what's going on here as this should all be covered by the unit tests, but it's clearly broken. I've yanked the 3.1.8 release from PyPI and will look into it properly and prepare a new release. Suggest you pip install bitstring==3.1.7 for the time being...

emanuel-schmid commented 3 years ago

👍 Thank you!

scott-griffiths commented 3 years ago

Should all be fixed in 3.1.9. Strangely the unit tests were all passing for 3.1.8 despite it being obviously broken.