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