This commit is contained in:
Raymond Hettinger 2016-02-01 21:21:19 -08:00
parent 94c3089818
commit a63897164e
3 changed files with 16 additions and 18 deletions

View File

@ -477,8 +477,8 @@ or subtracting from an empty counter.
Insert *x* into the deque at position *i*. Insert *x* into the deque at position *i*.
If the insertion causes a bounded deque to grow beyond *maxlen*, the If the insertion would cause a bounded deque to grow beyond *maxlen*,
rightmost element is then removed to restore the size limit. an :exc:`IndexError` is raised.
.. versionadded:: 3.5 .. versionadded:: 3.5

View File

@ -304,19 +304,20 @@ class TestBasic(unittest.TestCase):
s.insert(i, 'Z') s.insert(i, 'Z')
self.assertEqual(list(d), s) self.assertEqual(list(d), s)
def test_index_bug_26194(self): def test_insert_bug_26194(self):
data = 'ABC' data = 'ABC'
for i in range(len(data) + 1): d = deque(data, maxlen=len(data))
d = deque(data, len(data)) with self.assertRaises(IndexError):
d.insert(i, None) d.insert(2, None)
s = list(data)
s.insert(i, None) elements = 'ABCDEFGHI'
s.pop() for i in range(-len(elements), len(elements)):
self.assertEqual(list(d), s) d = deque(elements, maxlen=len(elements)+1)
if i < len(data): d.insert(i, 'Z')
self.assertIsNone(d[i]) if i >= 0:
self.assertEqual(d[i], 'Z')
else: else:
self.assertTrue(None not in d) self.assertEqual(d[i-1], 'Z')
def test_imul(self): def test_imul(self):
for n in (-10, -1, 0, 1, 2, 10, 1000): for n in (-10, -1, 0, 1, 2, 10, 1000):

View File

@ -1085,16 +1085,13 @@ deque_insert(dequeobject *deque, PyObject *args)
Py_ssize_t index; Py_ssize_t index;
Py_ssize_t n = Py_SIZE(deque); Py_ssize_t n = Py_SIZE(deque);
PyObject *value; PyObject *value;
PyObject *oldvalue;
PyObject *rv; PyObject *rv;
if (!PyArg_ParseTuple(args, "nO:insert", &index, &value)) if (!PyArg_ParseTuple(args, "nO:insert", &index, &value))
return NULL; return NULL;
if (deque->maxlen == Py_SIZE(deque)) { if (deque->maxlen == Py_SIZE(deque)) {
if (index >= deque->maxlen || Py_SIZE(deque) == 0) PyErr_SetString(PyExc_IndexError, "deque already at its maximum size");
Py_RETURN_NONE; return NULL;
oldvalue = deque_pop(deque, NULL);
Py_DECREF(oldvalue);
} }
if (index >= n) if (index >= n)
return deque_append(deque, value); return deque_append(deque, value);