diff options
author | Alexandre Vassalotti <alexandre@peadrop.com> | 2009-07-05 06:25:14 (GMT) |
---|---|---|
committer | Alexandre Vassalotti <alexandre@peadrop.com> | 2009-07-05 06:25:14 (GMT) |
commit | e503cf9b0e74ec76fce8c021f0c73aa75c4cb6f4 (patch) | |
tree | 99bacfeac2156d4190fb1d4110af554ed19017b6 | |
parent | b78637a5bc8fcb1787d9a7e452fa9926eec538b4 (diff) | |
download | cpython-e503cf9b0e74ec76fce8c021f0c73aa75c4cb6f4.zip cpython-e503cf9b0e74ec76fce8c021f0c73aa75c4cb6f4.tar.gz cpython-e503cf9b0e74ec76fce8c021f0c73aa75c4cb6f4.tar.bz2 |
Fix array.extend and array.__iadd__ to handle the case where an array
is extended with itself.
This bug is specific the py3k version of arraymodule.c
-rwxr-xr-x | Lib/test/test_array.py | 13 | ||||
-rw-r--r-- | Modules/arraymodule.c | 8 |
2 files changed, 18 insertions, 3 deletions
diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 79e1199..3dd4f6d 100755 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -272,6 +272,12 @@ class BaseTest(unittest.TestCase): a, array.array(self.typecode, self.example[::-1]+2*self.example) ) + a = array.array(self.typecode, self.example) + a += a + self.assertEqual( + a, + array.array(self.typecode, self.example + self.example) + ) b = array.array(self.badtypecode()) self.assertRaises(TypeError, a.__add__, b) @@ -667,6 +673,13 @@ class BaseTest(unittest.TestCase): array.array(self.typecode, self.example+self.example[::-1]) ) + a = array.array(self.typecode, self.example) + a.extend(a) + self.assertEqual( + a, + array.array(self.typecode, self.example+self.example) + ) + b = array.array(self.badtypecode()) self.assertRaises(TypeError, a.extend, b) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 256bcd8..1dbe918 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -810,8 +810,8 @@ array_iter_extend(arrayobject *self, PyObject *bb) static int array_do_extend(arrayobject *self, PyObject *bb) { - Py_ssize_t size, oldsize; - + Py_ssize_t size, oldsize, bbsize; + if (!array_Check(bb)) return array_iter_extend(self, bb); #define b ((arrayobject *)bb) @@ -826,11 +826,13 @@ array_do_extend(arrayobject *self, PyObject *bb) return -1; } oldsize = Py_SIZE(self); + /* Get the size of bb before resizing the array since bb could be self. */ + bbsize = Py_SIZE(bb); size = oldsize + Py_SIZE(b); if (array_resize(self, size) == -1) return -1; memcpy(self->ob_item + oldsize * self->ob_descr->itemsize, - b->ob_item, Py_SIZE(b) * b->ob_descr->itemsize); + b->ob_item, bbsize * b->ob_descr->itemsize); return 0; #undef b |