annotate Lib/test/test_weakref.py @ 58925:eecf1bef052d 2.6

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