]> granicus.if.org Git - python/commitdiff
Issue #26194: Inserting into a full deque to raise an IndexError
authorRaymond Hettinger <python@rcn.com>
Tue, 2 Feb 2016 05:19:22 +0000 (21:19 -0800)
committerRaymond Hettinger <python@rcn.com>
Tue, 2 Feb 2016 05:19:22 +0000 (21:19 -0800)
Doc/library/collections.rst
Lib/test/test_deque.py
Misc/NEWS
Modules/_collectionsmodule.c

index e89da350230b82571442b1fd7604f981ba727cc9..092221fa7ebc19c9630028eaad98c06934b027f9 100644 (file)
@@ -477,8 +477,8 @@ or subtracting from an empty counter.
 
         Insert *x* into the deque at position *i*.
 
-        If the insertion causes a bounded deque to grow beyond *maxlen*, the
-        rightmost element is then removed to restore the size limit.
+        If the insertion would cause a bounded deque to grow beyond *maxlen*,
+        an :exc:`IndexError` is raised.
 
         .. versionadded:: 3.5
 
index d2a463351bd4978990fcb009a72b03124b6d609e..2ca508d7e82839380ed38999f0a8e9de84b60379 100644 (file)
@@ -304,19 +304,20 @@ class TestBasic(unittest.TestCase):
             s.insert(i, 'Z')
             self.assertEqual(list(d), s)
 
-    def test_index_bug_26194(self):
+    def test_insert_bug_26194(self):
         data = 'ABC'
-        for i in range(len(data) + 1):
-            d = deque(data, len(data))
-            d.insert(i, None)
-            s = list(data)
-            s.insert(i, None)
-            s.pop()
-            self.assertEqual(list(d), s)
-            if i < len(data):
-                self.assertIsNone(d[i])
+        d = deque(data, maxlen=len(data))
+        with self.assertRaises(IndexError):
+            d.insert(2, None)
+
+        elements = 'ABCDEFGHI'
+        for i in range(-len(elements), len(elements)):
+            d = deque(elements, maxlen=len(elements)+1)
+            d.insert(i, 'Z')
+            if i >= 0:
+                self.assertEqual(d[i], 'Z')
             else:
-                self.assertTrue(None not in d)
+                self.assertEqual(d[i-1], 'Z')
 
     def test_imul(self):
         for n in (-10, -1, 0, 1, 2, 10, 1000):
index a95d2a06c058679e501591b5a5091103490c126d..ba1f4de5f5c3c235bde432e9b686345044adf241 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,8 +22,8 @@ Core and Builtins
   compiler issues.
 
 - Issue #26194:  Deque.insert() gave odd results for bounded deques that had
-  reached their maximum size.  Now, the insert will happen normally and then
-  any overflowing element will be truncated from the right side.
+  reached their maximum size.  Now an IndexError will be raised when attempting
+  to insert into a full deque.
 
 - Issue #25843: When compiling code, don't merge constants if they are equal
   but have a different types. For example, ``f1, f2 = lambda: 1, lambda: 1.0``
index b9c4f3b9263cc6326fe3994d8b4e067775310213..10fbcfe6b9bb99711ee49ef1ac4db3b014b2b6ab 100644 (file)
@@ -973,16 +973,13 @@ deque_insert(dequeobject *deque, PyObject *args)
     Py_ssize_t index;
     Py_ssize_t n = Py_SIZE(deque);
     PyObject *value;
-    PyObject *oldvalue;
     PyObject *rv;
 
     if (!PyArg_ParseTuple(args, "nO:insert", &index, &value))
         return NULL;
     if (deque->maxlen == Py_SIZE(deque)) {
-        if (index >= deque->maxlen || Py_SIZE(deque) == 0)
-            Py_RETURN_NONE;
-        oldvalue = deque_pop(deque, NULL);
-        Py_DECREF(oldvalue);
+        PyErr_SetString(PyExc_IndexError, "deque already at its maximum size");
+        return NULL;
     }
     if (index >= n)
         return deque_append(deque, value);