scott-griffiths / bitstring

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

An incorrect output and a crash in two examples from the REAMDE. #214

Closed eldipa closed 3 years ago

eldipa commented 3 years ago

I found two bugs in the README.

The first is a minor issue: the README shows one output but another is obtained:

      >>> a = BitArray('0x1af')
      >>> a.hex, a.bin, a.uint
      ('1af', '000110101111', 431)
      >>> a[10:3:-1].bin
      '1110101'
      >>> 3*a + '0b100'
      BitArray('0o0657056705674')

The value returned from the last expression is BitArray('0x1af1af1af, 0b100') instead of BitArray('0o0657056705674') as it is expected.

The other issue is more severe (also in the README):

     >>> c = BitArray('0b00010010010010001111')   # c.hex == '0x1248f'                                                         
     >>> c.find('0x48')                                                                                                        
     (8,)                                                                                                                      
     >>> c.replace('0b001', '0xabc')                                                                                           
     5                                                                                                                         
     >>> c.insert('0b0000')

The code fails with the following exception:

Traceback (most recent call last):
  File "/home/user/forks/bitstring/bitstring.py", line 3445, in insert
    pos = self._pos
AttributeError: 'BitArray' object has no attribute '_pos'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/forks/bitstring/bitstring.py", line 3447, in insert
    raise TypeError("insert require a bit position for this type.")
TypeError: insert require a bit position for this type

I tested these snippets with the latest version (3.1.7) and with the version of master (2643ac3a), both with Python 3.5.3.

For the record, I used byexample for testing:

$ byexample -l python README.rst

Disclaimer: I'm the author of byexample so I'm doing a little of marketing here. Nevertheless I tested the two snippets in a Python console and both issues exist.

scott-griffiths commented 3 years ago

Hi, thanks for letting me know about these. I should really have the docs examples all in the unit tests to catch these regressions.

Only documentation issues I think though (but that exception message could be clearer).

eldipa commented 3 years ago

Thanks! I'm glad to contributed even if it was just a little.

scott-griffiths commented 3 years ago

Fixed in 3.1-update branch.