Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BitStream.__add__ throws AttributeError: 'Bits' object has no attribute '_pos' #329

Closed
whitslack opened this issue May 10, 2024 · 3 comments

Comments

@whitslack
Copy link

Using version 4.2.1…

This is okay:

>>> BitStream() + '0xff'
BitStream('0xff')

But this fails:

>>> BitStream() + Bits('0xff')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.12/site-packages/bitstring/bitstream.py", line 189, in __add__
    s._pos = 0
    ^^^^^^
AttributeError: 'Bits' object has no attribute '_pos'

And so does this:

>>> BitStream() + BitArray('0xff')
Traceback (most recent call last):
  File "/usr/lib/python3.12/site-packages/bitstring/bitarray_.py", line 126, in __setattr__
    super().__setattr__(attribute, value)
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 "/usr/lib/python3.12/site-packages/bitstring/bitstream.py", line 189, in __add__
    s._pos = 0
    ^^^^^^
  File "/usr/lib/python3.12/site-packages/bitstring/bitarray_.py", line 128, in __setattr__
    dtype = bitstring.dtypes.Dtype(attribute)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/bitstring/dtypes.py", line 63, in __new__
    x = cls._new_from_token(token, scale)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/bitstring/dtypes.py", line 142, in _new_from_token
    return dtype_register.get_dtype(*utils.parse_name_length_token(token), scale=scale)
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/bitstring/utils.py", line 94, in parse_name_length_token
    raise ValueError(f"Can't parse 'name[:]length' token '{fmt}'.")
ValueError: Can't parse 'name[:]length' token '_pos'.

But this is okay:

>>> BitStream() + BitStream('0xff')
BitStream('0xff')

So it would appear that BitStream._add_ is expecting its right-hand operand to be a BitStream (and have a _pos attribute), but the API is supposed to allow any Bits on the right-hand side.

This is causing a regression in one of Core Lightning's pyln-proto unit tests, whereas there was no failure when using Bitstring 4.1.4.

gentoo-repo-qa-bot pushed a commit to gentoo-mirror/bitcoin that referenced this issue May 10, 2024
Bitstring 4.2 introduces a regression that causes unit test failures in
pyln-proto.

See: scott-griffiths/bitstring#329
@scott-griffiths scott-griffiths self-assigned this May 10, 2024
@scott-griffiths scott-griffiths added this to the 4.2.2 milestone May 10, 2024
@scott-griffiths
Copy link
Owner

Hi. Thanks for reporting this.

I'm hoping to make a 4.2.2 release this weekend, and I'll make sure I get a fix in for this as part of that release.

@scott-griffiths
Copy link
Owner

bitstring 4.2.2 is now released. 🎉

@whitslack
Copy link
Author

Thank you for the quick response.

gentoo-repo-qa-bot pushed a commit to gentoo-mirror/bitcoin that referenced this issue May 10, 2024
Bitstring 4.2.2 contains a fix for the regression that was breaking
pyln-proto.

See: scott-griffiths/bitstring#329 (comment)
See: scott-griffiths/bitstring@f1a7e62
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants