Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_weakref.ReferencesTestCase Class Reference
Inheritance diagram for test.test_weakref.ReferencesTestCase:
Inheritance graph
[legend]
Collaboration diagram for test.test_weakref.ReferencesTestCase:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_basic_ref
def test_basic_callback
def test_multiple_callbacks
def test_multiple_selfref_callbacks
def test_proxy_ref
def check_basic_ref
def check_basic_callback
def test_ref_reuse
def test_proxy_reuse
def test_basic_proxy
def test_proxy_unicode
def test_proxy_index
def test_proxy_div
def test_shared_ref_without_callback
def test_shared_proxy_without_callback
def check_shared_without_callback
def test_callable_proxy
def check_proxy
def test_proxy_deletion
def test_proxy_bool
def test_getweakrefcount
def test_getweakrefs
def test_newstyle_number_ops
def test_callbacks_protected
def test_sf_bug_840829
def test_callback_in_cycle_1
def test_callback_in_cycle_2
def test_callback_in_cycle_3
def test_callback_in_cycle_4
def test_callback_in_cycle_resurrection
def test_callbacks_on_callback
def test_gc_during_ref_creation
def test_gc_during_proxy_creation
def check_gc_during_creation
def test_ref_created_during_del
def test_init
def test_classes
def setUp
def callback

Public Attributes

 cbcalled
 ref
 result
 attribute

Detailed Description

Definition at line 44 of file test_weakref.py.


Member Function Documentation

def test.test_weakref.TestBase.callback (   self,
  ref 
) [inherited]

Definition at line 40 of file test_weakref.py.

00040 
00041     def callback(self, ref):
00042         self.cbcalled += 1
00043 

Here is the caller graph for this function:

Definition at line 119 of file test_weakref.py.

00119 
00120     def check_basic_callback(self, factory):
00121         self.cbcalled = 0
00122         o = factory()
00123         ref = weakref.ref(o, self.callback)
00124         del o
00125         self.assertTrue(self.cbcalled == 1,
00126                      "callback did not properly set 'cbcalled'")
00127         self.assertTrue(ref() is None,
00128                      "ref2 should be dead after deleting object reference")

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 110 of file test_weakref.py.

00110 
00111     def check_basic_ref(self, factory):
00112         o = factory()
00113         ref = weakref.ref(o)
00114         self.assertTrue(ref() is not None,
00115                      "weak reference to live object should be live")
00116         o2 = ref()
00117         self.assertTrue(o is o2,
00118                      "<ref>() should return original object if live")

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 638 of file test_weakref.py.

00638 
00639     def check_gc_during_creation(self, makeref):
00640         thresholds = gc.get_threshold()
00641         gc.set_threshold(1, 1, 1)
00642         gc.collect()
00643         class A:
00644             pass
00645 
00646         def callback(*args):
00647             pass
00648 
00649         referenced = A()
00650 
00651         a = A()
00652         a.a = a
00653         a.wr = makeref(referenced)
00654 
00655         try:
00656             # now make sure the object and the ref get labeled as
00657             # cyclic trash:
00658             a = A()
00659             weakref.ref(referenced, callback)
00660 
00661         finally:
00662             gc.set_threshold(*thresholds)

Here is the caller graph for this function:

def test.test_weakref.ReferencesTestCase.check_proxy (   self,
  o,
  proxy 
)

Definition at line 271 of file test_weakref.py.

00271 
00272     def check_proxy(self, o, proxy):
00273         o.foo = 1
00274         self.assertTrue(proxy.foo == 1,
00275                      "proxy does not reflect attribute addition")
00276         o.foo = 2
00277         self.assertTrue(proxy.foo == 2,
00278                      "proxy does not reflect attribute modification")
00279         del o.foo
00280         self.assertTrue(not hasattr(proxy, 'foo'),
00281                      "proxy does not reflect attribute removal")
00282 
00283         proxy.foo = 1
00284         self.assertTrue(o.foo == 1,
00285                      "object does not reflect attribute addition via proxy")
00286         proxy.foo = 2
00287         self.assertTrue(
00288             o.foo == 2,
00289             "object does not reflect attribute modification via proxy")
00290         del proxy.foo
00291         self.assertTrue(not hasattr(o, 'foo'),
00292                      "object does not reflect attribute removal via proxy")

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 232 of file test_weakref.py.

