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

Skip to content

Commit cbbaa96

Browse files
committed
Issue #10516: adding list.clear() and list.copy() methods
1 parent 3108f98 commit cbbaa96

6 files changed

Lines changed: 81 additions & 2 deletions

File tree

Doc/library/stdtypes.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,8 @@ Note that while lists allow their items to be of any type, bytearray object
16421642
single: append() (sequence method)
16431643
single: extend() (sequence method)
16441644
single: count() (sequence method)
1645+
single: clear() (sequence method)
1646+
single: copy() (sequence method)
16451647
single: index() (sequence method)
16461648
single: insert() (sequence method)
16471649
single: pop() (sequence method)
@@ -1673,6 +1675,12 @@ Note that while lists allow their items to be of any type, bytearray object
16731675
| ``s.extend(x)`` | same as ``s[len(s):len(s)] = | \(2) |
16741676
| | x`` | |
16751677
+------------------------------+--------------------------------+---------------------+
1678+
| ``s.clear()`` | remove all items from ``s`` | \(8) |
1679+
| | | |
1680+
+------------------------------+--------------------------------+---------------------+
1681+
| ``s.copy()`` | return a shallow copy of ``s`` | \(8) |
1682+
| | | |
1683+
+------------------------------+--------------------------------+---------------------+
16761684
| ``s.count(x)`` | return number of *i*'s for | |
16771685
| | which ``s[i] == x`` | |
16781686
+------------------------------+--------------------------------+---------------------+
@@ -1749,7 +1757,11 @@ Notes:
17491757
detect that the list has been mutated during a sort.
17501758

17511759
(8)
1752-
:meth:`sort` is not supported by :class:`bytearray` objects.
1760+
:meth:`clear`, :meth:`!copy` and :meth:`sort` are not supported by
1761+
:class:`bytearray` objects.
1762+
1763+
.. versionadded:: 3.3
1764+
:meth:`clear` and :meth:`!copy` methods.
17531765

17541766

17551767
.. _bytes-methods:

Lib/collections/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,8 @@ def append(self, item): self.data.append(item)
844844
def insert(self, i, item): self.data.insert(i, item)
845845
def pop(self, i=-1): return self.data.pop(i)
846846
def remove(self, item): self.data.remove(item)
847+
def clear(self): self.data.clear()
848+
def copy(self): return self.data.copy()
847849
def count(self, item): return self.data.count(item)
848850
def index(self, item, *args): return self.data.index(item, *args)
849851
def reverse(self): self.data.reverse()

Lib/test/list_tests.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,48 @@ def test_reverse(self):
425425

426426
self.assertRaises(TypeError, u.reverse, 42)
427427

428+
def test_clear(self):
429+
u = self.type2test([2, 3, 4])
430+
u.clear()
431+
self.assertEqual(u, [])
432+
433+
u = self.type2test([])
434+
u.clear()
435+
self.assertEqual(u, [])
436+
437+
u = self.type2test([])
438+
u.append(1)
439+
u.clear()
440+
u.append(2)
441+
self.assertEqual(u, [2])
442+
443+
self.assertRaises(TypeError, u.clear, None)
444+
445+
def test_copy(self):
446+
u = self.type2test([1, 2, 3])
447+
v = u.copy()
448+
self.assertEqual(v, [1, 2, 3])
449+
450+
u = self.type2test([])
451+
v = u.copy()
452+
self.assertEqual(v, [])
453+
454+
# test that it's indeed a copy and not a reference
455+
u = self.type2test(['a', 'b'])
456+
v = u.copy()
457+
v.append('i')
458+
self.assertEqual(u, ['a', 'b'])
459+
self.assertEqual(v, u + ['i'])
460+
461+
# test that it's a shallow, not a deep copy
462+
u = self.type2test([1, 2, [3, 4], 5])
463+
v = u.copy()
464+
v[2].append(666)
465+
self.assertEqual(u, [1, 2, [3, 4, 666], 5])
466+
self.assertEqual(u, v)
467+
468+
self.assertRaises(TypeError, u.copy, None)
469+
428470
def test_sort(self):
429471
u = self.type2test([1, 0])
430472
u.sort()

Lib/test/test_descrtut.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ def merge(self, other):
199199
'__str__',
200200
'__subclasshook__',
201201
'append',
202+
'clear',
203+
'copy',
202204
'count',
203205
'extend',
204206
'index',

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Core and Builtins
3030

3131
- Check for NULL result in PyType_FromSpec.
3232

33+
- Issue #10516: New copy() and clear() methods for lists.
34+
3335
Library
3436
-------
3537

Objects/listobject.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,19 @@ listinsert(PyListObject *self, PyObject *args)
746746
return NULL;
747747
}
748748

749+
static PyObject *
750+
listclear(PyListObject *self)
751+
{
752+
list_clear(self);
753+
Py_RETURN_NONE;
754+
}
755+
756+
static PyObject *
757+
listcopy(PyListObject *self)
758+
{
759+
return list_slice(self, 0, Py_SIZE(self));
760+
}
761+
749762
static PyObject *
750763
listappend(PyListObject *self, PyObject *v)
751764
{
@@ -2322,6 +2335,10 @@ PyDoc_STRVAR(reversed_doc,
23222335
"L.__reversed__() -- return a reverse iterator over the list");
23232336
PyDoc_STRVAR(sizeof_doc,
23242337
"L.__sizeof__() -- size of L in memory, in bytes");
2338+
PyDoc_STRVAR(clear_doc,
2339+
"L.clear() -> None -- remove all items from L");
2340+
PyDoc_STRVAR(copy_doc,
2341+
"L.copy() -> list -- a shallow copy of L");
23252342
PyDoc_STRVAR(append_doc,
23262343
"L.append(object) -- append object to end");
23272344
PyDoc_STRVAR(extend_doc,
@@ -2350,9 +2367,11 @@ static PyMethodDef list_methods[] = {
23502367
{"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
23512368
{"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
23522369
{"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
2370+
{"clear", (PyCFunction)listclear, METH_NOARGS, clear_doc},
2371+
{"copy", (PyCFunction)listcopy, METH_NOARGS, copy_doc},
23532372
{"append", (PyCFunction)listappend, METH_O, append_doc},
23542373
{"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
2355-
{"extend", (PyCFunction)listextend, METH_O, extend_doc},
2374+
{"extend", (PyCFunction)listextend, METH_O, extend_doc},
23562375
{"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
23572376
{"remove", (PyCFunction)listremove, METH_O, remove_doc},
23582377
{"index", (PyCFunction)listindex, METH_VARARGS, index_doc},

0 commit comments

Comments
 (0)