diff options
author | Raymond Hettinger <python@rcn.com> | 2005-03-19 00:00:51 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2005-03-19 00:00:51 (GMT) |
commit | d73202c596e730d8bfb22edb1a6c4cc2f0e9ca6f (patch) | |
tree | 0bfe624c8356dec97a46644233eae277b641dbbe | |
parent | 4aec61e0fc29217c93acd0615ad54bd081a64658 (diff) | |
download | cpython-d73202c596e730d8bfb22edb1a6c4cc2f0e9ca6f.zip cpython-d73202c596e730d8bfb22edb1a6c4cc2f0e9ca6f.tar.gz cpython-d73202c596e730d8bfb22edb1a6c4cc2f0e9ca6f.tar.bz2 |
Apply remove's mutation test after every equality test.
-rw-r--r-- | Lib/test/test_deque.py | 15 | ||||
-rw-r--r-- | Modules/collectionsmodule.c | 14 |
2 files changed, 15 insertions, 14 deletions
diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py index f498124..7e80e8c 100644 --- a/Lib/test/test_deque.py +++ b/Lib/test/test_deque.py @@ -19,11 +19,12 @@ class BadCmp: raise RuntimeError class MutateCmp: - def __init__(self, deque): + def __init__(self, deque, result): self.deque = deque + self.result = result def __eq__(self, other): self.deque.clear() - return True + return self.result class TestBasic(unittest.TestCase): @@ -226,11 +227,11 @@ class TestBasic(unittest.TestCase): self.assert_(x is y) # Handle evil mutator - d = deque(['ab']) - d.extend([MutateCmp(d), 'c']) - e = deque(d) - self.assertRaises(IndexError, d.remove, 'c') - self.assertEqual(d, deque()) + for match in (True, False): + d = deque(['ab']) + d.extend([MutateCmp(d, match), 'c']) + self.assertRaises(IndexError, d.remove, 'c') + self.assertEqual(d, deque()) def test_repr(self): d = deque(xrange(200)) diff --git a/Modules/collectionsmodule.c b/Modules/collectionsmodule.c index 49c486f..2fbd729 100644 --- a/Modules/collectionsmodule.c +++ b/Modules/collectionsmodule.c @@ -376,14 +376,14 @@ deque_remove(dequeobject *deque, PyObject *value) for (i=0 ; i<n ; i++) { PyObject *item = deque->leftblock->data[deque->leftindex]; int cmp = PyObject_RichCompareBool(item, value, Py_EQ); + + if (deque->len != n) { + PyErr_SetString(PyExc_IndexError, + "deque mutated during remove()."); + return NULL; + } if (cmp > 0) { - PyObject *tgt; - if (deque->len != n) { - PyErr_SetString(PyExc_IndexError, - "deque mutated during remove()."); - return NULL; - } - tgt = deque_popleft(deque, NULL); + PyObject *tgt = deque_popleft(deque, NULL); assert (tgt != NULL); Py_DECREF(tgt); if (_deque_rotate(deque, i) == -1) |