Changeset 391 for python/trunk/Lib/test/test_copy.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_copy.py
r2 r391 3 3 import copy 4 4 import copy_reg 5 import weakref 5 6 6 7 import unittest … … 12 13 13 14 def test_exceptions(self): 14 self.assert _(copy.Error is copy.error)15 self.assert _(issubclass(copy.Error, Exception))15 self.assertTrue(copy.Error is copy.error) 16 self.assertTrue(issubclass(copy.Error, Exception)) 16 17 17 18 # The copy() method … … 54 55 x = C() 55 56 y = copy.copy(x) 56 self.assert _(y is x)57 self.assertTrue(y is x) 57 58 58 59 def test_copy_reduce(self): … … 62 63 x = C() 63 64 y = copy.copy(x) 64 self.assert _(y is x)65 self.assertTrue(y is x) 65 66 66 67 def test_copy_cant(self): … … 86 87 NewStyle, xrange(10), Classic, max] 87 88 for x in tests: 88 self.assert _(copy.copy(x) is x, repr(x))89 self.assertTrue(copy.copy(x) is x, repr(x)) 89 90 90 91 def test_copy_list(self): … … 180 181 y = copy.deepcopy(x) 181 182 self.assertEqual(y, x) 182 self.assert _(y is not x)183 self.assert _(y[0] is not x[0])184 self.assert _(y[0] is y[1])183 self.assertTrue(y is not x) 184 self.assertTrue(y[0] is not x[0]) 185 self.assertTrue(y[0] is y[1]) 185 186 186 187 def test_deepcopy_issubclass(self): … … 227 228 x = C() 228 229 y = copy.deepcopy(x) 229 self.assert _(y is x)230 self.assertTrue(y is x) 230 231 231 232 def test_deepcopy_reduce(self): … … 235 236 x = C() 236 237 y = copy.deepcopy(x) 237 self.assert _(y is x)238 self.assertTrue(y is x) 238 239 239 240 def test_deepcopy_cant(self): … … 259 260 NewStyle, xrange(10), Classic, max] 260 261 for x in tests: 261 self.assert _(copy.deepcopy(x) is x, repr(x))262 self.assertTrue(copy.deepcopy(x) is x, repr(x)) 262 263 263 264 def test_deepcopy_list(self): … … 265 266 y = copy.deepcopy(x) 266 267 self.assertEqual(y, x) 267 self.assert _(x is not y)268 self.assert _(x[0] is not y[0])268 self.assertTrue(x is not y) 269 self.assertTrue(x[0] is not y[0]) 269 270 270 271 def test_deepcopy_reflexive_list(self): … … 273 274 y = copy.deepcopy(x) 274 275 self.assertRaises(RuntimeError, cmp, y, x) 275 self.assert _(y is not x)276 self.assert _(y[0] is y)276 self.assertTrue(y is not x) 277 self.assertTrue(y[0] is y) 277 278 self.assertEqual(len(y), 1) 278 279 … … 281 282 y = copy.deepcopy(x) 282 283 self.assertEqual(y, x) 283 self.assert _(x is not y)284 self.assert _(x[0] is not y[0])284 self.assertTrue(x is not y) 285 self.assertTrue(x[0] is not y[0]) 285 286 286 287 def test_deepcopy_reflexive_tuple(self): … … 289 290 y = copy.deepcopy(x) 290 291 self.assertRaises(RuntimeError, cmp, y, x) 291 self.assert _(y is not x)292 self.assert _(y[0] is not x[0])293 self.assert _(y[0][0] is y)292 self.assertTrue(y is not x) 293 self.assertTrue(y[0] is not x[0]) 294 self.assertTrue(y[0][0] is y) 294 295 295 296 def test_deepcopy_dict(self): … … 297 298 y = copy.deepcopy(x) 298 299 self.assertEqual(y, x) 299 self.assert _(x is not y)300 self.assert _(x["foo"] is not y["foo"])300 self.assertTrue(x is not y) 301 self.assertTrue(x["foo"] is not y["foo"]) 301 302 302 303 def test_deepcopy_reflexive_dict(self): … … 305 306 y = copy.deepcopy(x) 306 307 self.assertRaises(RuntimeError, cmp, y, x) 307 self.assert _(y is not x)308 self.assert _(y['foo'] is y)308 self.assertTrue(y is not x) 309 self.assertTrue(y['foo'] is y) 309 310 self.assertEqual(len(y), 1) 310 311 … … 313 314 x = 42 314 315 y = copy.deepcopy(x, memo) 315 self.assert _(memo[id(x)] is x)316 self.assertTrue(memo[id(x)] is x) 316 317 317 318 def test_deepcopy_inst_vanilla(self): … … 324 325 y = copy.deepcopy(x) 325 326 self.assertEqual(y, x) 326 self.assert _(y.foo is not x.foo)327 self.assertTrue(y.foo is not x.foo) 327 328 328 329 def test_deepcopy_inst_deepcopy(self): … … 337 338 y = copy.deepcopy(x) 338 339 self.assertEqual(y, x) 339 self.assert _(y is not x)340 self.assert _(y.foo is not x.foo)340 self.assertTrue(y is not x) 341 self.assertTrue(y.foo is not x.foo) 341 342 342 343 def test_deepcopy_inst_getinitargs(self): … … 351 352 y = copy.deepcopy(x) 352 353 self.assertEqual(y, x) 353 self.assert _(y is not x)354 self.assert _(y.foo is not x.foo)354 self.assertTrue(y is not x) 355 self.assertTrue(y.foo is not x.foo) 355 356 356 357 def test_deepcopy_inst_getstate(self): … … 365 366 y = copy.deepcopy(x) 366 367 self.assertEqual(y, x) 367 self.assert _(y is not x)368 self.assert _(y.foo is not x.foo)368 self.assertTrue(y is not x) 369 self.assertTrue(y.foo is not x.foo) 369 370 370 371 def test_deepcopy_inst_setstate(self): … … 379 380 y = copy.deepcopy(x) 380 381 self.assertEqual(y, x) 381 self.assert _(y is not x)382 self.assert _(y.foo is not x.foo)382 self.assertTrue(y is not x) 383 self.assertTrue(y.foo is not x.foo) 383 384 384 385 def test_deepcopy_inst_getstate_setstate(self): … … 395 396 y = copy.deepcopy(x) 396 397 self.assertEqual(y, x) 397 self.assert _(y is not x)398 self.assert _(y.foo is not x.foo)398 self.assertTrue(y is not x) 399 self.assertTrue(y.foo is not x.foo) 399 400 400 401 def test_deepcopy_reflexive_inst(self): … … 404 405 x.foo = x 405 406 y = copy.deepcopy(x) 406 self.assert _(y is not x)407 self.assert _(y.foo is y)407 self.assertTrue(y is not x) 408 self.assertTrue(y.foo is y) 408 409 409 410 # _reconstruct() … … 415 416 x = C() 416 417 y = copy.copy(x) 417 self.assert _(y is x)418 y = copy.deepcopy(x) 419 self.assert _(y is x)418 self.assertTrue(y is x) 419 y = copy.deepcopy(x) 420 self.assertTrue(y is x) 420 421 421 422 def test_reconstruct_nostate(self): … … 426 427 x.foo = 42 427 428 y = copy.copy(x) 428 self.assert _(y.__class__ is x.__class__)429 y = copy.deepcopy(x) 430 self.assert _(y.__class__ is x.__class__)429 self.assertTrue(y.__class__ is x.__class__) 430 y = copy.deepcopy(x) 431 self.assertTrue(y.__class__ is x.__class__) 431 432 432 433 def test_reconstruct_state(self): … … 443 444 y = copy.deepcopy(x) 444 445 self.assertEqual(y, x) 445 self.assert _(y.foo is not x.foo)446 self.assertTrue(y.foo is not x.foo) 446 447 447 448 def test_reconstruct_state_setstate(self): … … 460 461 y = copy.deepcopy(x) 461 462 self.assertEqual(y, x) 462 self.assert _(y.foo is not x.foo)463 self.assertTrue(y.foo is not x.foo) 463 464 464 465 def test_reconstruct_reflexive(self): … … 468 469 x.foo = x 469 470 y = copy.deepcopy(x) 470 self.assert _(y is not x)471 self.assert _(y.foo is y)471 self.assertTrue(y is not x) 472 self.assertTrue(y.foo is y) 472 473 473 474 # Additions for Python 2.3 and pickle protocol 2 … … 484 485 y = copy.copy(x) 485 486 self.assertEqual(x, y) 486 self.assert _(x is not y)487 self.assert _(x[0] is y[0])487 self.assertTrue(x is not y) 488 self.assertTrue(x[0] is y[0]) 488 489 y = copy.deepcopy(x) 489 490 self.assertEqual(x, y) 490 self.assert _(x is not y)491 self.assert _(x[0] is not y[0])491 self.assertTrue(x is not y) 492 self.assertTrue(x[0] is not y[0]) 492 493 493 494 def test_reduce_5tuple(self): … … 502 503 y = copy.copy(x) 503 504 self.assertEqual(x, y) 504 self.assert _(x is not y)505 self.assert _(x["foo"] is y["foo"])505 self.assertTrue(x is not y) 506 self.assertTrue(x["foo"] is y["foo"]) 506 507 y = copy.deepcopy(x) 507 508 self.assertEqual(x, y) 508 self.assert _(x is not y)509 self.assert _(x["foo"] is not y["foo"])509 self.assertTrue(x is not y) 510 self.assertTrue(x["foo"] is not y["foo"]) 510 511 511 512 def test_copy_slots(self): … … 515 516 x.foo = [42] 516 517 y = copy.copy(x) 517 self.assert _(x.foo is y.foo)518 self.assertTrue(x.foo is y.foo) 518 519 519 520 def test_deepcopy_slots(self): … … 524 525 y = copy.deepcopy(x) 525 526 self.assertEqual(x.foo, y.foo) 526 self.assert_(x.foo is not y.foo) 527 self.assertTrue(x.foo is not y.foo) 528 529 def test_deepcopy_dict_subclass(self): 530 class C(dict): 531 def __init__(self, d=None): 532 if not d: 533 d = {} 534 self._keys = list(d.keys()) 535 dict.__init__(self, d) 536 def __setitem__(self, key, item): 537 dict.__setitem__(self, key, item) 538 if key not in self._keys: 539 self._keys.append(key) 540 x = C(d={'foo':0}) 541 y = copy.deepcopy(x) 542 self.assertEqual(x, y) 543 self.assertEqual(x._keys, y._keys) 544 self.assertTrue(x is not y) 545 x['bar'] = 1 546 self.assertNotEqual(x, y) 547 self.assertNotEqual(x._keys, y._keys) 527 548 528 549 def test_copy_list_subclass(self): … … 534 555 self.assertEqual(list(x), list(y)) 535 556 self.assertEqual(x.foo, y.foo) 536 self.assert _(x[0] is y[0])537 self.assert _(x.foo is y.foo)557 self.assertTrue(x[0] is y[0]) 558 self.assertTrue(x.foo is y.foo) 538 559 539 560 def test_deepcopy_list_subclass(self): … … 545 566 self.assertEqual(list(x), list(y)) 546 567 self.assertEqual(x.foo, y.foo) 547 self.assert _(x[0] is not y[0])548 self.assert _(x.foo is not y.foo)568 self.assertTrue(x[0] is not y[0]) 569 self.assertTrue(x.foo is not y.foo) 549 570 550 571 def test_copy_tuple_subclass(self): … … 563 584 y = copy.deepcopy(x) 564 585 self.assertEqual(tuple(y), ([1, 2], 3)) 565 self.assert _(x is not y)566 self.assert _(x[0] is not y[0])586 self.assertTrue(x is not y) 587 self.assertTrue(x[0] is not y[0]) 567 588 568 589 def test_getstate_exc(self): … … 586 607 self.assertEqual(copy.deepcopy(bar), bar) 587 608 609 def _check_weakref(self, _copy): 610 class C(object): 611 pass 612 obj = C() 613 x = weakref.ref(obj) 614 y = _copy(x) 615 self.assertTrue(y is x) 616 del obj 617 y = _copy(x) 618 self.assertTrue(y is x) 619 620 def test_copy_weakref(self): 621 self._check_weakref(copy.copy) 622 623 def test_deepcopy_weakref(self): 624 self._check_weakref(copy.deepcopy) 625 626 def _check_copy_weakdict(self, _dicttype): 627 class C(object): 628 pass 629 a, b, c, d = [C() for i in xrange(4)] 630 u = _dicttype() 631 u[a] = b 632 u[c] = d 633 v = copy.copy(u) 634 self.assertFalse(v is u) 635 self.assertEqual(v, u) 636 self.assertEqual(v[a], b) 637 self.assertEqual(v[c], d) 638 self.assertEqual(len(v), 2) 639 del c, d 640 self.assertEqual(len(v), 1) 641 x, y = C(), C() 642 # The underlying containers are decoupled 643 v[x] = y 644 self.assertNotIn(x, u) 645 646 def test_copy_weakkeydict(self): 647 self._check_copy_weakdict(weakref.WeakKeyDictionary) 648 649 def test_copy_weakvaluedict(self): 650 self._check_copy_weakdict(weakref.WeakValueDictionary) 651 652 def test_deepcopy_weakkeydict(self): 653 class C(object): 654 def __init__(self, i): 655 self.i = i 656 a, b, c, d = [C(i) for i in xrange(4)] 657 u = weakref.WeakKeyDictionary() 658 u[a] = b 659 u[c] = d 660 # Keys aren't copied, values are 661 v = copy.deepcopy(u) 662 self.assertNotEqual(v, u) 663 self.assertEqual(len(v), 2) 664 self.assertFalse(v[a] is b) 665 self.assertFalse(v[c] is d) 666 self.assertEqual(v[a].i, b.i) 667 self.assertEqual(v[c].i, d.i) 668 del c 669 self.assertEqual(len(v), 1) 670 671 def test_deepcopy_weakvaluedict(self): 672 class C(object): 673 def __init__(self, i): 674 self.i = i 675 a, b, c, d = [C(i) for i in xrange(4)] 676 u = weakref.WeakValueDictionary() 677 u[a] = b 678 u[c] = d 679 # Keys are copied, values aren't 680 v = copy.deepcopy(u) 681 self.assertNotEqual(v, u) 682 self.assertEqual(len(v), 2) 683 (x, y), (z, t) = sorted(v.items(), key=lambda pair: pair[0].i) 684 self.assertFalse(x is a) 685 self.assertEqual(x.i, a.i) 686 self.assertTrue(y is b) 687 self.assertFalse(z is c) 688 self.assertEqual(z.i, c.i) 689 self.assertTrue(t is d) 690 del x, y, z, t 691 del d 692 self.assertEqual(len(v), 1) 693 694 def test_deepcopy_bound_method(self): 695 class Foo(object): 696 def m(self): 697 pass 698 f = Foo() 699 f.b = f.m 700 g = copy.deepcopy(f) 701 self.assertEqual(g.m, g.b) 702 self.assertTrue(g.b.im_self is g) 703 g.b() 704 705 588 706 def global_foo(x, y): return x+y 589 707
Note:
See TracChangeset
for help on using the changeset viewer.