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