Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b00da57

Browse files
committed
Issue #26194: Inserting into a full deque to raise an IndexError
1 parent 3e72309 commit b00da57

4 files changed

Lines changed: 18 additions & 20 deletions

File tree

Doc/library/collections.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,8 @@ or subtracting from an empty counter.
477477

478478
Insert *x* into the deque at position *i*.
479479

480-
If the insertion causes a bounded deque to grow beyond *maxlen*, the
481-
rightmost element is then removed to restore the size limit.
480+
If the insertion would cause a bounded deque to grow beyond *maxlen*,
481+
an :exc:`IndexError` is raised.
482482

483483
.. versionadded:: 3.5
484484

Lib/test/test_deque.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -304,19 +304,20 @@ def test_insert(self):
304304
s.insert(i, 'Z')
305305
self.assertEqual(list(d), s)
306306

307-
def test_index_bug_26194(self):
307+
def test_insert_bug_26194(self):
308308
data = 'ABC'
309-
for i in range(len(data) + 1):
310-
d = deque(data, len(data))
311-
d.insert(i, None)
312-
s = list(data)
313-
s.insert(i, None)
314-
s.pop()
315-
self.assertEqual(list(d), s)
316-
if i < len(data):
317-
self.assertIsNone(d[i])
309+
d = deque(data, maxlen=len(data))
310+
with self.assertRaises(IndexError):
311+
d.insert(2, None)
312+
313+
elements = 'ABCDEFGHI'
314+
for i in range(-len(elements), len(elements)):
315+
d = deque(elements, maxlen=len(elements)+1)
316+
d.insert(i, 'Z')
317+
if i >= 0:
318+
self.assertEqual(d[i], 'Z')
318319
else:
319-
self.assertTrue(None not in d)
320+
self.assertEqual(d[i-1], 'Z')
320321

321322
def test_imul(self):
322323
for n in (-10, -1, 0, 1, 2, 10, 1000):

Misc/NEWS

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Core and Builtins
2222
compiler issues.
2323

2424
- Issue #26194: Deque.insert() gave odd results for bounded deques that had
25-
reached their maximum size. Now, the insert will happen normally and then
26-
any overflowing element will be truncated from the right side.
25+
reached their maximum size. Now an IndexError will be raised when attempting
26+
to insert into a full deque.
2727

2828
- Issue #25843: When compiling code, don't merge constants if they are equal
2929
but have a different types. For example, ``f1, f2 = lambda: 1, lambda: 1.0``

Modules/_collectionsmodule.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -973,16 +973,13 @@ deque_insert(dequeobject *deque, PyObject *args)
973973
Py_ssize_t index;
974974
Py_ssize_t n = Py_SIZE(deque);
975975
PyObject *value;
976-
PyObject *oldvalue;
977976
PyObject *rv;
978977

979978
if (!PyArg_ParseTuple(args, "nO:insert", &index, &value))
980979
return NULL;
981980
if (deque->maxlen == Py_SIZE(deque)) {
982-
if (index >= deque->maxlen || Py_SIZE(deque) == 0)
983-
Py_RETURN_NONE;
984-
oldvalue = deque_pop(deque, NULL);
985-
Py_DECREF(oldvalue);
981+
PyErr_SetString(PyExc_IndexError, "deque already at its maximum size");
982+
return NULL;
986983
}
987984
if (index >= n)
988985
return deque_append(deque, value);

0 commit comments

Comments
 (0)