00232 
00233     def check_shared_without_callback(self, makeref):
00234         o = Object(1)
00235         p1 = makeref(o, None)
00236         p2 = makeref(o, None)
00237         self.assertTrue(p1 is p2, "both callbacks were None in the C API")
00238         del p1, p2
00239         p1 = makeref(o)
00240         p2 = makeref(o, None)
00241         self.assertTrue(p1 is p2, "callbacks were NULL, None in the C API")
00242         del p1, p2
00243         p1 = makeref(o)
00244         p2 = makeref(o)
00245         self.assertTrue(p1 is p2, "both callbacks were NULL in the C API")
00246         del p1, p2
00247         p1 = makeref(o, None)
00248         p2 = makeref(o)
00249         self.assertTrue(p1 is p2, "callbacks were None, NULL in the C API")

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_weakref.TestBase.setUp (   self) [inherited]

Definition at line 37 of file test_weakref.py.

00037 
00038     def setUp(self):
00039         self.cbcalled = 0

Here is the caller graph for this function:

Definition at line 60 of file test_weakref.py.

00060 
00061     def test_basic_callback(self):
00062         self.check_basic_callback(C)
00063         self.check_basic_callback(create_function)
00064         self.check_basic_callback(create_bound_method)

Here is the call graph for this function:

Definition at line 159 of file test_weakref.py.

00159 
00160     def test_basic_proxy(self):
00161         o = C()
00162         self.check_proxy(o, weakref.proxy(o))
00163 
00164         L = collections.UserList()
00165         p = weakref.proxy(L)
00166         self.assertFalse(p, "proxy for empty UserList should be false")
00167         p.append(12)
00168         self.assertEqual(len(L), 1)
00169         self.assertTrue(p, "proxy for non-empty UserList should be true")
00170         p[:] = [2, 3]
00171         self.assertEqual(len(L), 2)
00172         self.assertEqual(len(p), 2)
00173         self.assertIn(3, p, "proxy didn't support __contains__() properly")
00174         p[1] = 5
00175         self.assertEqual(L[1], 5)
00176         self.assertEqual(p[1], 5)
00177         L2 = collections.UserList(L)
00178         p2 = weakref.proxy(L2)
00179         self.assertEqual(p, p2)
00180         ## self.assertEqual(repr(L2), repr(p2))
00181         L3 = collections.UserList(range(10))
00182         p3 = weakref.proxy(L3)
00183         self.assertEqual(L3[:], p3[:])
00184         self.assertEqual(L3[5:], p3[5:])
00185         self.assertEqual(L3[:5], p3[:5])
00186         self.assertEqual(L3[2:5], p3[2:5])

Here is the call graph for this function:

Definition at line 46 of file test_weakref.py.

00046 
00047     def test_basic_ref(self):
00048         self.check_basic_ref(C)
00049         self.check_basic_ref(create_function)
00050         self.check_basic_ref(create_bound_method)
00051 
00052         # Just make sure the tp_repr handler doesn't raise an exception.
00053         # Live reference:
00054         o = C()
00055         wr = weakref.ref(o)
00056         repr(wr)
00057         # Dead reference:
00058         del o
00059         repr(wr)

Here is the call graph for this function:

Definition at line 250 of file test_weakref.py.

00250 
00251     def test_callable_proxy(self):
00252         o = Callable()
00253         ref1 = weakref.proxy(o)
00254 
00255         self.check_proxy(o, ref1)
00256 
00257         self.assertTrue(type(ref1) is weakref.CallableProxyType,
00258                      "proxy is not of callable type")
00259         ref1('twinkies!')
00260         self.assertTrue(o.bar == 'twinkies!',
00261                      "call through proxy not passed through to original")
00262         ref1(x='Splat.')
00263         self.assertTrue(o.bar == 'Splat.',
00264                      "call through proxy not passed through to original")
00265 
00266         # expect due to too few args
00267         self.assertRaises(TypeError, ref1)
00268 
00269         # expect due to too many args
00270         self.assertRaises(TypeError, ref1, 1, 2, 3)

