[Python-checkins] cpython (merge 3.5 -> default): merge

raymond.hettinger python-checkins at python.org
Wed Jan 27 00:46:10 EST 2016


https://hg.python.org/cpython/rev/58266f5101cc
changeset:   100073:58266f5101cc
parent:      100071:58086f0a953a
parent:      100072:ce3d47eaeb21
user:        Raymond Hettinger <python at rcn.com>
date:        Tue Jan 26 21:46:03 2016 -0800
summary:
  merge

files:
  Doc/library/collections.rst  |   3 +++
  Lib/test/test_deque.py       |  14 ++++++++++++++
  Modules/_collectionsmodule.c |   7 +++++++
  3 files changed, 24 insertions(+), 0 deletions(-)


diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -477,6 +477,9 @@
 
         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.
+
         .. versionadded:: 3.5
 
 
diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py
--- a/Lib/test/test_deque.py
+++ b/Lib/test/test_deque.py
@@ -304,6 +304,20 @@
             s.insert(i, 'Z')
             self.assertEqual(list(d), s)
 
+    def test_index_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])
+            else:
+                self.assertTrue(None not in d)
+
     def test_imul(self):
         for n in (-10, -1, 0, 1, 2, 10, 1000):
             d = deque()
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -1085,10 +1085,17 @@
     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);
+    }
     if (index >= n)
         return deque_append(deque, value);
     if (index <= -n || index == 0)

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list