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

4.2.0 crashes upon import with 32 bit python versions #317

Closed daxie117 closed 7 months ago

daxie117 commented 7 months ago

We are running into an error upon import, error trace pasted below:

import bitstring
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\<path>\python\3117\test\Lib\site-packages\bitstring\__init__.py", line 197, in <module>
    DtypeDefinition('uintle', Bits._setuintle, Bits._getuintle, int, False, uint_bits2chars,
  File "C:\<path>\python\3117\test\Lib\site-packages\bitstring\dtypes.py", line 275, in __init__
    if self.allowed_lengths:
  File "C:\<path>\python\3117\test\Lib\site-packages\bitstring\dtypes.py", line 244, in __len__
    return len(self.value)
           ^^^^^^^^^^^^^^^
OverflowError: Python int too large to convert to C ssize_t

https://github.com/scott-griffiths/bitstring/blame/main/bitstring/dtypes.py#L230 traced it down to this line when the module attempts to calculate allowed sizes. It works fine on 64 bit python, but the generator expression here:

https://github.com/scott-griffiths/bitstring/blame/main/bitstring/dtypes.py#L231 self.value = range(value[0], infinity, step)

causes the length of self.value to exceed the max ssize_t (presumably 4 bytes).

scott-griffiths commented 7 months ago

Thank you for the swift bug report.

Ah, 32-bit Python. That brings back memories. The fix should be trivial (what could possibly go wrong hard-coding a value for infinity?!) but I'll probably wait a day to see if anything else has broken before releasing a 4.2.1.

Now I just have to work out how to get the CI to target a 32-bit Python....

scott-griffiths commented 7 months ago

This should now be fixed in 4.2.1. although I couldn't find a way to add the test on 32-bit Python to the CI, so strictly speaking I haven't tested the fix actually works...