Debian Codegraph @

Lib/test/test_list.py

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