Here is the call graph for this function:

Definition at line 427 of file test_weakref.py.

00427 
00428     def test_callback_in_cycle_1(self):
00429         import gc
00430 
00431         class J(object):
00432             pass
00433 
00434         class II(object):
00435             def acallback(self, ignore):
00436                 self.J
00437 
00438         I = II()
00439         I.J = J
00440         I.wr = weakref.ref(J, I.acallback)
00441 
00442         # Now J and II are each in a self-cycle (as all new-style class
00443         # objects are, since their __mro__ points back to them).  I holds
00444         # both a weak reference (I.wr) and a strong reference (I.J) to class
00445         # J.  I is also in a cycle (I.wr points to a weakref that references
00446         # I.acallback).  When we del these three, they all become trash, but
00447         # the cycles prevent any of them from getting cleaned up immediately.
00448         # Instead they have to wait for cyclic gc to deduce that they're
00449         # trash.
00450         #
00451         # gc used to call tp_clear on all of them, and the order in which
00452         # it does that is pretty accidental.  The exact order in which we
00453         # built up these things manages to provoke gc into running tp_clear
00454         # in just the right order (I last).  Calling tp_clear on II leaves
00455         # behind an insane class object (its __mro__ becomes NULL).  Calling
00456         # tp_clear on J breaks its self-cycle, but J doesn't get deleted
00457         # just then because of the strong reference from I.J.  Calling
00458         # tp_clear on I starts to clear I's __dict__, and just happens to
00459         # clear I.J first -- I.wr is still intact.  That removes the last
00460         # reference to J, which triggers the weakref callback.  The callback
00461         # tries to do "self.J", and instances of new-style classes look up
00462         # attributes ("J") in the class dict first.  The class (II) wants to
00463         # search II.__mro__, but that's NULL.   The result was a segfault in
00464         # a release build, and an assert failure in a debug build.
00465         del I, J, II
00466         gc.collect()

Definition at line 467 of file test_weakref.py.

00467 
00468     def test_callback_in_cycle_2(self):
00469         import gc
00470 
00471         # This is just like test_callback_in_cycle_1, except that II is an
00472         # old-style class.  The symptom is different then:  an instance of an
00473         # old-style class looks in its own __dict__ first.  'J' happens to
00474         # get cleared from I.__dict__ before 'wr', and 'J' was never in II's
00475         # __dict__, so the attribute isn't found.  The difference is that
00476         # the old-style II doesn't have a NULL __mro__ (it doesn't have any
00477         # __mro__), so no segfault occurs.  Instead it got:
00478         #    test_callback_in_cycle_2 (__main__.ReferencesTestCase) ...
00479         #    Exception exceptions.AttributeError:
00480         #   "II instance has no attribute 'J'" in <bound method II.acallback
00481         #       of <?.II instance at 0x00B9B4B8>> ignored
00482 
00483         class J(object):
00484             pass
00485 
00486         class II:
00487             def acallback(self, ignore):
00488                 self.J
00489 
00490         I = II()
00491         I.J = J
00492         I.wr = weakref.ref(J, I.acallback)
00493 
00494         del I, J, II
00495         gc.collect()

Definition at line 496 of file test_weakref.py.

00496 
00497     def test_callback_in_cycle_3(self):
00498         import gc
00499 
00500         # This one broke the first patch that fixed the last two.  In this
00501         # case, the objects reachable from the callback aren't also reachable
00502         # from the object (c1) *triggering* the callback:  you can get to
00503         # c1 from c2, but not vice-versa.  The result was that c2's __dict__
00504         # got tp_clear'ed by the time the c2.cb callback got invoked.
00505 
00506         class C:
00507             def cb(self, ignore):
00508                 self.me
00509                 self.c1
00510                 self.wr
00511 
00512         c1, c2 = C(), C()
00513 
00514         c2.me = c2
00515         c2.c1 = c1
00516         c2.wr = weakref.ref(c1, c2.cb)
00517 
00518         del c1, c2
00519         gc.collect()

Definition at line 520 of file test_weakref.py.

