PageRenderTime 215ms CodeModel.GetById 41ms app.highlight 139ms RepoModel.GetById 21ms app.codeStats 1ms

/Lib/test/test_weakref.py

http://unladen-swallow.googlecode.com/
Python | 1227 lines | 880 code | 173 blank | 174 comment | 48 complexity | bbbf9b9e02f53255a9be72e4f244359d MD5 | raw file
   1import gc
   2import sys
   3import unittest
   4import UserList
   5import weakref
   6import operator
   7
   8from test import test_support
   9
  10# Used in ReferencesTestCase.test_ref_created_during_del() .
  11ref_from_del = None
  12
  13class C:
  14    def method(self):
  15        pass
  16
  17
  18class Callable:
  19    bar = None
  20
  21    def __call__(self, x):
  22        self.bar = x
  23
  24
  25def create_function():
  26    def f(): pass
  27    return f
  28
  29def create_bound_method():
  30    return C().method
  31
  32def create_unbound_method():
  33    return C.method
  34
  35
  36class TestBase(unittest.TestCase):
  37
  38    def setUp(self):
  39        self.cbcalled = 0
  40
  41    def callback(self, ref):
  42        self.cbcalled += 1
  43
  44
  45class ReferencesTestCase(TestBase):
  46
  47    def test_basic_ref(self):
  48        self.check_basic_ref(C)
  49        self.check_basic_ref(create_function)
  50        self.check_basic_ref(create_bound_method)
  51        self.check_basic_ref(create_unbound_method)
  52
  53        # Just make sure the tp_repr handler doesn't raise an exception.
  54        # Live reference:
  55        o = C()
  56        wr = weakref.ref(o)
  57        `wr`
  58        # Dead reference:
  59        del o
  60        `wr`
  61
  62    def test_basic_callback(self):
  63        self.check_basic_callback(C)
  64        self.check_basic_callback(create_function)
  65        self.check_basic_callback(create_bound_method)
  66        self.check_basic_callback(create_unbound_method)
  67
  68    def test_multiple_callbacks(self):
  69        o = C()
  70        ref1 = weakref.ref(o, self.callback)
  71        ref2 = weakref.ref(o, self.callback)
  72        del o
  73        self.assert_(ref1() is None,
  74                     "expected reference to be invalidated")
  75        self.assert_(ref2() is None,
  76                     "expected reference to be invalidated")
  77        self.assert_(self.cbcalled == 2,
  78                     "callback not called the right number of times")
  79
  80    def test_multiple_selfref_callbacks(self):
  81        # Make sure all references are invalidated before callbacks are called
  82        #
  83        # What's important here is that we're using the first
  84        # reference in the callback invoked on the second reference
  85        # (the most recently created ref is cleaned up first).  This
  86        # tests that all references to the object are invalidated
  87        # before any of the callbacks are invoked, so that we only
  88        # have one invocation of _weakref.c:cleanup_helper() active
  89        # for a particular object at a time.
  90        #
  91        def callback(object, self=self):
  92            self.ref()
  93        c = C()
  94        self.ref = weakref.ref(c, callback)
  95        ref1 = weakref.ref(c, callback)
  96        del c
  97
  98    def test_proxy_ref(self):
  99        o = C()
 100        o.bar = 1
 101        ref1 = weakref.proxy(o, self.callback)
 102        ref2 = weakref.proxy(o, self.callback)
 103        del o
 104
 105        def check(proxy):
 106            proxy.bar
 107
 108        self.assertRaises(weakref.ReferenceError, check, ref1)
 109        self.assertRaises(weakref.ReferenceError, check, ref2)
 110        self.assertRaises(weakref.ReferenceError, bool, weakref.proxy(C()))
 111        self.assert_(self.cbcalled == 2)
 112
 113    def check_basic_ref(self, factory):
 114        o = factory()
 115        ref = weakref.ref(o)
 116        self.assert_(ref() is not None,
 117                     "weak reference to live object should be live")
 118        o2 = ref()
 119        self.assert_(o is o2,
 120                     "<ref>() should return original object if live")
 121
 122    def check_basic_callback(self, factory):
 123        self.cbcalled = 0
 124        o = factory()
 125        ref = weakref.ref(o, self.callback)
 126        del o
 127        self.assert_(self.cbcalled == 1,
 128                     "callback did not properly set 'cbcalled'")
 129        self.assert_(ref() is None,
 130                     "ref2 should be dead after deleting object reference")
 131
 132    def test_ref_reuse(self):
 133        o = C()
 134        ref1 = weakref.ref(o)
 135        # create a proxy to make sure that there's an intervening creation
 136        # between these two; it should make no difference
 137        proxy = weakref.proxy(o)
 138        ref2 = weakref.ref(o)
 139        self.assert_(ref1 is ref2,
 140                     "reference object w/out callback should be re-used")
 141
 142        o = C()
 143        proxy = weakref.proxy(o)
 144        ref1 = weakref.ref(o)
 145        ref2 = weakref.ref(o)
 146        self.assert_(ref1 is ref2,
 147                     "reference object w/out callback should be re-used")
 148        self.assert_(weakref.getweakrefcount(o) == 2,
 149                     "wrong weak ref count for object")
 150        del proxy
 151        self.assert_(weakref.getweakrefcount(o) == 1,
 152                     "wrong weak ref count for object after deleting proxy")
 153
 154    def test_proxy_reuse(self):
 155        o = C()
 156        proxy1 = weakref.proxy(o)
 157        ref = weakref.ref(o)
 158        proxy2 = weakref.proxy(o)
 159        self.assert_(proxy1 is proxy2,
 160                     "proxy object w/out callback should have been re-used")
 161
 162    def test_basic_proxy(self):
 163        o = C()
 164        self.check_proxy(o, weakref.proxy(o))
 165
 166        L = UserList.UserList()
 167        p = weakref.proxy(L)
 168        self.failIf(p, "proxy for empty UserList should be false")
 169        p.append(12)
 170        self.assertEqual(len(L), 1)
 171        self.failUnless(p, "proxy for non-empty UserList should be true")
 172        p[:] = [2, 3]
 173        self.assertEqual(len(L), 2)
 174        self.assertEqual(len(p), 2)
 175        self.failUnless(3 in p,
 176                        "proxy didn't support __contains__() properly")
 177        p[1] = 5
 178        self.assertEqual(L[1], 5)
 179        self.assertEqual(p[1], 5)
 180        L2 = UserList.UserList(L)
 181        p2 = weakref.proxy(L2)
 182        self.assertEqual(p, p2)
 183        ## self.assertEqual(repr(L2), repr(p2))
 184        L3 = UserList.UserList(range(10))
 185        p3 = weakref.proxy(L3)
 186        self.assertEqual(L3[:], p3[:])
 187        self.assertEqual(L3[5:], p3[5:])
 188        self.assertEqual(L3[:5], p3[:5])
 189        self.assertEqual(L3[2:5], p3[2:5])
 190
 191    def test_proxy_index(self):
 192        class C:
 193            def __index__(self):
 194                return 10
 195        o = C()
 196        p = weakref.proxy(o)
 197        self.assertEqual(operator.index(p), 10)
 198
 199    def test_proxy_div(self):
 200        class C:
 201            def __floordiv__(self, other):
 202                return 42
 203            def __ifloordiv__(self, other):
 204                return 21
 205        o = C()
 206        p = weakref.proxy(o)
 207        self.assertEqual(p // 5, 42)
 208        p //= 5
 209        self.assertEqual(p, 21)
 210
 211    # The PyWeakref_* C API is documented as allowing either NULL or
 212    # None as the value for the callback, where either means "no
 213    # callback".  The "no callback" ref and proxy objects are supposed
 214    # to be shared so long as they exist by all callers so long as
 215    # they are active.  In Python 2.3.3 and earlier, this guarantee
 216    # was not honored, and was broken in different ways for
 217    # PyWeakref_NewRef() and PyWeakref_NewProxy().  (Two tests.)
 218
 219    def test_shared_ref_without_callback(self):
 220        self.check_shared_without_callback(weakref.ref)
 221
 222    def test_shared_proxy_without_callback(self):
 223        self.check_shared_without_callback(weakref.proxy)
 224
 225    def check_shared_without_callback(self, makeref):
 226        o = Object(1)
 227        p1 = makeref(o, None)
 228        p2 = makeref(o, None)
 229        self.assert_(p1 is p2, "both callbacks were None in the C API")
 230        del p1, p2
 231        p1 = makeref(o)
 232        p2 = makeref(o, None)
 233        self.assert_(p1 is p2, "callbacks were NULL, None in the C API")
 234        del p1, p2
 235        p1 = makeref(o)
 236        p2 = makeref(o)
 237        self.assert_(p1 is p2, "both callbacks were NULL in the C API")
 238        del p1, p2
 239        p1 = makeref(o, None)
 240        p2 = makeref(o)
 241        self.assert_(p1 is p2, "callbacks were None, NULL in the C API")
 242
 243    def test_callable_proxy(self):
 244        o = Callable()
 245        ref1 = weakref.proxy(o)
 246
 247        self.check_proxy(o, ref1)
 248
 249        self.assert_(type(ref1) is weakref.CallableProxyType,
 250                     "proxy is not of callable type")
 251        ref1('twinkies!')
 252        self.assert_(o.bar == 'twinkies!',
 253                     "call through proxy not passed through to original")
 254        ref1(x='Splat.')
 255        self.assert_(o.bar == 'Splat.',
 256                     "call through proxy not passed through to original")
 257
 258        # expect due to too few args
 259        self.assertRaises(TypeError, ref1)
 260
 261        # expect due to too many args
 262        self.assertRaises(TypeError, ref1, 1, 2, 3)
 263
 264    def check_proxy(self, o, proxy):
 265        o.foo = 1
 266        self.assert_(proxy.foo == 1,
 267                     "proxy does not reflect attribute addition")
 268        o.foo = 2
 269        self.assert_(proxy.foo == 2,
 270                     "proxy does not reflect attribute modification")
 271        del o.foo
 272        self.assert_(not hasattr(proxy, 'foo'),
 273                     "proxy does not reflect attribute removal")
 274
 275        proxy.foo = 1
 276        self.assert_(o.foo == 1,
 277                     "object does not reflect attribute addition via proxy")
 278        proxy.foo = 2
 279        self.assert_(
 280            o.foo == 2,
 281            "object does not reflect attribute modification via proxy")
 282        del proxy.foo
 283        self.assert_(not hasattr(o, 'foo'),
 284                     "object does not reflect attribute removal via proxy")
 285
 286    def test_proxy_deletion(self):
 287        # Test clearing of SF bug #762891
 288        class Foo:
 289            result = None
 290            def __delitem__(self, accessor):
 291                self.result = accessor
 292        g = Foo()
 293        f = weakref.proxy(g)
 294        del f[0]
 295        self.assertEqual(f.result, 0)
 296
 297    def test_proxy_bool(self):
 298        # Test clearing of SF bug #1170766
 299        class List(list): pass
 300        lyst = List()
 301        self.assertEqual(bool(weakref.proxy(lyst)), bool(lyst))
 302
 303    def test_getweakrefcount(self):
 304        o = C()
 305        ref1 = weakref.ref(o)
 306        ref2 = weakref.ref(o, self.callback)
 307        self.assert_(weakref.getweakrefcount(o) == 2,
 308                     "got wrong number of weak reference objects")
 309
 310        proxy1 = weakref.proxy(o)
 311        proxy2 = weakref.proxy(o, self.callback)
 312        self.assert_(weakref.getweakrefcount(o) == 4,
 313                     "got wrong number of weak reference objects")
 314
 315        del ref1, ref2, proxy1, proxy2
 316        self.assert_(weakref.getweakrefcount(o) == 0,
 317                     "weak reference objects not unlinked from"
 318                     " referent when discarded.")
 319
 320        # assumes ints do not support weakrefs
 321        self.assert_(weakref.getweakrefcount(1) == 0,
 322                     "got wrong number of weak reference objects for int")
 323
 324    def test_getweakrefs(self):
 325        o = C()
 326        ref1 = weakref.ref(o, self.callback)
 327        ref2 = weakref.ref(o, self.callback)
 328        del ref1
 329        self.assert_(weakref.getweakrefs(o) == [ref2],
 330                     "list of refs does not match")
 331
 332        o = C()
 333        ref1 = weakref.ref(o, self.callback)
 334        ref2 = weakref.ref(o, self.callback)
 335        del ref2
 336        self.assert_(weakref.getweakrefs(o) == [ref1],
 337                     "list of refs does not match")
 338
 339        del ref1
 340        self.assert_(weakref.getweakrefs(o) == [],
 341                     "list of refs not cleared")
 342
 343        # assumes ints do not support weakrefs
 344        self.assert_(weakref.getweakrefs(1) == [],
 345                     "list of refs does not match for int")
 346
 347    def test_newstyle_number_ops(self):
 348        class F(float):
 349            pass
 350        f = F(2.0)
 351        p = weakref.proxy(f)
 352        self.assert_(p + 1.0 == 3.0)
 353        self.assert_(1.0 + p == 3.0)  # this used to SEGV
 354
 355    def test_callbacks_protected(self):
 356        # Callbacks protected from already-set exceptions?
 357        # Regression test for SF bug #478534.
 358        class BogusError(Exception):
 359            pass
 360        data = {}
 361        def remove(k):
 362            del data[k]
 363        def encapsulate():
 364            f = lambda : ()
 365            data[weakref.ref(f, remove)] = None
 366            raise BogusError
 367        try:
 368            encapsulate()
 369        except BogusError:
 370            pass
 371        else:
 372            self.fail("exception not properly restored")
 373        try:
 374            encapsulate()
 375        except BogusError:
 376            pass
 377        else:
 378            self.fail("exception not properly restored")
 379
 380    def test_sf_bug_840829(self):
 381        # "weakref callbacks and gc corrupt memory"
 382        # subtype_dealloc erroneously exposed a new-style instance
 383        # already in the process of getting deallocated to gc,
 384        # causing double-deallocation if the instance had a weakref
 385        # callback that triggered gc.
 386        # If the bug exists, there probably won't be an obvious symptom
 387        # in a release build.  In a debug build, a segfault will occur
 388        # when the second attempt to remove the instance from the "list
 389        # of all objects" occurs.
 390
 391        import gc
 392
 393        class C(object):
 394            pass
 395
 396        c = C()
 397        wr = weakref.ref(c, lambda ignore: gc.collect())
 398        del c
 399
 400        # There endeth the first part.  It gets worse.
 401        del wr
 402
 403        c1 = C()
 404        c1.i = C()
 405        wr = weakref.ref(c1.i, lambda ignore: gc.collect())
 406
 407        c2 = C()
 408        c2.c1 = c1
 409        del c1  # still alive because c2 points to it
 410
 411        # Now when subtype_dealloc gets called on c2, it's not enough just
 412        # that c2 is immune from gc while the weakref callbacks associated
 413        # with c2 execute (there are none in this 2nd half of the test, btw).
 414        # subtype_dealloc goes on to call the base classes' deallocs too,
 415        # so any gc triggered by weakref callbacks associated with anything
 416        # torn down by a base class dealloc can also trigger double
 417        # deallocation of c2.
 418        del c2
 419
 420    def test_callback_in_cycle_1(self):
 421        import gc
 422
 423        class J(object):
 424            pass
 425
 426        class II(object):
 427            def acallback(self, ignore):
 428                self.J
 429
 430        I = II()
 431        I.J = J
 432        I.wr = weakref.ref(J, I.acallback)
 433
 434        # Now J and II are each in a self-cycle (as all new-style class
 435        # objects are, since their __mro__ points back to them).  I holds
 436        # both a weak reference (I.wr) and a strong reference (I.J) to class
 437        # J.  I is also in a cycle (I.wr points to a weakref that references
 438        # I.acallback).  When we del these three, they all become trash, but
 439        # the cycles prevent any of them from getting cleaned up immediately.
 440        # Instead they have to wait for cyclic gc to deduce that they're
 441        # trash.
 442        #
 443        # gc used to call tp_clear on all of them, and the order in which
 444        # it does that is pretty accidental.  The exact order in which we
 445        # built up these things manages to provoke gc into running tp_clear
 446        # in just the right order (I last).  Calling tp_clear on II leaves
 447        # behind an insane class object (its __mro__ becomes NULL).  Calling
 448        # tp_clear on J breaks its self-cycle, but J doesn't get deleted
 449        # just then because of the strong reference from I.J.  Calling
 450        # tp_clear on I starts to clear I's __dict__, and just happens to
 451        # clear I.J first -- I.wr is still intact.  That removes the last
 452        # reference to J, which triggers the weakref callback.  The callback
 453        # tries to do "self.J", and instances of new-style classes look up
 454        # attributes ("J") in the class dict first.  The class (II) wants to
 455        # search II.__mro__, but that's NULL.   The result was a segfault in
 456        # a release build, and an assert failure in a debug build.
 457        del I, J, II
 458        gc.collect()
 459
 460    def test_callback_in_cycle_2(self):
 461        import gc
 462
 463        # This is just like test_callback_in_cycle_1, except that II is an
 464        # old-style class.  The symptom is different then:  an instance of an
 465        # old-style class looks in its own __dict__ first.  'J' happens to
 466        # get cleared from I.__dict__ before 'wr', and 'J' was never in II's
 467        # __dict__, so the attribute isn't found.  The difference is that
 468        # the old-style II doesn't have a NULL __mro__ (it doesn't have any
 469        # __mro__), so no segfault occurs.  Instead it got:
 470        #    test_callback_in_cycle_2 (__main__.ReferencesTestCase) ...
 471        #    Exception exceptions.AttributeError:
 472        #   "II instance has no attribute 'J'" in <bound method II.acallback
 473        #       of <?.II instance at 0x00B9B4B8>> ignored
 474
 475        class J(object):
 476            pass
 477
 478        class II:
 479            def acallback(self, ignore):
 480                self.J
 481
 482        I = II()
 483        I.J = J
 484        I.wr = weakref.ref(J, I.acallback)
 485
 486        del I, J, II
 487        gc.collect()
 488
 489    def test_callback_in_cycle_3(self):
 490        import gc
 491
 492        # This one broke the first patch that fixed the last two.  In this
 493        # case, the objects reachable from the callback aren't also reachable
 494        # from the object (c1) *triggering* the callback:  you can get to
 495        # c1 from c2, but not vice-versa.  The result was that c2's __dict__
 496        # got tp_clear'ed by the time the c2.cb callback got invoked.
 497
 498        class C:
 499            def cb(self, ignore):
 500                self.me
 501                self.c1
 502                self.wr
 503
 504        c1, c2 = C(), C()
 505
 506        c2.me = c2
 507        c2.c1 = c1
 508        c2.wr = weakref.ref(c1, c2.cb)
 509
 510        del c1, c2
 511        gc.collect()
 512
 513    def test_callback_in_cycle_4(self):
 514        import gc
 515
 516        # Like test_callback_in_cycle_3, except c2 and c1 have different
 517        # classes.  c2's class (C) isn't reachable from c1 then, so protecting
 518        # objects reachable from the dying object (c1) isn't enough to stop
 519        # c2's class (C) from getting tp_clear'ed before c2.cb is invoked.
 520        # The result was a segfault (C.__mro__ was NULL when the callback
 521        # tried to look up self.me).
 522
 523        class C(object):
 524            def cb(self, ignore):
 525                self.me
 526                self.c1
 527                self.wr
 528
 529        class D:
 530            pass
 531
 532        c1, c2 = D(), C()
 533
 534        c2.me = c2
 535        c2.c1 = c1
 536        c2.wr = weakref.ref(c1, c2.cb)
 537
 538        del c1, c2, C, D
 539        gc.collect()
 540
 541    def test_callback_in_cycle_resurrection(self):
 542        import gc
 543
 544        # Do something nasty in a weakref callback:  resurrect objects
 545        # from dead cycles.  For this to be attempted, the weakref and
 546        # its callback must also be part of the cyclic trash (else the
 547        # objects reachable via the callback couldn't be in cyclic trash
 548        # to begin with -- the callback would act like an external root).
 549        # But gc clears trash weakrefs with callbacks early now, which
 550        # disables the callbacks, so the callbacks shouldn't get called
 551        # at all (and so nothing actually gets resurrected).
 552
 553        alist = []
 554        class C(object):
 555            def __init__(self, value):
 556                self.attribute = value
 557
 558            def acallback(self, ignore):
 559                alist.append(self.c)
 560
 561        common = C(0)
 562        c1, c2 = C(common), C(common)
 563        c1.c = c2
 564        c2.c = c1
 565        c1.wr = weakref.ref(c2, c1.acallback)
 566        c2.wr = weakref.ref(c1, c2.acallback)
 567
 568        def common_went_away(ignore):
 569            alist.append("common went away")
 570        wr = weakref.ref(common, common_went_away)
 571
 572        del c1, c2, common   # make them all trash
 573        self.assertEqual(alist, [])  # del isn't enough to reclaim anything
 574
 575        gc.collect()
 576        # c1.wr and c2.wr were part of the cyclic trash, so should have
 577        # been cleared without their callbacks executing.  OTOH, the weakref
 578        # to common is bound to a function local (wr), and wasn't trash, so that
 579        # callback should have been invoked when common went away.
 580        self.assertEqual(alist, ["common went away"])
 581        # The remaining weakref should be dead now (its callback ran).
 582        self.assertEqual(wr(), None)
 583
 584        del alist[:]
 585        gc.collect()
 586        self.assertEqual(alist, [])
 587
 588    def test_callbacks_on_callback(self):
 589        import gc
 590
 591        # Set up weakref callbacks *on* weakref callbacks.
 592        alist = []
 593        def safe_callback(ignore):
 594            alist.append("safe_callback called")
 595
 596        class C(object):
 597            def cb(self, ignore):
 598                alist.append("cb called")
 599
 600        c, d = C(), C()
 601        c.other = d
 602        d.other = c
 603        callback = c.cb
 604        c.wr = weakref.ref(d, callback)     # this won't trigger
 605        d.wr = weakref.ref(callback, d.cb)  # ditto
 606        external_wr = weakref.ref(callback, safe_callback)  # but this will
 607        self.assert_(external_wr() is callback)
 608
 609        # The weakrefs attached to c and d should get cleared, so that
 610        # C.cb is never called.  But external_wr isn't part of the cyclic
 611        # trash, and no cyclic trash is reachable from it, so safe_callback
 612        # should get invoked when the bound method object callback (c.cb)
 613        # -- which is itself a callback, and also part of the cyclic trash --
 614        # gets reclaimed at the end of gc.
 615
 616        del callback, c, d, C
 617        self.assertEqual(alist, [])  # del isn't enough to clean up cycles
 618        gc.collect()
 619        self.assertEqual(alist, ["safe_callback called"])
 620        self.assertEqual(external_wr(), None)
 621
 622        del alist[:]
 623        gc.collect()
 624        self.assertEqual(alist, [])
 625
 626    def test_gc_during_ref_creation(self):
 627        self.check_gc_during_creation(weakref.ref)
 628
 629    def test_gc_during_proxy_creation(self):
 630        self.check_gc_during_creation(weakref.proxy)
 631
 632    def check_gc_during_creation(self, makeref):
 633        thresholds = gc.get_threshold()
 634        gc.set_threshold(1, 1, 1)
 635        gc.collect()
 636        class A:
 637            pass
 638
 639        def callback(*args):
 640            pass
 641
 642        referenced = A()
 643
 644        a = A()
 645        a.a = a
 646        a.wr = makeref(referenced)
 647
 648        try:
 649            # now make sure the object and the ref get labeled as
 650            # cyclic trash:
 651            a = A()
 652            weakref.ref(referenced, callback)
 653
 654        finally:
 655            gc.set_threshold(*thresholds)
 656
 657    def test_ref_created_during_del(self):
 658        # Bug #1377858
 659        # A weakref created in an object's __del__() would crash the
 660        # interpreter when the weakref was cleaned up since it would refer to
 661        # non-existent memory.  This test should not segfault the interpreter.
 662        class Target(object):
 663            def __del__(self):
 664                global ref_from_del
 665                ref_from_del = weakref.ref(self)
 666
 667        w = Target()
 668
 669    def test_init(self):
 670        # Issue 3634
 671        # <weakref to class>.__init__() doesn't check errors correctly
 672        r = weakref.ref(Exception)
 673        self.assertRaises(TypeError, r.__init__, 0, 0, 0, 0, 0)
 674        # No exception should be raised here
 675        gc.collect()
 676
 677
 678class SubclassableWeakrefTestCase(TestBase):
 679
 680    def test_subclass_refs(self):
 681        class MyRef(weakref.ref):
 682            def __init__(self, ob, callback=None, value=42):
 683                self.value = value
 684                super(MyRef, self).__init__(ob, callback)
 685            def __call__(self):
 686                self.called = True
 687                return super(MyRef, self).__call__()
 688        o = Object("foo")
 689        mr = MyRef(o, value=24)
 690        self.assert_(mr() is o)
 691        self.assert_(mr.called)
 692        self.assertEqual(mr.value, 24)
 693        del o
 694        self.assert_(mr() is None)
 695        self.assert_(mr.called)
 696
 697    def test_subclass_refs_dont_replace_standard_refs(self):
 698        class MyRef(weakref.ref):
 699            pass
 700        o = Object(42)
 701        r1 = MyRef(o)
 702        r2 = weakref.ref(o)
 703        self.assert_(r1 is not r2)
 704        self.assertEqual(weakref.getweakrefs(o), [r2, r1])
 705        self.assertEqual(weakref.getweakrefcount(o), 2)
 706        r3 = MyRef(o)
 707        self.assertEqual(weakref.getweakrefcount(o), 3)
 708        refs = weakref.getweakrefs(o)
 709        self.assertEqual(len(refs), 3)
 710        self.assert_(r2 is refs[0])
 711        self.assert_(r1 in refs[1:])
 712        self.assert_(r3 in refs[1:])
 713
 714    def test_subclass_refs_dont_conflate_callbacks(self):
 715        class MyRef(weakref.ref):
 716            pass
 717        o = Object(42)
 718        r1 = MyRef(o, id)
 719        r2 = MyRef(o, str)
 720        self.assert_(r1 is not r2)
 721        refs = weakref.getweakrefs(o)
 722        self.assert_(r1 in refs)
 723        self.assert_(r2 in refs)
 724
 725    def test_subclass_refs_with_slots(self):
 726        class MyRef(weakref.ref):
 727            __slots__ = "slot1", "slot2"
 728            def __new__(type, ob, callback, slot1, slot2):
 729                return weakref.ref.__new__(type, ob, callback)
 730            def __init__(self, ob, callback, slot1, slot2):
 731                self.slot1 = slot1
 732                self.slot2 = slot2
 733            def meth(self):
 734                return self.slot1 + self.slot2
 735        o = Object(42)
 736        r = MyRef(o, None, "abc", "def")
 737        self.assertEqual(r.slot1, "abc")
 738        self.assertEqual(r.slot2, "def")
 739        self.assertEqual(r.meth(), "abcdef")
 740        self.failIf(hasattr(r, "__dict__"))
 741
 742    def test_subclass_refs_with_cycle(self):
 743        # Bug #3110
 744        # An instance of a weakref subclass can have attributes.
 745        # If such a weakref holds the only strong reference to the object,
 746        # deleting the weakref will delete the object. In this case,
 747        # the callback must not be called, because the ref object is
 748        # being deleted.
 749        class MyRef(weakref.ref):
 750            pass
 751
 752        # Use a local callback, for "regrtest -R::"
 753        # to detect refcounting problems
 754        def callback(w):
 755            self.cbcalled += 1
 756
 757        o = C()
 758        r1 = MyRef(o, callback)
 759        r1.o = o
 760        del o
 761
 762        del r1 # Used to crash here
 763
 764        self.assertEqual(self.cbcalled, 0)
 765
 766        # Same test, with two weakrefs to the same object
 767        # (since code paths are different)
 768        o = C()
 769        r1 = MyRef(o, callback)
 770        r2 = MyRef(o, callback)
 771        r1.r = r2
 772        r2.o = o
 773        del o
 774        del r2
 775
 776        del r1 # Used to crash here
 777
 778        self.assertEqual(self.cbcalled, 0)
 779
 780
 781class Object:
 782    def __init__(self, arg):
 783        self.arg = arg
 784    def __repr__(self):
 785        return "<Object %r>" % self.arg
 786
 787
 788class MappingTestCase(TestBase):
 789
 790    COUNT = 10
 791
 792    def test_weak_values(self):
 793        #
 794        #  This exercises d.copy(), d.items(), d[], del d[], len(d).
 795        #
 796        dict, objects = self.make_weak_valued_dict()
 797        for o in objects:
 798            self.assert_(weakref.getweakrefcount(o) == 1,
 799                         "wrong number of weak references to %r!" % o)
 800            self.assert_(o is dict[o.arg],
 801                         "wrong object returned by weak dict!")
 802        items1 = dict.items()
 803        items2 = dict.copy().items()
 804        items1.sort()
 805        items2.sort()
 806        self.assert_(items1 == items2,
 807                     "cloning of weak-valued dictionary did not work!")
 808        del items1, items2
 809        self.assert_(len(dict) == self.COUNT)
 810        del objects[0]
 811        self.assert_(len(dict) == (self.COUNT - 1),
 812                     "deleting object did not cause dictionary update")
 813        del objects, o
 814        self.assert_(len(dict) == 0,
 815                     "deleting the values did not clear the dictionary")
 816        # regression on SF bug #447152:
 817        dict = weakref.WeakValueDictionary()
 818        self.assertRaises(KeyError, dict.__getitem__, 1)
 819        dict[2] = C()
 820        self.assertRaises(KeyError, dict.__getitem__, 2)
 821
 822    def test_weak_keys(self):
 823        #
 824        #  This exercises d.copy(), d.items(), d[] = v, d[], del d[],
 825        #  len(d), d.has_key().
 826        #
 827        dict, objects = self.make_weak_keyed_dict()
 828        for o in objects:
 829            self.assert_(weakref.getweakrefcount(o) == 1,
 830                         "wrong number of weak references to %r!" % o)
 831            self.assert_(o.arg is dict[o],
 832                         "wrong object returned by weak dict!")
 833        items1 = dict.items()
 834        items2 = dict.copy().items()
 835        self.assert_(set(items1) == set(items2),
 836                     "cloning of weak-keyed dictionary did not work!")
 837        del items1, items2
 838        self.assert_(len(dict) == self.COUNT)
 839        del objects[0]
 840        self.assert_(len(dict) == (self.COUNT - 1),
 841                     "deleting object did not cause dictionary update")
 842        del objects, o
 843        self.assert_(len(dict) == 0,
 844                     "deleting the keys did not clear the dictionary")
 845        o = Object(42)
 846        dict[o] = "What is the meaning of the universe?"
 847        self.assert_(dict.has_key(o))
 848        self.assert_(not dict.has_key(34))
 849
 850    def test_weak_keyed_iters(self):
 851        dict, objects = self.make_weak_keyed_dict()
 852        self.check_iters(dict)
 853
 854        # Test keyrefs()
 855        refs = dict.keyrefs()
 856        self.assertEqual(len(refs), len(objects))
 857        objects2 = list(objects)
 858        for wr in refs:
 859            ob = wr()
 860            self.assert_(dict.has_key(ob))
 861            self.assert_(ob in dict)
 862            self.assertEqual(ob.arg, dict[ob])
 863            objects2.remove(ob)
 864        self.assertEqual(len(objects2), 0)
 865
 866        # Test iterkeyrefs()
 867        objects2 = list(objects)
 868        self.assertEqual(len(list(dict.iterkeyrefs())), len(objects))
 869        for wr in dict.iterkeyrefs():
 870            ob = wr()
 871            self.assert_(dict.has_key(ob))
 872            self.assert_(ob in dict)
 873            self.assertEqual(ob.arg, dict[ob])
 874            objects2.remove(ob)
 875        self.assertEqual(len(objects2), 0)
 876
 877    def test_weak_valued_iters(self):
 878        dict, objects = self.make_weak_valued_dict()
 879        self.check_iters(dict)
 880
 881        # Test valuerefs()
 882        refs = dict.valuerefs()
 883        self.assertEqual(len(refs), len(objects))
 884        objects2 = list(objects)
 885        for wr in refs:
 886            ob = wr()
 887            self.assertEqual(ob, dict[ob.arg])
 888            self.assertEqual(ob.arg, dict[ob.arg].arg)
 889            objects2.remove(ob)
 890        self.assertEqual(len(objects2), 0)
 891
 892        # Test itervaluerefs()
 893        objects2 = list(objects)
 894        self.assertEqual(len(list(dict.itervaluerefs())), len(objects))
 895        for wr in dict.itervaluerefs():
 896            ob = wr()
 897            self.assertEqual(ob, dict[ob.arg])
 898            self.assertEqual(ob.arg, dict[ob.arg].arg)
 899            objects2.remove(ob)
 900        self.assertEqual(len(objects2), 0)
 901
 902    def check_iters(self, dict):
 903        # item iterator:
 904        items = dict.items()
 905        for item in dict.iteritems():
 906            items.remove(item)
 907        self.assert_(len(items) == 0, "iteritems() did not touch all items")
 908
 909        # key iterator, via __iter__():
 910        keys = dict.keys()
 911        for k in dict:
 912            keys.remove(k)
 913        self.assert_(len(keys) == 0, "__iter__() did not touch all keys")
 914
 915        # key iterator, via iterkeys():
 916        keys = dict.keys()
 917        for k in dict.iterkeys():
 918            keys.remove(k)
 919        self.assert_(len(keys) == 0, "iterkeys() did not touch all keys")
 920
 921        # value iterator:
 922        values = dict.values()
 923        for v in dict.itervalues():
 924            values.remove(v)
 925        self.assert_(len(values) == 0,
 926                     "itervalues() did not touch all values")
 927
 928    def test_make_weak_keyed_dict_from_dict(self):
 929        o = Object(3)
 930        dict = weakref.WeakKeyDictionary({o:364})
 931        self.assert_(dict[o] == 364)
 932
 933    def test_make_weak_keyed_dict_from_weak_keyed_dict(self):
 934        o = Object(3)
 935        dict = weakref.WeakKeyDictionary({o:364})
 936        dict2 = weakref.WeakKeyDictionary(dict)
 937        self.assert_(dict[o] == 364)
 938
 939    def make_weak_keyed_dict(self):
 940        dict = weakref.WeakKeyDictionary()
 941        objects = map(Object, range(self.COUNT))
 942        for o in objects:
 943            dict[o] = o.arg
 944        return dict, objects
 945
 946    def make_weak_valued_dict(self):
 947        dict = weakref.WeakValueDictionary()
 948        objects = map(Object, range(self.COUNT))
 949        for o in objects:
 950            dict[o.arg] = o
 951        return dict, objects
 952
 953    def check_popitem(self, klass, key1, value1, key2, value2):
 954        weakdict = klass()
 955        weakdict[key1] = value1
 956        weakdict[key2] = value2
 957        self.assert_(len(weakdict) == 2)
 958        k, v = weakdict.popitem()
 959        self.assert_(len(weakdict) == 1)
 960        if k is key1:
 961            self.assert_(v is value1)
 962        else:
 963            self.assert_(v is value2)
 964        k, v = weakdict.popitem()
 965        self.assert_(len(weakdict) == 0)
 966        if k is key1:
 967            self.assert_(v is value1)
 968        else:
 969            self.assert_(v is value2)
 970
 971    def test_weak_valued_dict_popitem(self):
 972        self.check_popitem(weakref.WeakValueDictionary,
 973                           "key1", C(), "key2", C())
 974
 975    def test_weak_keyed_dict_popitem(self):
 976        self.check_popitem(weakref.WeakKeyDictionary,
 977                           C(), "value 1", C(), "value 2")
 978
 979    def check_setdefault(self, klass, key, value1, value2):
 980        self.assert_(value1 is not value2,
 981                     "invalid test"
 982                     " -- value parameters must be distinct objects")
 983        weakdict = klass()
 984        o = weakdict.setdefault(key, value1)
 985        self.assert_(o is value1)
 986        self.assert_(weakdict.has_key(key))
 987        self.assert_(weakdict.get(key) is value1)
 988        self.assert_(weakdict[key] is value1)
 989
 990        o = weakdict.setdefault(key, value2)
 991        self.assert_(o is value1)
 992        self.assert_(weakdict.has_key(key))
 993        self.assert_(weakdict.get(key) is value1)
 994        self.assert_(weakdict[key] is value1)
 995
 996    def test_weak_valued_dict_setdefault(self):
 997        self.check_setdefault(weakref.WeakValueDictionary,
 998                              "key", C(), C())
 999
1000    def test_weak_keyed_dict_setdefault(self):
1001        self.check_setdefault(weakref.WeakKeyDictionary,
1002                              C(), "value 1", "value 2")
1003
1004    def check_update(self, klass, dict):
1005        #
1006        #  This exercises d.update(), len(d), d.keys(), d.has_key(),
1007        #  d.get(), d[].
1008        #
1009        weakdict = klass()
1010        weakdict.update(dict)
1011        self.assert_(len(weakdict) == len(dict))
1012        for k in weakdict.keys():
1013            self.assert_(dict.has_key(k),
1014                         "mysterious new key appeared in weak dict")
1015            v = dict.get(k)
1016            self.assert_(v is weakdict[k])
1017            self.assert_(v is weakdict.get(k))
1018        for k in dict.keys():
1019            self.assert_(weakdict.has_key(k),
1020                         "original key disappeared in weak dict")
1021            v = dict[k]
1022            self.assert_(v is weakdict[k])
1023            self.assert_(v is weakdict.get(k))
1024
1025    def test_weak_valued_dict_update(self):
1026        self.check_update(weakref.WeakValueDictionary,
1027                          {1: C(), 'a': C(), C(): C()})
1028
1029    def test_weak_keyed_dict_update(self):
1030        self.check_update(weakref.WeakKeyDictionary,
1031                          {C(): 1, C(): 2, C(): 3})
1032
1033    def test_weak_keyed_delitem(self):
1034        d = weakref.WeakKeyDictionary()
1035        o1 = Object('1')
1036        o2 = Object('2')
1037        d[o1] = 'something'
1038        d[o2] = 'something'
1039        self.assert_(len(d) == 2)
1040        del d[o1]
1041        self.assert_(len(d) == 1)
1042        self.assert_(d.keys() == [o2])
1043
1044    def test_weak_valued_delitem(self):
1045        d = weakref.WeakValueDictionary()
1046        o1 = Object('1')
1047        o2 = Object('2')
1048        d['something'] = o1
1049        d['something else'] = o2
1050        self.assert_(len(d) == 2)
1051        del d['something']
1052        self.assert_(len(d) == 1)
1053        self.assert_(d.items() == [('something else', o2)])
1054
1055    def test_weak_keyed_bad_delitem(self):
1056        d = weakref.WeakKeyDictionary()
1057        o = Object('1')
1058        # An attempt to delete an object that isn't there should raise
1059        # KeyError.  It didn't before 2.3.
1060        self.assertRaises(KeyError, d.__delitem__, o)
1061        self.assertRaises(KeyError, d.__getitem__, o)
1062
1063        # If a key isn't of a weakly referencable type, __getitem__ and
1064        # __setitem__ raise TypeError.  __delitem__ should too.
1065        self.assertRaises(TypeError, d.__delitem__,  13)
1066        self.assertRaises(TypeError, d.__getitem__,  13)
1067        self.assertRaises(TypeError, d.__setitem__,  13, 13)
1068
1069    def test_weak_keyed_cascading_deletes(self):
1070        # SF bug 742860.  For some reason, before 2.3 __delitem__ iterated
1071        # over the keys via self.data.iterkeys().  If things vanished from
1072        # the dict during this (or got added), that caused a RuntimeError.
1073
1074        d = weakref.WeakKeyDictionary()
1075        mutate = False
1076
1077        class C(object):
1078            def __init__(self, i):
1079                self.value = i
1080            def __hash__(self):
1081                return hash(self.value)
1082            def __eq__(self, other):
1083                if mutate:
1084                    # Side effect that mutates the dict, by removing the
1085                    # last strong reference to a key.
1086                    del objs[-1]
1087                return self.value == other.value
1088
1089        objs = [C(i) for i in range(4)]
1090        for o in objs:
1091            d[o] = o.value
1092        del o   # now the only strong references to keys are in objs
1093        # Find the order in which iterkeys sees the keys.
1094        objs = d.keys()
1095        # Reverse it, so that the iteration implementation of __delitem__
1096        # has to keep looping to find the first object we delete.
1097        objs.reverse()
1098
1099        # Turn on mutation in C.__eq__.  The first time thru the loop,
1100        # under the iterkeys() business the first comparison will delete
1101        # the last item iterkeys() would see, and that causes a
1102        #     RuntimeError: dictionary changed size during iteration
1103        # when the iterkeys() loop goes around to try comparing the next
1104        # key.  After this was fixed, it just deletes the last object *our*
1105        # "for o in obj" loop would have gotten to.
1106        mutate = True
1107        count = 0
1108        for o in objs:
1109            count += 1
1110            del d[o]
1111        self.assertEqual(len(d), 0)
1112        self.assertEqual(count, 2)
1113
1114from test import mapping_tests
1115
1116class WeakValueDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
1117    """Check that WeakValueDictionary conforms to the mapping protocol"""
1118    __ref = {"key1":Object(1), "key2":Object(2), "key3":Object(3)}
1119    type2test = weakref.WeakValueDictionary
1120    def _reference(self):
1121        return self.__ref.copy()
1122
1123class WeakKeyDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
1124    """Check that WeakKeyDictionary conforms to the mapping protocol"""
1125    __ref = {Object("key1"):1, Object("key2"):2, Object("key3"):3}
1126    type2test = weakref.WeakKeyDictionary
1127    def _reference(self):
1128        return self.__ref.copy()
1129
1130libreftest = """ Doctest for examples in the library reference: weakref.rst
1131
1132>>> import weakref
1133>>> class Dict(dict):
1134...     pass
1135...
1136>>> obj = Dict(red=1, green=2, blue=3)   # this object is weak referencable
1137>>> r = weakref.ref(obj)
1138>>> print r() is obj
1139True
1140
1141>>> import weakref
1142>>> class Object:
1143...     pass
1144...
1145>>> o = Object()
1146>>> r = weakref.ref(o)
1147>>> o2 = r()
1148>>> o is o2
1149True
1150>>> del o, o2
1151>>> print r()
1152None
1153
1154>>> import weakref
1155>>> class ExtendedRef(weakref.ref):
1156...     def __init__(self, ob, callback=None, **annotations):
1157...         super(ExtendedRef, self).__init__(ob, callback)
1158...         self.__counter = 0
1159...         for k, v in annotations.iteritems():
1160...             setattr(self, k, v)
1161...     def __call__(self):
1162...         '''Return a pair containing the referent and the number of
1163...         times the reference has been called.
1164...         '''
1165...         ob = super(ExtendedRef, self).__call__()
1166...         if ob is not None:
1167...             self.__counter += 1
1168...             ob = (ob, self.__counter)
1169...         return ob
1170...
1171>>> class A:   # not in docs from here, just testing the ExtendedRef
1172...     pass
1173...
1174>>> a = A()
1175>>> r = ExtendedRef(a, foo=1, bar="baz")
1176>>> r.foo
11771
1178>>> r.bar
1179'baz'
1180>>> r()[1]
11811
1182>>> r()[1]
11832
1184>>> r()[0] is a
1185True
1186
1187
1188>>> import weakref
1189>>> _id2obj_dict = weakref.WeakValueDictionary()
1190>>> def remember(obj):
1191...     oid = id(obj)
1192...     _id2obj_dict[oid] = obj
1193...     return oid
1194...
1195>>> def id2obj(oid):
1196...     return _id2obj_dict[oid]
1197...
1198>>> a = A()             # from here, just testing
1199>>> a_id = remember(a)
1200>>> id2obj(a_id) is a
1201True
1202>>> del a
1203>>> try:
1204...     id2obj(a_id)
1205... except KeyError:
1206...     print 'OK'
1207... else:
1208...     print 'WeakValueDictionary error'
1209OK
1210
1211"""
1212
1213__test__ = {'libreftest' : libreftest}
1214
1215def test_main():
1216    test_support.run_unittest(
1217        ReferencesTestCase,
1218        MappingTestCase,
1219        WeakValueDictionaryTestCase,
1220        WeakKeyDictionaryTestCase,
1221        SubclassableWeakrefTestCase,
1222        )
1223    test_support.run_doctest(sys.modules[__name__])
1224
1225
1226if __name__ == "__main__":
1227    test_main()