Debian Codegraph @

lib-python/3/test/test_list.py

raw symbols SCIP
1 import sys
2 from test import list_tests
3 from test.support import cpython_only
4 import pickle
5 import unittest
6
7 class ListTest(list_tests.CommonTest):
8 type2test = list
9
10 def test_basic(self):
11 self.assertEqual(list([]), [])
12 l0_3 = [0, 1, 2, 3]
13 l0_3_bis = list(l0_3)
14 self.assertEqual(l0_3, l0_3_bis)
15 self.assertTrue(l0_3 is not l0_3_bis)
16 self.assertEqual(list(()), [])
17 self.assertEqual(list((0, 1, 2, 3)), [0, 1, 2, 3])
18 self.assertEqual(list(''), [])
19 self.assertEqual(list('spam'), ['s', 'p', 'a', 'm'])
20 self.assertEqual(list(x for x in range(10) if x % 2),
21 [1, 3, 5, 7, 9])
22
23 if sys.maxsize == 0x7fffffff:
24 # This test can currently only work on 32-bit machines.
25 # XXX If/when PySequence_Length() returns a ssize_t, it should be
26 # XXX re-enabled.
27 # Verify clearing of bug #556025.
28 # This assumes that the max data size (sys.maxint) == max
29 # address size this also assumes that the address size is at
30 # least 4 bytes with 8 byte addresses, the bug is not well
31 # tested
32 #
33 # Note: This test is expected to SEGV under Cygwin 1.3.12 or
34 # earlier due to a newlib bug. See the following mailing list
35 # thread for the details:
36
37 # http://sources.redhat.com/ml/newlib/2002/msg00369.html
38 self.assertRaises(MemoryError, list, range(sys.maxsize // 2))
39
40 # This code used to segfault in Py2.4a3
41 x = []
42 x.extend(-y for y in x)
43 self.assertEqual(x, [])
44
45 def test_keyword_args(self):
46 with self.assertRaisesRegex(TypeError, 'keyword argument'):
47 list(sequence=[])
48
49 def test_keywords_in_subclass(self):
50 class subclass(list):
51 pass
52 u = subclass([1, 2])
53 self.assertIs(type(u), subclass)
54 self.assertEqual(list(u), [1, 2])
55 with self.assertRaises(TypeError):
56 subclass(sequence=())
57
58 class subclass_with_init(list):
59 def __init__(self, seq, newarg=None):
60 super().__init__(seq)
61 self.newarg = newarg
62 u = subclass_with_init([1, 2], newarg=3)
63 self.assertIs(type(u), subclass_with_init)
64 self.assertEqual(list(u), [1, 2])
65 self.assertEqual(u.newarg, 3)
66
67 class subclass_with_new(list):
68 def __new__(cls, seq, newarg=None):
69 self = super().__new__(cls, seq)
70 self.newarg = newarg
71 return self
72 u = subclass_with_new([1, 2], newarg=3)
73 self.assertIs(type(u), subclass_with_new)
74 self.assertEqual(list(u), [1, 2])
75 self.assertEqual(u.newarg, 3)
76
77 def test_truth(self):
78 super().test_truth()
79 self.assertTrue(not [])
80 self.assertTrue([42])
81
82 def test_identity(self):
83 self.assertTrue([] is not [])
84
85 def test_len(self):
86 super().test_len()
87 self.assertEqual(len([]), 0)
88 self.assertEqual(len([0]), 1)
89 self.assertEqual(len([0, 1, 2]), 3)
90
91 def test_overflow(self):
92 lst = [4, 5, 6, 7]
93 n = int((sys.maxsize*2+2) // len(lst))
94 def mul(a, b): return a * b
95 def imul(a, b): a *= b
96 self.assertRaises((MemoryError, OverflowError), mul, lst, n)
97 self.assertRaises((MemoryError, OverflowError), imul, lst, n)
98
99 def test_list_resize_overflow(self):
100 # gh-97616: test new_allocated * sizeof(PyObject*) overflow
101 # check in list_resize()
102 lst = [0] * 65
103 del lst[1:]
104 self.assertEqual(len(lst), 1)
105
106 # No tuple.__itemsize__ on PyPy, the replacement is equivalent
107 # size = ((2 ** (tuple.__itemsize__ * 8) - 1) // 2)
108 size = sys.maxsize
109 with self.assertRaises((MemoryError, OverflowError)):
110 lst * size
111 with self.assertRaises((MemoryError, OverflowError)):
112 lst *= size
113
114 def test_repr_large(self):
115 # Check the repr of large list objects
116 def check(n):
117 l = [0] * n
118 s = repr(l)
119 self.assertEqual(s,
120 '[' + ', '.join(['0'] * n) + ']')
121 check(10) # check our checking code
122 check(1000000)
123
124 def test_iterator_pickle(self):
125 orig = self.type2test([4, 5, 6, 7])
126 data = [10, 11, 12, 13, 14, 15]
127 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
128 # initial iterator
129 itorig = iter(orig)
130 d = pickle.dumps((itorig, orig), proto)
131 it, a = pickle.loads(d)
132 a[:] = data
133 self.assertEqual(type(it), type(itorig))
134 self.assertEqual(list(it), data)
135
136 # running iterator
137 next(itorig)
138 d = pickle.dumps((itorig, orig), proto)
139 it, a = pickle.loads(d)
140 a[:] = data
141 self.assertEqual(type(it), type(itorig))
142 self.assertEqual(list(it), data[1:])
143
144 # empty iterator
145 for i in range(1, len(orig)):
146 next(itorig)
147 d = pickle.dumps((itorig, orig), proto)
148 it, a = pickle.loads(d)
149 a[:] = data
150 self.assertEqual(type(it), type(itorig))
151 self.assertEqual(list(it), data[len(orig):])
152
153 # exhausted iterator
154 self.assertRaises(StopIteration, next, itorig)
155 d = pickle.dumps((itorig, orig), proto)
156 it, a = pickle.loads(d)
157 a[:] = data
158 self.assertEqual(list(it), [])
159
160 def test_reversed_pickle(self):
161 orig = self.type2test([4, 5, 6, 7])
162 data = [10, 11, 12, 13, 14, 15]
163 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
164 # initial iterator
165 itorig = reversed(orig)
166 d = pickle.dumps((itorig, orig), proto)
167 it, a = pickle.loads(d)
168 a[:] = data
169 self.assertEqual(type(it), type(itorig))
170 self.assertEqual(list(it), data[len(orig)-1::-1])
171
172 # running iterator
173 next(itorig)
174 d = pickle.dumps((itorig, orig), proto)
175 it, a = pickle.loads(d)
176 a[:] = data
177 self.assertEqual(type(it), type(itorig))
178 self.assertEqual(list(it), data[len(orig)-2::-1])
179
180 # empty iterator
181 for i in range(1, len(orig)):
182 next(itorig)
183 d = pickle.dumps((itorig, orig), proto)
184 it, a = pickle.loads(d)
185 a[:] = data
186 self.assertEqual(type(it), type(itorig))
187 self.assertEqual(list(it), [])
188
189 # exhausted iterator
190 self.assertRaises(StopIteration, next, itorig)
191 d = pickle.dumps((itorig, orig), proto)
192 it, a = pickle.loads(d)
193 a[:] = data
194 self.assertEqual(list(it), [])
195
196 def test_step_overflow(self):
197 a = [0, 1, 2, 3, 4]
198 a[1::sys.maxsize] = [0]
199 self.assertEqual(a[3::sys.maxsize], [3])
200
201 def test_no_comdat_folding(self):
202 # Issue 8847: In the PGO build, the MSVC linker's COMDAT folding
203 # optimization causes failures in code that relies on distinct
204 # function addresses.
205 class L(list): pass
206 with self.assertRaises(TypeError):
207 (3,) + L([1,2])
208
209 @cpython_only
210 def test_equal_operator_modifying_operand(self):
211 # test fix for seg fault reported in bpo-38588 part 2.
212 class X:
213 def __eq__(self,other) :
214 list2.clear()
215 return NotImplemented
216
217 class Y:
218 def __eq__(self, other):
219 list1.clear()
220 return NotImplemented
221
222 class Z:
223 def __eq__(self, other):
224 list3.clear()
225 return NotImplemented
226
227 list1 = [X()]
228 list2 = [Y()]
229 self.assertTrue(list1 == list2)
230
231 list3 = [Z()]
232 list4 = [1]
233 self.assertFalse(list3 == list4)
234
235 def test_lt_operator_modifying_operand(self):
236 # See gh-120298
237 class evil:
238 def __lt__(self, other):
239 other.clear()
240 return NotImplemented
241
242 a = [[evil()]]
243 with self.assertRaises(TypeError):
244 a[0] < a
245
246 def test_list_index_modifing_operand(self):
247 # See gh-120384
248 class evil:
249 def __init__(self, lst):
250 self.lst = lst
251 def __iter__(self):
252 yield from self.lst
253 self.lst.clear()
254
255 lst = list(range(5))
256 operand = evil(lst)
257 with self.assertRaises(ValueError):
258 lst[::-1] = operand
259
260 @cpython_only
261 def test_preallocation(self):
262 iterable = [0] * 10
263 iter_size = sys.getsizeof(iterable)
264
265 self.assertEqual(iter_size, sys.getsizeof(list([0] * 10)))
266 self.assertEqual(iter_size, sys.getsizeof(list(range(10))))
267
268 def test_count_index_remove_crashes(self):
269 # bpo-38610: The count(), index(), and remove() methods were not
270 # holding strong references to list elements while calling
271 # PyObject_RichCompareBool().
272 class X:
273 def __eq__(self, other):
274 lst.clear()
275 return NotImplemented
276
277 lst = [X()]
278 with self.assertRaises(ValueError):
279 lst.index(lst)
280
281 class L(list):
282 def __eq__(self, other):
283 str(other)
284 return NotImplemented
285
286 lst = L([X()])
287 lst.count(lst)
288
289 lst = L([X()])
290 with self.assertRaises(ValueError):
291 lst.remove(lst)
292
293 # bpo-39453: list.__contains__ was not holding strong references
294 # to list elements while calling PyObject_RichCompareBool().
295 lst = [X(), X()]
296 3 in lst
297 lst = [X(), X()]
298 X() in lst
299
300
301 if __name__ == "__main__":
302 unittest.main()