00520 
00521     def test_callback_in_cycle_4(self):
00522         import gc
00523 
00524         # Like test_callback_in_cycle_3, except c2 and c1 have different
00525         # classes.  c2's class (C) isn't reachable from c1 then, so protecting
00526         # objects reachable from the dying object (c1) isn't enough to stop
00527         # c2's class (C) from getting tp_clear'ed before c2.cb is invoked.
00528         # The result was a segfault (C.__mro__ was NULL when the callback
00529         # tried to look up self.me).
00530 
00531         class C(object):
00532             def cb(self, ignore):
00533                 self.me
00534                 self.c1
00535                 self.wr
00536 
00537         class D:
00538             pass
00539 
00540         c1, c2 = D(), C()
00541 
00542         c2.me = c2
00543         c2.c1 = c1
00544         c2.wr = weakref.ref(c1, c2.cb)
00545 
00546         del c1, c2, C, D
00547         gc.collect()

Definition at line 548 of file test_weakref.py.

00548 
00549     def test_callback_in_cycle_resurrection(self):
00550         import gc
00551 
00552         # Do something nasty in a weakref callback:  resurrect objects
00553         # from dead cycles.  For this to be attempted, the weakref and
00554         # its callback must also be part of the cyclic trash (else the
00555         # objects reachable via the callback couldn't be in cyclic trash
00556         # to begin with -- the callback would act like an external root).
00557         # But gc clears trash weakrefs with callbacks early now, which
00558         # disables the callbacks, so the callbacks shouldn't get called
00559         # at all (and so nothing actually gets resurrected).
00560 
00561         alist = []
00562         class C(object):
00563             def __init__(self, value):
00564                 self.attribute = value
00565 
00566             def acallback(self, ignore):
00567                 alist.append(self.c)
00568 
00569         c1, c2 = C(1), C(2)
00570         c1.c = c2
00571         c2.c = c1
00572         c1.wr = weakref.ref(c2, c1.acallback)
00573         c2.wr = weakref.ref(c1, c2.acallback)
00574 
00575         def C_went_away(ignore):
00576             alist.append("C went away")
00577         wr = weakref.ref(C, C_went_away)
00578 
00579         del c1, c2, C   # make them all trash
00580         self.assertEqual(alist, [])  # del isn't enough to reclaim anything
00581 
00582         gc.collect()
00583         # c1.wr and c2.wr were part of the cyclic trash, so should have
00584         # been cleared without their callbacks executing.  OTOH, the weakref
00585         # to C is bound to a function local (wr), and wasn't trash, so that
00586         # callback should have been invoked when C went away.
00587         self.assertEqual(alist, ["C went away"])
00588         # The remaining weakref should be dead now (its callback ran).
00589         self.assertEqual(wr(), None)
00590 
00591         del alist[:]
00592         gc.collect()
00593         self.assertEqual(alist, [])

Here is the call graph for this function:

Definition at line 594 of file test_weakref.py.

00594 
00595     def test_callbacks_on_callback(self):
00596         import gc
00597 
00598         # Set up weakref callbacks *on* weakref callbacks.
00599         alist = []
00600         def safe_callback(ignore):
00601             alist.append("safe_callback called")
00602 
00603         class C(object):
00604             def cb(self, ignore):
00605                 alist.append("cb called")
00606 
00607         c, d = C(), C()
00608         c.other = d
00609         d.other = c
00610         callback = c.cb
00611         c.wr = weakref.ref(d, callback)     # this won't trigger
00612         d.wr = weakref.ref(callback, d.cb)  # ditto
00613         external_wr = weakref.ref(callback, safe_callback)  # but this will
00614         self.assertTrue(external_wr() is callback)
00615 
00616         # The weakrefs attached to c and d should get cleared, so that
00617         # C.cb is never called.  But external_wr isn't part of the cyclic
00618         # trash, and no cyclic trash is reachable from it, so safe_callback
00619         # should get invoked when the bound method object callback (c.cb)
00620         # -- which is itself a callback, and also part of the cyclic trash --
00621         # gets reclaimed at the end of gc.
00622 
00623         del callback, c, d, C
00624         self.assertEqual(alist, [])  # del isn't enough to clean up cycles
00625         gc.collect()
00626         self.assertEqual(alist, ["safe_callback called"])
00627         self.assertEqual(external_wr(), None)
00628 
00629         del alist[:]
00630         gc.collect()
00631         self.assertEqual(alist, [])

