Skip to content

Commit

Permalink
fix another speical case
Browse files Browse the repository at this point in the history
  • Loading branch information
ilanschnell committed Aug 21, 2021
1 parent 779d2df commit 73081e9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
11 changes: 7 additions & 4 deletions bitarray/_bitarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -1881,10 +1881,13 @@ setslice_bitarray(bitarrayobject *self, PyObject *slice,
/* number of bits by which self has to be increased (decreased) */
increase = other->nbits - slicelength;

/* make a copy of other, when the other buffer is part of the self buffer,
that is when it's address falls into self's buffer */
if (self->ob_item <= other->ob_item &&
other->ob_item <= self->ob_item + Py_SIZE(self)) {
/* make a copy of other, when the other buffer is part of the self buffer
that is when it's address falls into self's buffer (or vise versa) */
if ((self->ob_item <= other->ob_item &&
other->ob_item <= self->ob_item + Py_SIZE(self)) ||
(other->ob_item <= self->ob_item &&
self->ob_item <= other->ob_item + Py_SIZE(other)))
{
other = (bitarrayobject *) bitarray_copy(other);
if (other == NULL)
return -1;
Expand Down
14 changes: 14 additions & 0 deletions bitarray/test_bitarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,20 @@ def test_setslice_self_shared_buffer_2(self):
c[8:56] = c[55:7:-1]
self.assertEqual(a, c)

def test_setslice_self_shared_buffer_3(self):
# Requires to check for (in setslice_bitarray()):
#
# other->ob_item <= self->ob_item <= other->ob_item + Py_SIZE(other)
#
a = bitarray('11111111 11000000 00000000')
b = bitarray(buffer=memoryview(a)[:2])
c = bitarray(buffer=memoryview(a)[1:])
self.assertEqual(b, bitarray('11111111 11000000'))
self.assertEqual(c, bitarray('11000000 00000000'))
c[::-1] = b
self.assertEqual(c, bitarray('00000011 11111111'))
self.assertEqual(a, bitarray('11111111 00000011 11111111'))

def test_setslice_bitarray(self):
a = bitarray('11111111 1111')
a[2:6] = bitarray('0010')
Expand Down

0 comments on commit 73081e9

Please sign in to comment.