[Python-checkins] r73852 - in python/branches/py3k: Lib/test/test_array.py Modules/arraymodule.c

alexandre.vassalotti python-checkins at python.org
Sun Jul 5 08:25:14 CEST 2009


Author: alexandre.vassalotti
Date: Sun Jul  5 08:25:14 2009
New Revision: 73852

Log:
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


Modified:
   python/branches/py3k/Lib/test/test_array.py
   python/branches/py3k/Modules/arraymodule.c

Modified: python/branches/py3k/Lib/test/test_array.py
==============================================================================
--- python/branches/py3k/Lib/test/test_array.py	(original)
+++ python/branches/py3k/Lib/test/test_array.py	Sun Jul  5 08:25:14 2009
@@ -272,6 +272,12 @@
             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 @@
             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)
 

Modified: python/branches/py3k/Modules/arraymodule.c
==============================================================================
--- python/branches/py3k/Modules/arraymodule.c	(original)
+++ python/branches/py3k/Modules/arraymodule.c	Sun Jul  5 08:25:14 2009
@@ -810,8 +810,8 @@
 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 @@
 		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


More information about the Python-checkins mailing list