1"""
2Tests common to list and UserList.UserList
3"""
4
5import sys
6import os
7from functools import cmp_to_key
8
9from test import support, seq_tests
10from test.support import ALWAYS_EQ, NEVER_EQ
11
12
13class CommonTest(seq_tests.CommonTest):
14
15    def test_init(self):
16        # Iterable arg is optional
17        self.assertEqual(self.type2test([]), self.type2test())
18
19        # Init clears previous values
20        a = self.type2test([1, 2, 3])
21        a.__init__()
22        self.assertEqual(a, self.type2test([]))
23
24        # Init overwrites previous values
25        a = self.type2test([1, 2, 3])
26        a.__init__([4, 5, 6])
27        self.assertEqual(a, self.type2test([4, 5, 6]))
28
29        # Mutables always return a new object
30        b = self.type2test(a)
31        self.assertNotEqual(id(a), id(b))
32        self.assertEqual(a, b)
33
34    def test_getitem_error(self):
35        a = []
36        msg = "list indices must be integers or slices"
37        with self.assertRaisesRegex(TypeError, msg):
38            a['a']
39
40    def test_setitem_error(self):
41        a = []
42        msg = "list indices must be integers or slices"
43        with self.assertRaisesRegex(TypeError, msg):
44            a['a'] = "python"
45
46    def test_repr(self):
47        l0 = []
48        l2 = [0, 1, 2]
49        a0 = self.type2test(l0)
50        a2 = self.type2test(l2)
51
52        self.assertEqual(str(a0), str(l0))
53        self.assertEqual(repr(a0), repr(l0))
54        self.assertEqual(repr(a2), repr(l2))
55        self.assertEqual(str(a2), "[0, 1, 2]")
56        self.assertEqual(repr(a2), "[0, 1, 2]")
57
58        a2.append(a2)
59        a2.append(3)
60        self.assertEqual(str(a2), "[0, 1, 2, [...], 3]")
61        self.assertEqual(repr(a2), "[0, 1, 2, [...], 3]")
62
63    def test_repr_deep(self):
64        a = self.type2test([])
65        for i in range(sys.getrecursionlimit() + 100):
66            a = self.type2test([a])
67        self.assertRaises(RecursionError, repr, a)
68
69    def test_print(self):
70        d = self.type2test(range(200))
71        d.append(d)
72        d.extend(range(200,400))
73        d.append(d)
74        d.append(400)
75        try:
76            with open(support.TESTFN, "w") as fo:
77                fo.write(str(d))
78            with open(support.TESTFN, "r") as fo:
79                self.assertEqual(fo.read(), repr(d))
80        finally:
81            os.remove(support.TESTFN)
82
83    def test_set_subscript(self):
84        a = self.type2test(range(20))
85        self.assertRaises(ValueError, a.__setitem__, slice(0, 10, 0), [1,2,3])
86        self.assertRaises(TypeError, a.__setitem__, slice(0, 10), 1)
87        self.assertRaises(ValueError, a.__setitem__, slice(0, 10, 2), [1,2])
88        self.assertRaises(TypeError, a.__getitem__, 'x', 1)
89        a[slice(2,10,3)] = [1,2,3]
90        self.assertEqual(a, self.type2test([0, 1, 1, 3, 4, 2, 6, 7, 3,
91                                            9, 10, 11, 12, 13, 14, 15,
92                                            16, 17, 18, 19]))
93
94    def test_reversed(self):
95        a = self.type2test(range(20))
96        r = reversed(a)
97        self.assertEqual(list(r), self.type2test(range(19, -1, -1)))
98        self.assertRaises(StopIteration, next, r)
99        self.assertEqual(list(reversed(self.type2test())),
100                         self.type2test())
101        # Bug 3689: make sure list-reversed-iterator doesn't have __len__
102        self.assertRaises(TypeError, len, reversed([1,2,3]))
103
104    def test_setitem(self):
105        a = self.type2test([0, 1])
106        a[0] = 0
107        a[1] = 100
108        self.assertEqual(a, self.type2test([0, 100]))
109        a[-1] = 200
110        self.assertEqual(a, self.type2test([0, 200]))
111        a[-2] = 100
112        self.assertEqual(a, self.type2test([100, 200]))
113        self.assertRaises(IndexError, a.__setitem__, -3, 200)
114        self.assertRaises(IndexError, a.__setitem__, 2, 200)
115
116        a = self.type2test([])
117        self.assertRaises(IndexError, a.__setitem__, 0, 200)
118        self.assertRaises(IndexError, a.__setitem__, -1, 200)
119        self.assertRaises(TypeError, a.__setitem__)
120
121        a = self.type2test([0,1,2,3,4])
122        a[0] = 1
123        a[1] = 2
124        a[2] = 3
125        self.assertEqual(a, self.type2test([1,2,3,3,4]))
126        a[0] = 5
127        a[1] = 6
128        a[2] = 7
129        self.assertEqual(a, self.type2test([5,6,7,3,4]))
130        a[-2] = 88
131        a[-1] = 99
132        self.assertEqual(a, self.type2test([5,6,7,88,99]))
133        a[-2] = 8
134        a[-1] = 9
135        self.assertEqual(a, self.type2test([5,6,7,8,9]))
136
137        msg = "list indices must be integers or slices"
138        with self.assertRaisesRegex(TypeError, msg):
139            a['a'] = "python"
140
141    def test_delitem(self):
142        a = self.type2test([0, 1])
143        del a[1]
144        self.assertEqual(a, [0])
145        del a[0]
146        self.assertEqual(a, [])
147
148        a = self.type2test([0, 1])
149        del a[-2]
150        self.assertEqual(a, [1])
151        del a[-1]
152        self.assertEqual(a, [])
153
154        a = self.type2test([0, 1])
155        self.assertRaises(IndexError, a.__delitem__, -3)
156        self.assertRaises(IndexError, a.__delitem__, 2)
157
158        a = self.type2test([])
159        self.assertRaises(IndexError, a.__delitem__, 0)
160
161        self.assertRaises(TypeError, a.__delitem__)
162
163    def test_setslice(self):
164        l = [0, 1]
165        a = self.type2test(l)
166
167        for i in range(-3, 4):
168            a[:i] = l[:i]
169            self.assertEqual(a, l)
170            a2 = a[:]
171            a2[:i] = a[:i]
172            self.assertEqual(a2, a)
173            a[i:] = l[i:]
174            self.assertEqual(a, l)
175            a2 = a[:]
176            a2[i:] = a[i:]
177            self.assertEqual(a2, a)
178            for j in range(-3, 4):
179                a[i:j] = l[i:j]
180                self.assertEqual(a, l)
181                a2 = a[:]
182                a2[i:j] = a[i:j]
183                self.assertEqual(a2, a)
184
185        aa2 = a2[:]
186        aa2[:0] = [-2, -1]
187        self.assertEqual(aa2, [-2, -1, 0, 1])
188        aa2[0:] = []
189        self.assertEqual(aa2, [])
190
191        a = self.type2test([1, 2, 3, 4, 5])
192        a[:-1] = a
193        self.assertEqual(a, self.type2test([1, 2, 3, 4, 5, 5]))
194        a = self.type2test([1, 2, 3, 4, 5])
195        a[1:] = a
196        self.assertEqual(a, self.type2test([1, 1, 2, 3, 4, 5]))
197        a = self.type2test([1, 2, 3, 4, 5])
198        a[1:-1] = a
199        self.assertEqual(a, self.type2test([1, 1, 2, 3, 4, 5, 5]))
200
201        a = self.type2test([])
202        a[:] = tuple(range(10))
203        self.assertEqual(a, self.type2test(range(10)))
204
205        self.assertRaises(TypeError, a.__setitem__, slice(0, 1, 5))
206
207        self.assertRaises(TypeError, a.__setitem__)
208
209    def test_delslice(self):
210        a = self.type2test([0, 1])
211        del a[1:2]
212        del a[0:1]
213        self.assertEqual(a, self.type2test([]))
214
215        a = self.type2test([0, 1])
216        del a[1:2]
217        del a[0:1]
218        self.assertEqual(a, self.type2test([]))
219
220        a = self.type2test([0, 1])
221        del a[-2:-1]
222        self.assertEqual(a, self.type2test([1]))
223
224        a = self.type2test([0, 1])
225        del a[-2:-1]
226        self.assertEqual(a, self.type2test([1]))
227
228        a = self.type2test([0, 1])
229        del a[1:]
230        del a[:1]
231        self.assertEqual(a, self.type2test([]))
232
233        a = self.type2test([0, 1])
234        del a[1:]
235        del a[:1]
236        self.assertEqual(a, self.type2test([]))
237
238        a = self.type2test([0, 1])
239        del a[-1:]
240        self.assertEqual(a, self.type2test([0]))
241
242        a = self.type2test([0, 1])
243        del a[-1:]
244        self.assertEqual(a, self.type2test([0]))
245
246        a = self.type2test([0, 1])
247        del a[:]
248        self.assertEqual(a, self.type2test([]))
249
250    def test_append(self):
251        a = self.type2test([])
252        a.append(0)
253        a.append(1)
254        a.append(2)
255        self.assertEqual(a, self.type2test([0, 1, 2]))
256
257        self.assertRaises(TypeError, a.append)
258
259    def test_extend(self):
260        a1 = self.type2test([0])
261        a2 = self.type2test((0, 1))
262        a = a1[:]
263        a.extend(a2)
264        self.assertEqual(a, a1 + a2)
265
266        a.extend(self.type2test([]))
267        self.assertEqual(a, a1 + a2)
268
269        a.extend(a)
270        self.assertEqual(a, self.type2test([0, 0, 1, 0, 0, 1]))
271
272        a = self.type2test("spam")
273        a.extend("eggs")
274        self.assertEqual(a, list("spameggs"))
275
276        self.assertRaises(TypeError, a.extend, None)
277        self.assertRaises(TypeError, a.extend)
278
279        # overflow test. issue1621
280        class CustomIter:
281            def __iter__(self):
282                return self
283            def __next__(self):
284                raise StopIteration
285            def __length_hint__(self):
286                return sys.maxsize
287        a = self.type2test([1,2,3,4])
288        a.extend(CustomIter())
289        self.assertEqual(a, [1,2,3,4])
290
291
292    def test_insert(self):
293        a = self.type2test([0, 1, 2])
294        a.insert(0, -2)
295        a.insert(1, -1)
296        a.insert(2, 0)
297        self.assertEqual(a, [-2, -1, 0, 0, 1, 2])
298
299        b = a[:]
300        b.insert(-2, "foo")
301        b.insert(-200, "left")
302        b.insert(200, "right")
303        self.assertEqual(b, self.type2test(["left",-2,-1,0,0,"foo",1,2,"right"]))
304
305        self.assertRaises(TypeError, a.insert)
306
307    def test_pop(self):
308        a = self.type2test([-1, 0, 1])
309        a.pop()
310        self.assertEqual(a, [-1, 0])
311        a.pop(0)
312        self.assertEqual(a, [0])
313        self.assertRaises(IndexError, a.pop, 5)
314        a.pop(0)
315        self.assertEqual(a, [])
316        self.assertRaises(IndexError, a.pop)
317        self.assertRaises(TypeError, a.pop, 42, 42)
318        a = self.type2test([0, 10, 20, 30, 40])
319
320    def test_remove(self):
321        a = self.type2test([0, 0, 1])
322        a.remove(1)
323        self.assertEqual(a, [0, 0])
324        a.remove(0)
325        self.assertEqual(a, [0])
326        a.remove(0)
327        self.assertEqual(a, [])
328
329        self.assertRaises(ValueError, a.remove, 0)
330
331        self.assertRaises(TypeError, a.remove)
332
333        a = self.type2test([1, 2])
334        self.assertRaises(ValueError, a.remove, NEVER_EQ)
335        self.assertEqual(a, [1, 2])
336        a.remove(ALWAYS_EQ)
337        self.assertEqual(a, [2])
338        a = self.type2test([ALWAYS_EQ])
339        a.remove(1)
340        self.assertEqual(a, [])
341        a = self.type2test([ALWAYS_EQ])
342        a.remove(NEVER_EQ)
343        self.assertEqual(a, [])
344        a = self.type2test([NEVER_EQ])
345        self.assertRaises(ValueError, a.remove, ALWAYS_EQ)
346
347        class BadExc(Exception):
348            pass
349
350        class BadCmp:
351            def __eq__(self, other):
352                if other == 2:
353                    raise BadExc()
354                return False
355
356        a = self.type2test([0, 1, 2, 3])
357        self.assertRaises(BadExc, a.remove, BadCmp())
358
359        class BadCmp2:
360            def __eq__(self, other):
361                raise BadExc()
362
363        d = self.type2test('abcdefghcij')
364        d.remove('c')
365        self.assertEqual(d, self.type2test('abdefghcij'))
366        d.remove('c')
367        self.assertEqual(d, self.type2test('abdefghij'))
368        self.assertRaises(ValueError, d.remove, 'c')
369        self.assertEqual(d, self.type2test('abdefghij'))
370
371        # Handle comparison errors
372        d = self.type2test(['a', 'b', BadCmp2(), 'c'])
373        e = self.type2test(d)
374        self.assertRaises(BadExc, d.remove, 'c')
375        for x, y in zip(d, e):
376            # verify that original order and values are retained.
377            self.assertIs(x, y)
378
379    def test_index(self):
380        super().test_index()
381        a = self.type2test([-2, -1, 0, 0, 1, 2])
382        a.remove(0)
383        self.assertRaises(ValueError, a.index, 2, 0, 4)
384        self.assertEqual(a, self.type2test([-2, -1, 0, 1, 2]))
385
386        # Test modifying the list during index's iteration
387        class EvilCmp:
388            def __init__(self, victim):
389                self.victim = victim
390            def __eq__(self, other):
391                del self.victim[:]
392                return False
393        a = self.type2test()
394        a[:] = [EvilCmp(a) for _ in range(100)]
395        # This used to seg fault before patch #1005778
396        self.assertRaises(ValueError, a.index, None)
397
398    def test_reverse(self):
399        u = self.type2test([-2, -1, 0, 1, 2])
400        u2 = u[:]
401        u.reverse()
402        self.assertEqual(u, [2, 1, 0, -1, -2])
403        u.reverse()
404        self.assertEqual(u, u2)
405
406        self.assertRaises(TypeError, u.reverse, 42)
407
408    def test_clear(self):
409        u = self.type2test([2, 3, 4])
410        u.clear()
411        self.assertEqual(u, [])
412
413        u = self.type2test([])
414        u.clear()
415        self.assertEqual(u, [])
416
417        u = self.type2test([])
418        u.append(1)
419        u.clear()
420        u.append(2)
421        self.assertEqual(u, [2])
422
423        self.assertRaises(TypeError, u.clear, None)
424
425    def test_copy(self):
426        u = self.type2test([1, 2, 3])
427        v = u.copy()
428        self.assertEqual(v, [1, 2, 3])
429
430        u = self.type2test([])
431        v = u.copy()
432        self.assertEqual(v, [])
433
434        # test that it's indeed a copy and not a reference
435        u = self.type2test(['a', 'b'])
436        v = u.copy()
437        v.append('i')
438        self.assertEqual(u, ['a', 'b'])
439        self.assertEqual(v, u + ['i'])
440
441        # test that it's a shallow, not a deep copy
442        u = self.type2test([1, 2, [3, 4], 5])
443        v = u.copy()
444        self.assertEqual(u, v)
445        self.assertIs(v[3], u[3])
446
447        self.assertRaises(TypeError, u.copy, None)
448
449    def test_sort(self):
450        u = self.type2test([1, 0])
451        u.sort()
452        self.assertEqual(u, [0, 1])
453
454        u = self.type2test([2,1,0,-1,-2])
455        u.sort()
456        self.assertEqual(u, self.type2test([-2,-1,0,1,2]))
457
458        self.assertRaises(TypeError, u.sort, 42, 42)
459
460        def revcmp(a, b):
461            if a == b:
462                return 0
463            elif a < b:
464                return 1
465            else: # a > b
466                return -1
467        u.sort(key=cmp_to_key(revcmp))
468        self.assertEqual(u, self.type2test([2,1,0,-1,-2]))
469
470        # The following dumps core in unpatched Python 1.5:
471        def myComparison(x,y):
472            xmod, ymod = x%3, y%7
473            if xmod == ymod:
474                return 0
475            elif xmod < ymod:
476                return -1
477            else: # xmod > ymod
478                return 1
479        z = self.type2test(range(12))
480        z.sort(key=cmp_to_key(myComparison))
481
482        self.assertRaises(TypeError, z.sort, 2)
483
484        def selfmodifyingComparison(x,y):
485            z.append(1)
486            if x == y:
487                return 0
488            elif x < y:
489                return -1
490            else: # x > y
491                return 1
492        self.assertRaises(ValueError, z.sort,
493                          key=cmp_to_key(selfmodifyingComparison))
494
495        self.assertRaises(TypeError, z.sort, 42, 42, 42, 42)
496
497    def test_slice(self):
498        u = self.type2test("spam")
499        u[:2] = "h"
500        self.assertEqual(u, list("ham"))
501
502    def test_iadd(self):
503        super().test_iadd()
504        u = self.type2test([0, 1])
505        u2 = u
506        u += [2, 3]
507        self.assertIs(u, u2)
508
509        u = self.type2test("spam")
510        u += "eggs"
511        self.assertEqual(u, self.type2test("spameggs"))
512
513        self.assertRaises(TypeError, u.__iadd__, None)
514
515    def test_imul(self):
516        super().test_imul()
517        s = self.type2test([])
518        oldid = id(s)
519        s *= 10
520        self.assertEqual(id(s), oldid)
521
522    def test_extendedslicing(self):
523        #  subscript
524        a = self.type2test([0,1,2,3,4])
525
526        #  deletion
527        del a[::2]
528        self.assertEqual(a, self.type2test([1,3]))
529        a = self.type2test(range(5))
530        del a[1::2]
531        self.assertEqual(a, self.type2test([0,2,4]))
532        a = self.type2test(range(5))
533        del a[1::-2]
534        self.assertEqual(a, self.type2test([0,2,3,4]))
535        a = self.type2test(range(10))
536        del a[::1000]
537        self.assertEqual(a, self.type2test([1, 2, 3, 4, 5, 6, 7, 8, 9]))
538        #  assignment
539        a = self.type2test(range(10))
540        a[::2] = [-1]*5
541        self.assertEqual(a, self.type2test([-1, 1, -1, 3, -1, 5, -1, 7, -1, 9]))
542        a = self.type2test(range(10))
543        a[::-4] = [10]*3
544        self.assertEqual(a, self.type2test([0, 10, 2, 3, 4, 10, 6, 7, 8 ,10]))
545        a = self.type2test(range(4))
546        a[::-1] = a
547        self.assertEqual(a, self.type2test([3, 2, 1, 0]))
548        a = self.type2test(range(10))
549        b = a[:]
550        c = a[:]
551        a[2:3] = self.type2test(["two", "elements"])
552        b[slice(2,3)] = self.type2test(["two", "elements"])
553        c[2:3:] = self.type2test(["two", "elements"])
554        self.assertEqual(a, b)
555        self.assertEqual(a, c)
556        a = self.type2test(range(10))
557        a[::2] = tuple(range(5))
558        self.assertEqual(a, self.type2test([0, 1, 1, 3, 2, 5, 3, 7, 4, 9]))
559        # test issue7788
560        a = self.type2test(range(10))
561        del a[9::1<<333]
562
563    def test_constructor_exception_handling(self):
564        # Bug #1242657
565        class F(object):
566            def __iter__(self):
567                raise KeyboardInterrupt
568        self.assertRaises(KeyboardInterrupt, list, F())
569
570    def test_exhausted_iterator(self):
571        a = self.type2test([1, 2, 3])
572        exhit = iter(a)
573        empit = iter(a)
574        for x in exhit:  # exhaust the iterator
575            next(empit)  # not exhausted
576        a.append(9)
577        self.assertEqual(list(exhit), [])
578        self.assertEqual(list(empit), [9])
579        self.assertEqual(a, self.type2test([1, 2, 3, 9]))
580