Browse files

Fix #32: Add .clear() and .copy() methods, to match those added to Py…

…thon's list in 3.2.
  • Loading branch information...
1 parent bdef127 commit 54375be064aad0b4d2c5765cbfc5932b83babbc3 @DanielStutzbach committed Dec 9, 2012
Showing with 78 additions and 2 deletions.
  1. +30 −2 blist/_blist.c
  2. +41 −0 blist/test/list_tests.py
  3. +7 −0 blist/test/unittest.py
View
32 blist/_blist.c
@@ -5726,7 +5726,7 @@ py_blist_traverse(PyObject *oself, visitproc visit, void *arg)
}
BLIST_PYAPI(int)
-py_blist_clear(PyObject *oself)
+py_blist_tp_clear(PyObject *oself)
{
PyBList *self;
@@ -6806,6 +6806,27 @@ py_blist_pop(PyBList *self, PyObject *args)
}
BLIST_PYAPI(PyObject *)
+py_blist_clear(PyBList *self)
+{
+ invariants(self, VALID_USER|VALID_RW|VALID_DECREF);
+
+ blist_forget_children(self);
+ self->n = 0;
+ self->leaf = 1;
+ ext_dealloc((PyBListRoot *) self);
+
+ decref_flush();
+ Py_RETURN_NONE;
+}
+
+BLIST_PYAPI(PyObject *)
+py_blist_copy(PyBList *self)
+{
+ invariants(self, VALID_USER);
+ return (PyObject *) _blist(blist_root_copy(self));
+}
+
+BLIST_PYAPI(PyObject *)
py_blist_insert(PyBList *self, PyObject *args)
{
Py_ssize_t i;
@@ -7078,6 +7099,10 @@ PyDoc_STRVAR(reverse_doc,
PyDoc_STRVAR(sort_doc,
"L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;\n\
cmp(x, y) -> -1, 0, 1");
+PyDoc_STRVAR(clear_doc,
+"L.clear() -> None -- remove all items from L");
+PyDoc_STRVAR(copy_doc,
+"L.copy() -> list -- a shallow copy of L");
static PyMethodDef blist_methods[] = {
{"__getitem__", (PyCFunction)py_blist_subscript, METH_O|METH_COEXIST, getitem_doc},
@@ -7092,6 +7117,9 @@ static PyMethodDef blist_methods[] = {
{"pop", (PyCFunction)py_blist_pop, METH_VARARGS, pop_doc},
{"remove", (PyCFunction)py_blist_remove, METH_O, remove_doc},
{"index", (PyCFunction)py_blist_index, METH_VARARGS, index_doc},
+ {"clear", (PyCFunction)py_blist_clear, METH_NOARGS, clear_doc},
+ {"copy", (PyCFunction)py_blist_copy, METH_NOARGS, copy_doc},
+
{"count", (PyCFunction)py_blist_count, METH_O, count_doc},
{"reverse", (PyCFunction)py_blist_reverse, METH_NOARGS, reverse_doc},
{"sort", (PyCFunction)py_blist_sort, METH_VARARGS | METH_KEYWORDS, sort_doc},
@@ -7299,7 +7327,7 @@ PyTypeObject PyRootBList_Type = {
,
blist_doc, /* tp_doc */
py_blist_traverse, /* tp_traverse */
- py_blist_clear, /* tp_clear */
+ py_blist_tp_clear, /* tp_clear */
py_blist_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */
py_blist_iter, /* tp_iter */
View
41 blist/test/list_tests.py
@@ -430,6 +430,47 @@ def test_reverse(self):
self.assertRaises(TypeError, u.reverse, 42)
+ def test_clear(self):
+ u = self.type2test([2, 3, 4])
+ u.clear()
+ self.assertEqual(u, [])
+
+ u = self.type2test([])
+ u.clear()
+ self.assertEqual(u, [])
+
+ u = self.type2test([])
+ u.append(1)
+ u.clear()
+ u.append(2)
+ self.assertEqual(u, [2])
+
+ self.assertRaises(TypeError, u.clear, None)
+
+ def test_copy(self):
+ u = self.type2test([1, 2, 3])
+ v = u.copy()
+ self.assertEqual(v, [1, 2, 3])
+
+ u = self.type2test([])
+ v = u.copy()
+ self.assertEqual(v, [])
+
+ # test that it's indeed a copy and not a reference
+ u = self.type2test(['a', 'b'])
+ v = u.copy()
+ v.append('i')
+ self.assertEqual(u, ['a', 'b'])
+ self.assertEqual(v, u + self.type2test(['i']))
+
+ # test that it's a shallow, not a deep copy
+ u = self.type2test([1, 2, [3, 4], 5])
+ v = u.copy()
+ self.assertEqual(u, v)
+ self.assertIs(v[3], u[3])
+
+ self.assertRaises(TypeError, u.copy, None)
+
def test_sort(self):
u = self.type2test([1, 0])
u.sort()
View
7 blist/test/unittest.py
@@ -201,6 +201,13 @@ def __init__(self, methodName='runTest'):
raise ValueError("no such test method in %s: %s" % \
(self.__class__, methodName))
+ def assertIs(self, expr1, expr2, msg=None):
+ """Just like self.assertTrue(a is b), but with a nicer default message."""
+ if expr1 is not expr2:
+ standardMsg = '%s is not %s' % (safe_repr(expr1),
+ safe_repr(expr2))
+ self.fail(self._formatMessage(msg, standardMsg))
+
def assertIn(self, member, container, msg=None):
"""Just like self.assertTrue(a in b), but with a nicer default message."""
if member not in container:

0 comments on commit 54375be

Please sign in to comment.