diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py index 1d3f726839a..a0d30f18281 100644 --- a/Lib/test/test_deque.py +++ b/Lib/test/test_deque.py @@ -137,6 +137,15 @@ class TestBasic(unittest.TestCase): m.d = d self.assertRaises(RuntimeError, d.count, 3) + # test issue11004 + # block advance failed after rotation aligned elements on right side of block + d = deque([None]*16) + for i in range(len(d)): + d.rotate(-1) + d.rotate(1) + self.assertEqual(d.count(1), 0) + self.assertEqual(d.count(None), 16) + def test_comparisons(self): d = deque('xabc'); d.popleft() for e in [d, deque('abc'), deque('ab'), deque(), list(d)]: diff --git a/Misc/NEWS b/Misc/NEWS index 42ce636bc2e..f244c2336db 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -19,6 +19,8 @@ Core and Builtins non-Python managed memory while it is being modified by another thread. Patch by Matt Bandy. +- Issue #11004: Repaired edge case in deque.count(). + - Issue #8278: On Windows and with a NTFS filesystem, os.stat() and os.utime() can now handle dates after 2038. diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index c140e9777ce..faa2577f2aa 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -485,7 +485,8 @@ deque_reverse(dequeobject *deque, PyObject *unused) /* Advance left block/index pair */ leftindex++; if (leftindex == BLOCKLEN) { - assert (leftblock->rightlink != NULL); + if (leftblock->rightlink == NULL) + break; leftblock = leftblock->rightlink; leftindex = 0; } @@ -493,7 +494,8 @@ deque_reverse(dequeobject *deque, PyObject *unused) /* Step backwards with the right block/index pair */ rightindex--; if (rightindex == -1) { - assert (rightblock->leftlink != NULL); + if (rightblock->leftlink == NULL) + break; rightblock = rightblock->leftlink; rightindex = BLOCKLEN - 1; } @@ -509,7 +511,7 @@ deque_count(dequeobject *deque, PyObject *v) { block *leftblock = deque->leftblock; Py_ssize_t leftindex = deque->leftindex; - Py_ssize_t n = (deque->len); + Py_ssize_t n = deque->len; Py_ssize_t i; Py_ssize_t count = 0; PyObject *item; @@ -533,7 +535,8 @@ deque_count(dequeobject *deque, PyObject *v) /* Advance left block/index pair */ leftindex++; if (leftindex == BLOCKLEN) { - assert (leftblock->rightlink != NULL); + if (leftblock->rightlink == NULL) /* can occur when i==n-1 */ + break; leftblock = leftblock->rightlink; leftindex = 0; }