Here is the call graph for this function:

Definition at line 362 of file test_weakref.py.

00362 
00363     def test_callbacks_protected(self):
00364         # Callbacks protected from already-set exceptions?
00365         # Regression test for SF bug #478534.
00366         class BogusError(Exception):
00367             pass
00368         data = {}
00369         def remove(k):
00370             del data[k]
00371         def encapsulate():
00372             f = lambda : ()
00373             data[weakref.ref(f, remove)] = None
00374             raise BogusError
00375         try:
00376             encapsulate()
00377         except BogusError:
00378             pass
00379         else:
00380             self.fail("exception not properly restored")
00381         try:
00382             encapsulate()
00383         except BogusError:
00384             pass
00385         else:
00386             self.fail("exception not properly restored")

Definition at line 683 of file test_weakref.py.

00683 
00684     def test_classes(self):
00685         # Check that classes are weakrefable.
00686         class A(object):
00687             pass
00688         l = []
00689         weakref.ref(int)
00690         a = weakref.ref(A, l.append)
00691         A = None
00692         gc.collect()
00693         self.assertEqual(a(), None)
00694         self.assertEqual(l, [a])
00695 

Definition at line 635 of file test_weakref.py.

00635 
00636     def test_gc_during_proxy_creation(self):
00637         self.check_gc_during_creation(weakref.proxy)

Here is the call graph for this function:

Definition at line 632 of file test_weakref.py.

00632 
00633     def test_gc_during_ref_creation(self):
00634         self.check_gc_during_creation(weakref.ref)

Here is the call graph for this function:

Definition at line 310 of file test_weakref.py.

00310 
00311     def test_getweakrefcount(self):
00312         o = C()
00313         ref1 = weakref.ref(o)
00314         ref2 = weakref.ref(o, self.callback)
00315         self.assertTrue(weakref.getweakrefcount(o) == 2,
00316                      "got wrong number of weak reference objects")
00317 
00318         proxy1 = weakref.proxy(o)
00319         proxy2 = weakref.proxy(o, self.callback)
00320         self.assertTrue(weakref.getweakrefcount(o) == 4,
00321                      "got wrong number of weak reference objects")
00322 
00323         del ref1, ref2, proxy1, proxy2
00324         self.assertTrue(weakref.getweakrefcount(o) == 0,
00325                      "weak reference objects not unlinked from"
00326                      " referent when discarded.")
00327 
00328         # assumes ints do not support weakrefs
00329         self.assertTrue(weakref.getweakrefcount(1) == 0,
00330                      "got wrong number of weak reference objects for int")

Here is the call graph for this function:

Definition at line 331 of file test_weakref.py.

00331 
00332     def test_getweakrefs(self):
00333         o = C()
00334         ref1 = weakref.ref(o, self.callback)
00335         ref2 = weakref.ref(o, self.callback)
00336         del ref1
00337         self.assertTrue(weakref.getweakrefs(o) == [ref2],
00338                      "list of refs does not match")
00339 
00340         o = C()
00341         ref1 = weakref.ref(o, self.callback)
00342         ref2 = weakref.ref(o, self.callback)
00343         del ref2
00344         self.assertTrue(weakref.getweakrefs(o) == [ref1],
00345                      "list of refs does not match")
00346 
00347         del ref1
00348         self.assertTrue(weakref.getweakrefs(o) == [],
00349                      "list of refs not cleared")
00350 
00351         # assumes ints do not support weakrefs
00352         self.assertTrue(weakref.getweakrefs(1) == [],
00353                      "list of refs does not match for int")

Here is the call graph for this function:

Definition at line 675 of file test_weakref.py.

00675 
00676     def test_init(self):
00677         # Issue 3634
00678         # <weakref to class>.__init__() doesn't check errors correctly
00679         r = weakref.ref(Exception)
00680         self.assertRaises(TypeError, r.__init__, 0, 0, 0, 0, 0)
00681         # No exception should be raised here
00682         gc.collect()

Here is the call graph for this function:

Definition at line 65 of file test_weakref.py.

