]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix array.array.insert(), so that it treats negative indices as
authorWalter Dörwald <walter@livinglogic.de>
Sun, 18 May 2003 03:15:10 +0000 (03:15 +0000)
committerWalter Dörwald <walter@livinglogic.de>
Sun, 18 May 2003 03:15:10 +0000 (03:15 +0000)
being relative to the end of the array, just like list.insert() does.
This closes SF bug #739313.

Doc/lib/libarray.tex
Lib/test/test_array.py
Misc/NEWS
Modules/arraymodule.c

index a590ed985aaa0ecbf1be8a061d1aeece2ea10f74..6ec056fbf2c169f2cdaa480380fb92f9088dbe41 100644 (file)
@@ -145,7 +145,8 @@ the first occurence of \var{x} in the array.
 
 \begin{methoddesc}[array]{insert}{i, x}
 Insert a new item with value \var{x} in the array before position
-\var{i}.
+\var{i}. Negative values are treated as being relative to the end
+of the array.
 \end{methoddesc}
 
 \begin{methoddesc}[array]{pop}{\optional{i}}
index 7a3308be5549d99b7092203b28055afed375620f..98589a5c5bbab712a56629bab7360a8d788aa091 100755 (executable)
@@ -82,6 +82,30 @@ class BaseTest(unittest.TestCase):
         self.assertRaises(TypeError, a.insert, None)
         self.assertRaises(TypeError, a.insert, 0, None)
 
+        a = array.array(self.typecode, self.example)
+        a.insert(-1, self.example[0])
+        self.assertEqual(
+            a,
+            array.array(
+                self.typecode,
+                self.example[:-1] + self.example[:1] + self.example[-1:]
+            )
+        )
+
+        a = array.array(self.typecode, self.example)
+        a.insert(-1000, self.example[0])
+        self.assertEqual(
+            a,
+            array.array(self.typecode, self.example[:1] + self.example)
+        )
+
+        a = array.array(self.typecode, self.example)
+        a.insert(1000, self.example[0])
+        self.assertEqual(
+            a,
+            array.array(self.typecode, self.example + self.example[:1])
+        )
+
     def test_tofromfile(self):
         a = array.array(self.typecode, 2*self.example)
         self.assertRaises(TypeError, a.tofile)
index 02f845da0fdc890379599c0aeabeb726cf32a7f5..3a819b881f182fcedd884cf3160f064496222486 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -26,6 +26,9 @@ Core and builtins
 Extension modules
 -----------------
 
+- array.array.insert() now treats negative indices as being relative
+  to the end of the array, just like list.insert() does. (SF bug #739313)
+
 - The datetime module classes datetime, time, and timedelta are now
   properly subclassable.
 
index c03160eabac4b0c7f5c168bd32e456e02978330d..dcb66cf0a89da388718008620b4de4bcd6f4be43 100644 (file)
@@ -470,8 +470,11 @@ ins1(arrayobject *self, int where, PyObject *v)
                PyErr_NoMemory();
                return -1;
        }
-       if (where < 0)
-               where = 0;
+       if (where < 0) {
+               where += self->ob_size;
+               if (where < 0)
+                       where = 0;
+       }
        if (where > self->ob_size)
                where = self->ob_size;
        memmove(items + (where+1)*self->ob_descr->itemsize,