kutsurak / python-bitstring

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

Using a numpy.int32 to initialise a Bits object stalls #83

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
When feeding a numpy.int32 to create a Bits object, bitstring stalls. Using 
__version_=1.3.0, Mac 
OS X 10.5.8, Python 2.6.4. See transcript below.

Python 2.6.4 (r264:75706, Oct 30 2009, 10:12:38) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from bitstring import Bits
>>> a = 5
>>> b = Bits(uint=a,length=3)
>>> b
Bits('0b101')
>>> import numpy
>>> c = numpy.uint32(5)
>>> c
5
>>> b = Bits(uint=c,length=3)
... wait indefinitely

Original issue reported on code.google.com by s.t.optl...@gmail.com on 22 Mar 2010 at 12:27

GoogleCodeExporter commented 9 years ago
Thanks for the report.

The short answer is that numpy objects aren't supported (and I've never tested 
them)
so it's not strictly a bug if they don't work.

It's not unreasonable to expect them to work though, so the slightly longer 
answer is
that the numpy integer isn't behaving like a plain int and so causes an infinite
loop. In particular:

>>> numpy.uint32(5) >> 64
5

which clearly isn't correct!:

>>> 5 >> 64
0

This is probably a C-level issue in numpy (shifting a 32-bit integer by 64 bits
doesn't have a defined behaviour in C).

I'll leave it open as a bug for now though until I decide if it makes sense to
explicitly support numpy types (or at least raise an exception rather than 
hang!)

The workaround of course is just to cast to an int:

>>> b = Bits(uint=int(c), length=3)

Original comment by python.bitstring@googlemail.com on 22 Mar 2010 at 12:55

GoogleCodeExporter commented 9 years ago
Agreed, I already use the cast workaround.

It reasonable to not support numpy types, but I think it is wise to raise an 
exception meanwhile. I spent quite 
some time to find out that my code hung because I used a numpy type instead of 
an int ;)

For the rest, bitstring is brilliant!

Original comment by s.t.optl...@gmail.com on 22 Mar 2010 at 1:05

GoogleCodeExporter commented 9 years ago
Thanks for the kind comment. Having reviewed the problem I think that I might 
as well
support the numpy type in this (although not officially). The problem is caused 
by a
rather over-zealous optimisation of mine which I can just revert.

Original comment by python.bitstring@googlemail.com on 24 Mar 2010 at 8:50

GoogleCodeExporter commented 9 years ago
Fixed in revision 680.

Original comment by python.bitstring@googlemail.com on 25 Mar 2010 at 10:14