00065 
00066     def test_multiple_callbacks(self):
00067         o = C()
00068         ref1 = weakref.ref(o, self.callback)
00069         ref2 = weakref.ref(o, self.callback)
00070         del o
00071         self.assertTrue(ref1() is None,
00072                      "expected reference to be invalidated")
00073         self.assertTrue(ref2() is None,
00074                      "expected reference to be invalidated")
00075         self.assertTrue(self.cbcalled == 2,
00076                      "callback not called the right number of times")

Here is the call graph for this function:

Definition at line 77 of file test_weakref.py.

00077 
00078     def test_multiple_selfref_callbacks(self):
00079         # Make sure all references are invalidated before callbacks are called
00080         #
00081         # What's important here is that we're using the first
00082         # reference in the callback invoked on the second reference
00083         # (the most recently created ref is cleaned up first).  This
00084         # tests that all references to the object are invalidated
00085         # before any of the callbacks are invoked, so that we only
00086         # have one invocation of _weakref.c:cleanup_helper() active
00087         # for a particular object at a time.
00088         #
00089         def callback(object, self=self):
00090             self.ref()
00091         c = C()
00092         self.ref = weakref.ref(c, callback)
00093         ref1 = weakref.ref(c, callback)
00094         del c

Here is the call graph for this function:

Definition at line 354 of file test_weakref.py.

00354 
00355     def test_newstyle_number_ops(self):
00356         class F(float):
00357             pass
00358         f = F(2.0)
00359         p = weakref.proxy(f)
00360         self.assertTrue(p + 1.0 == 3.0)
00361         self.assertTrue(1.0 + p == 3.0)  # this used to SEGV

Definition at line 304 of file test_weakref.py.

00304 
00305     def test_proxy_bool(self):
00306         # Test clearing of SF bug #1170766
00307         class List(list): pass
00308         lyst = List()
00309         self.assertEqual(bool(weakref.proxy(lyst)), bool(lyst))

Here is the call graph for this function:

Definition at line 293 of file test_weakref.py.

00293 
00294     def test_proxy_deletion(self):
00295         # Test clearing of SF bug #762891
00296         class Foo:
00297             result = None
00298             def __delitem__(self, accessor):
00299                 self.result = accessor
00300         g = Foo()
00301         f = weakref.proxy(g)
00302         del f[0]
00303         self.assertEqual(f.result, 0)

Definition at line 206 of file test_weakref.py.

