diff options
author | Raymond Hettinger <python@rcn.com> | 2016-02-02 05:21:19 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2016-02-02 05:21:19 (GMT) |
commit | a63897164eb1fb01bdc51fb8de0d6bc7eb791de9 (patch) | |
tree | 12034d565dae842890d89a224e276b72486771cf | |
parent | 94c3089818e68462150b325e83bd2996bf6a5ed7 (diff) | |
download | cpython-a63897164eb1fb01bdc51fb8de0d6bc7eb791de9.zip cpython-a63897164eb1fb01bdc51fb8de0d6bc7eb791de9.tar.gz cpython-a63897164eb1fb01bdc51fb8de0d6bc7eb791de9.tar.bz2 |
merge
-rw-r--r-- | Doc/library/collections.rst | 4 | ||||
-rw-r--r-- | Lib/test/test_deque.py | 23 | ||||
-rw-r--r-- | Modules/_collectionsmodule.c | 7 |
3 files changed, 16 insertions, 18 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index 6a0c127..080f968 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -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 diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py index 34b1be2..e2c6b6c 100644 --- a/Lib/test/test_deque.py +++ b/Lib/test/test_deque.py @@ -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): diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index 69acc64..479b052 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -1085,16 +1085,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); |