Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_copy.py

    r2 r391  
    33import copy
    44import copy_reg
     5import weakref
    56
    67import unittest
     
    1213
    1314    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))
    1617
    1718    # The copy() method
     
    5455        x = C()
    5556        y = copy.copy(x)
    56         self.assert_(y is x)
     57        self.assertTrue(y is x)
    5758
    5859    def test_copy_reduce(self):
     
    6263        x = C()
    6364        y = copy.copy(x)
    64         self.assert_(y is x)
     65        self.assertTrue(y is x)
    6566
    6667    def test_copy_cant(self):
     
    8687                 NewStyle, xrange(10), Classic, max]
    8788        for x in tests:
    88             self.assert_(copy.copy(x) is x, repr(x))
     89            self.assertTrue(copy.copy(x) is x, repr(x))
    8990
    9091    def test_copy_list(self):
     
    180181        y = copy.deepcopy(x)
    181182        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])
    185186
    186187    def test_deepcopy_issubclass(self):
     
    227228        x = C()
    228229        y = copy.deepcopy(x)
    229         self.assert_(y is x)
     230        self.assertTrue(y is x)
    230231
    231232    def test_deepcopy_reduce(self):
     
    235236        x = C()
    236237        y = copy.deepcopy(x)
    237         self.assert_(y is x)
     238        self.assertTrue(y is x)
    238239
    239240    def test_deepcopy_cant(self):
     
    259260                 NewStyle, xrange(10), Classic, max]
    260261        for x in tests:
    261             self.assert_(copy.deepcopy(x) is x, repr(x))
     262            self.assertTrue(copy.deepcopy(x) is x, repr(x))
    262263
    263264    def test_deepcopy_list(self):
     
    265266        y = copy.deepcopy(x)
    266267        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])
    269270
    270271    def test_deepcopy_reflexive_list(self):
     
    273274        y = copy.deepcopy(x)
    274275        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)
    277278        self.assertEqual(len(y), 1)
    278279
     
    281282        y = copy.deepcopy(x)
    282283        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])
    285286
    286287    def test_deepcopy_reflexive_tuple(self):
     
    289290        y = copy.deepcopy(x)
    290291        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)
    294295
    295296    def test_deepcopy_dict(self):
     
    297298        y = copy.deepcopy(x)
    298299        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"])
    301302
    302303    def test_deepcopy_reflexive_dict(self):
     
    305306        y = copy.deepcopy(x)
    306307        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)
    309310        self.assertEqual(len(y), 1)
    310311
     
    313314        x = 42
    314315        y = copy.deepcopy(x, memo)
    315         self.assert_(memo[id(x)] is x)
     316        self.assertTrue(memo[id(x)] is x)
    316317
    317318    def test_deepcopy_inst_vanilla(self):
     
    324325        y = copy.deepcopy(x)
    325326        self.assertEqual(y, x)
    326         self.assert_(y.foo is not x.foo)
     327        self.assertTrue(y.foo is not x.foo)
    327328
    328329    def test_deepcopy_inst_deepcopy(self):
     
    337338        y = copy.deepcopy(x)
    338339        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)
    341342
    342343    def test_deepcopy_inst_getinitargs(self):
     
    351352        y = copy.deepcopy(x)
    352353        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)
    355356
    356357    def test_deepcopy_inst_getstate(self):
     
    365366        y = copy.deepcopy(x)
    366367        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)
    369370
    370371    def test_deepcopy_inst_setstate(self):
     
    379380        y = copy.deepcopy(x)
    380381        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)
    383384
    384385    def test_deepcopy_inst_getstate_setstate(self):
     
    395396        y = copy.deepcopy(x)
    396397        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)
    399400
    400401    def test_deepcopy_reflexive_inst(self):
     
    404405        x.foo = x
    405406        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)
    408409
    409410    # _reconstruct()
     
    415416        x = C()
    416417        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)
    420421
    421422    def test_reconstruct_nostate(self):
     
    426427        x.foo = 42
    427428        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__)
    431432
    432433    def test_reconstruct_state(self):
     
    443444        y = copy.deepcopy(x)
    444445        self.assertEqual(y, x)
    445         self.assert_(y.foo is not x.foo)
     446        self.assertTrue(y.foo is not x.foo)
    446447
    447448    def test_reconstruct_state_setstate(self):
     
    460461        y = copy.deepcopy(x)
    461462        self.assertEqual(y, x)
    462         self.assert_(y.foo is not x.foo)
     463        self.assertTrue(y.foo is not x.foo)
    463464
    464465    def test_reconstruct_reflexive(self):
     
    468469        x.foo = x
    469470        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)
    472473
    473474    # Additions for Python 2.3 and pickle protocol 2
     
    484485        y = copy.copy(x)
    485486        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])
    488489        y = copy.deepcopy(x)
    489490        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])
    492493
    493494    def test_reduce_5tuple(self):
     
    502503        y = copy.copy(x)
    503504        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"])
    506507        y = copy.deepcopy(x)
    507508        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"])
    510511
    511512    def test_copy_slots(self):
     
    515516        x.foo = [42]
    516517        y = copy.copy(x)
    517         self.assert_(x.foo is y.foo)
     518        self.assertTrue(x.foo is y.foo)
    518519
    519520    def test_deepcopy_slots(self):
     
    524525        y = copy.deepcopy(x)
    525526        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)
    527548
    528549    def test_copy_list_subclass(self):
     
    534555        self.assertEqual(list(x), list(y))
    535556        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)
    538559
    539560    def test_deepcopy_list_subclass(self):
     
    545566        self.assertEqual(list(x), list(y))
    546567        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)
    549570
    550571    def test_copy_tuple_subclass(self):
     
    563584        y = copy.deepcopy(x)
    564585        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])
    567588
    568589    def test_getstate_exc(self):
     
    586607        self.assertEqual(copy.deepcopy(bar), bar)
    587608
     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
    588706def global_foo(x, y): return x+y
    589707
Note: See TracChangeset for help on using the changeset viewer.