00206 
00207     def test_proxy_div(self):
00208         class C:
00209             def __floordiv__(self, other):
00210                 return 42
00211             def __ifloordiv__(self, other):
00212                 return 21
00213         o = C()
00214         p = weakref.proxy(o)
00215         self.assertEqual(p // 5, 42)
00216         p //= 5
00217         self.assertEqual(p, 21)

Here is the call graph for this function:

Definition at line 198 of file test_weakref.py.

00198 
00199     def test_proxy_index(self):
00200         class C:
00201             def __index__(self):
00202                 return 10
00203         o = C()
00204         p = weakref.proxy(o)
00205         self.assertEqual(operator.index(p), 10)

Here is the call graph for this function:

Definition at line 95 of file test_weakref.py.

00095 
00096     def test_proxy_ref(self):
00097         o = C()
00098         o.bar = 1
00099         ref1 = weakref.proxy(o, self.callback)
00100         ref2 = weakref.proxy(o, self.callback)
00101         del o
00102 
00103         def check(proxy):
00104             proxy.bar
00105 
00106         self.assertRaises(ReferenceError, check, ref1)
00107         self.assertRaises(ReferenceError, check, ref2)
00108         self.assertRaises(ReferenceError, bool, weakref.proxy(C()))
00109         self.assertEqual(self.cbcalled, 2)

Here is the call graph for this function:

Definition at line 151 of file test_weakref.py.

00151 
00152     def test_proxy_reuse(self):
00153         o = C()
00154         proxy1 = weakref.proxy(o)
00155         ref = weakref.ref(o)
00156         proxy2 = weakref.proxy(o)
00157         self.assertTrue(proxy1 is proxy2,
00158                      "proxy object w/out callback should have been re-used")

Here is the call graph for this function:

Definition at line 187 of file test_weakref.py.

00187 
00188     def test_proxy_unicode(self):
00189         # See bug 5037
00190         class C(object):
00191             def __str__(self):
00192                 return "string"
00193             def __bytes__(self):
00194                 return b"bytes"
00195         instance = C()
00196         self.assertIn("__bytes__", dir(weakref.proxy(instance)))
00197         self.assertEqual(bytes(weakref.proxy(instance)), b"bytes")

Here is the call graph for this function:

Definition at line 663 of file test_weakref.py.

00663 
00664     def test_ref_created_during_del(self):
00665         # Bug #1377858
00666         # A weakref created in an object's __del__() would crash the
00667         # interpreter when the weakref was cleaned up since it would refer to
00668         # non-existent memory.  This test should not segfault the interpreter.
00669         class Target(object):
00670             def __del__(self):
00671                 global ref_from_del
00672                 ref_from_del = weakref.ref(self)
00673 
00674         w = Target()

Definition at line 129 of file test_weakref.py.

00129 
00130     def test_ref_reuse(self):
00131         o = C()
00132         ref1 = weakref.ref(o)
00133         # create a proxy to make sure that there's an intervening creation
00134         # between these two; it should make no difference
00135         proxy = weakref.proxy(o)
00136         ref2 = weakref.ref(o)
00137         self.assertTrue(ref1 is ref2,
00138                      "reference object w/out callback should be re-used")
00139 
00140         o = C()
00141         proxy = weakref.proxy(o)
00142         ref1 = weakref.ref(o)
00143         ref2 = weakref.ref(o)
00144         self.assertTrue(ref1 is ref2,
00145                      "reference object w/out callback should be re-used")
00146         self.assertTrue(weakref.getweakrefcount(o) == 2,
00147                      "wrong weak ref count for object")
00148         del proxy
00149         self.assertTrue(weakref.getweakrefcount(o) == 1,
00150                      "wrong weak ref count for object after deleting proxy")

Here is the call graph for this function:

Definition at line 387 of file test_weakref.py.

00387 
00388     def test_sf_bug_840829(self):
00389         # "weakref callbacks and gc corrupt memory"
00390         # subtype_dealloc erroneously exposed a new-style instance
00391         # already in the process of getting deallocated to gc,
00392         # causing double-deallocation if the instance had a weakref
00393         # callback that triggered gc.
00394         # If the bug exists, there probably won't be an obvious symptom
00395         # in a release build.  In a debug build, a segfault will occur
00396         # when the second attempt to remove the instance from the "list
00397         # of all objects" occurs.
00398 
00399         import gc
00400 
00401         class C(object):
00402             pass
00403 
00404         c = C()
00405         wr = weakref.ref(c, lambda ignore: gc.collect())
00406         del c
00407 
00408         # There endeth the first part.  It gets worse.
00409         del wr
00410 
00411         c1 = C()
00412         c1.i = C()
00413         wr = weakref.ref(c1.i, lambda ignore: gc.collect())
00414 
00415         c2 = C()
00416         c2.c1 = c1
00417         del c1  # still alive because c2 points to it
00418 
00419         # Now when subtype_dealloc gets called on c2, it's not enough just
00420         # that c2 is immune from gc while the weakref callbacks associated
00421         # with c2 execute (there are none in this 2nd half of the test, btw).
00422         # subtype_dealloc goes on to call the base classes' deallocs too,
00423         # so any gc triggered by weakref callbacks associated with anything
00424         # torn down by a base class dealloc can also trigger double
00425         # deallocation of c2.
00426         del c2

Definition at line 229 of file test_weakref.py.

00229 
00230     def test_shared_proxy_without_callback(self):
00231         self.check_shared_without_callback(weakref.proxy)

Here is the call graph for this function:

Definition at line 226 of file test_weakref.py.

00226 
00227     def test_shared_ref_without_callback(self):
00228         self.check_shared_without_callback(weakref.ref)

Here is the call graph for this function:


Member Data Documentation

Definition at line 563 of file test_weakref.py.

Reimplemented from test.test_weakref.TestBase.

Definition at line 74 of file test_weakref.py.

Definition at line 91 of file test_weakref.py.

Definition at line 298 of file test_weakref.py.


The documentation for this class was generated from the following file: