From 73081e98d81a2538540d1cfb3dfc40b4c7ca13f4 Mon Sep 17 00:00:00 2001 From: Ilan Schnell Date: Fri, 20 Aug 2021 22:44:49 -0500 Subject: [PATCH] fix another speical case --- bitarray/_bitarray.c | 11 +++++++---- bitarray/test_bitarray.py | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/bitarray/_bitarray.c b/bitarray/_bitarray.c index c275baea..e0b213b1 100644 --- a/bitarray/_bitarray.c +++ b/bitarray/_bitarray.c @@ -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; diff --git a/bitarray/test_bitarray.py b/bitarray/test_bitarray.py index fb57ad55..09983d14 100644 --- a/bitarray/test_bitarray.py +++ b/bitarray/test_bitarray.py @@ -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')