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

ensure readonly attribute is handled correctly when creating new objects #211

Merged
merged 5 commits into from
Nov 10, 2023

Conversation

ilanschnell
Copy link
Owner

@ilanschnell ilanschnell commented Nov 9, 2023

Currently, the .copy() method (as well as several others which create new bitarray objects) always sets the read-only attribute to False. While this is not problematic for bitarrays, it is a problem when copying frozenbitarrays:

>>> from bitarray import frozenbitarray
>>> a = frozenbitarray('1101')
>>> c = a.copy()
>>> c.readonly
False
>>> v = memoryview(c)
>>> v[0] = 0  # chaning the buffer should not be possible
>>> c
frozenbitarray('0000')

In order to fix this problem, we set the readonly attribute depending on whether it object in an instance of the frozenbitarray class. With this PR, we get:

>>> a = frozenbitarray('1101')
>>> c = a.copy()
>>> v = memoryview(c)
>>> v[0] = 0x00
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot modify read-only memory

@ilanschnell ilanschnell merged commit 139aa63 into master Nov 10, 2023
@ilanschnell ilanschnell deleted the cp-readonly branch November 28, 2023 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant