kutsurak / python-bitstring

Automatically exported from code.google.com/p/python-bitstring
0 stars 0 forks source link

Bits created from BitArray is not immutable #144

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What's the problem? (If this is a feature request then feel free to delete
all the questions and just say what you want!)
According to the documentation at 
https://pythonhosted.org/bitstring/creation.html#the-auto-initialiser, the 
"auto" initialiser "can also be used to convert between the BitArray and Bits 
classes". However, this only appears to work in one direction. 

What is the expected output? What do you see instead?

Actual behavior (all but the last line are in the documentation linked above):
>>> immutable = Bits('0xabc')
>>> mutable = BitArray(immutable)
>>> mutable += '0xdef'
>>> immutable = Bits(mutable)
>>> set([immutable])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'BitArray'
>>> print type(immutable)
<class 'bitstring.BitArray'>

Expected behavior:
>>> immutable = Bits('0xabc')
>>> mutable = BitArray(immutable)
>>> mutable += '0xdef'
>>> immutable = Bits(mutable)
>>> set([immutable])
set([Bits('0xabcdef')])
>>> print type(immutable)
<class 'bitstring.Bits'>

Which version of bitstring are you using (bitstring.__version__), which
version of Python (e.g. 2.7, 3.2) and what platform
(Linux, Windows, etc.)

bitstring.__version__: '3.1.3'
sys.version: '2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit 
(Intel)]'
platform: Windows 8.1 x64

Please provide any additional information below.
Line 791 of bitstring.py contains:
"""
if isinstance(auto, Bits):
                return auto
"""

While this may be efficient when ``auto`` is an instance of Bits, the correct 
behavior is to fall through to lines 795-796 when ``auto`` is an instance of a 
subclass of Bits.
In the meantime I am working around this behavior using the following snippet:

>>> mutable=BitArray('0xabcdef')
>>> immutable=Bits()
>>> immutable._initialise(mutable,None,None)
>>> set([immutable])
set([Bits('0xabcdef')])

Original issue reported on code.google.com by david.ha...@gmail.com on 22 Sep 2014 at 1:17

GoogleCodeExporter commented 9 years ago
Hi, thanks for the bug report. Your diagnosis is probably correct and I'll get 
it fixed for the next release.

Original comment by dr.scott...@gmail.com on 22 Sep 2014 at 10:45