summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorEli Bendersky <eliben@gmail.com>2011-02-25 05:47:53 (GMT)
committerEli Bendersky <eliben@gmail.com>2011-02-25 05:47:53 (GMT)
commitcbbaa96036b8467c21f2c7127a3711fcb405d00f (patch)
treeeb49c673b4b8adb17eb3bf17188c65df9d5f89ea /Lib
parent3108f983197991865c30d49de37b9a48e60c656e (diff)
downloadcpython-cbbaa96036b8467c21f2c7127a3711fcb405d00f.zip
cpython-cbbaa96036b8467c21f2c7127a3711fcb405d00f.tar.gz
cpython-cbbaa96036b8467c21f2c7127a3711fcb405d00f.tar.bz2
Issue #10516: adding list.clear() and list.copy() methods
Diffstat (limited to 'Lib')
-rw-r--r--Lib/collections/__init__.py2
-rw-r--r--Lib/test/list_tests.py42
-rw-r--r--Lib/test/test_descrtut.py2
3 files changed, 46 insertions, 0 deletions
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index 11a2993..4317535 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -844,6 +844,8 @@ class UserList(MutableSequence):
def insert(self, i, item): self.data.insert(i, item)
def pop(self, i=-1): return self.data.pop(i)
def remove(self, item): self.data.remove(item)
+ def clear(self): self.data.clear()
+ def copy(self): return self.data.copy()
def count(self, item): return self.data.count(item)
def index(self, item, *args): return self.data.index(item, *args)
def reverse(self): self.data.reverse()
diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py
index e3a7845..89ea40f 100644
--- a/Lib/test/list_tests.py
+++ b/Lib/test/list_tests.py
@@ -425,6 +425,48 @@ class CommonTest(seq_tests.CommonTest):
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 + ['i'])
+
+ # test that it's a shallow, not a deep copy
+ u = self.type2test([1, 2, [3, 4], 5])
+ v = u.copy()
+ v[2].append(666)
+ self.assertEqual(u, [1, 2, [3, 4, 666], 5])
+ self.assertEqual(u, v)
+
+ self.assertRaises(TypeError, u.copy, None)
+
def test_sort(self):
u = self.type2test([1, 0])
u.sort()
diff --git a/Lib/test/test_descrtut.py b/Lib/test/test_descrtut.py
index 2db3d33..c3355b9 100644
--- a/Lib/test/test_descrtut.py
+++ b/Lib/test/test_descrtut.py
@@ -199,6 +199,8 @@ You can get the information from the list type:
'__str__',
'__subclasshook__',
'append',
+ 'clear',
+ 'copy',
'count',
'extend',
'index',