annotate Lib/test/test_weakref.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 17891566a478
children c8c840162804
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)
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
57 `wr`
22946
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
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
60 `wr`
22946
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
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
73 self.assertTrue(ref1() is None,
17172
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")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
75 self.assertTrue(ref2() is None,
17172
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")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
77 self.assertTrue(self.cbcalled == 2,
17172
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()))
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
111 self.assertTrue(self.cbcalled == 2)
17172
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)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
116 self.assertTrue(ref() is not None,
17172
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()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
119 self.assertTrue(o is o2,
17172
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
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
127 self.assertTrue(self.cbcalled == 1,
17498
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'")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
129 self.assertTrue(ref() is None,
17498
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)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
139 self.assertTrue(ref1 is ref2,
17172
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)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
146 self.assertTrue(ref1 is ref2,
17172
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")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
148 self.assertTrue(weakref.getweakrefcount(o) == 2,
17172
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
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
151 self.assertTrue(weakref.getweakrefcount(o) == 1,
17172
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)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
159 self.assertTrue(proxy1 is proxy2,
17172
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)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
168 self.assertFalse(p, "proxy for empty UserList should be false")
21484
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)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
171 self.assertTrue(p, "proxy for non-empty UserList should be true")
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
172 p[:] = [2, 3]
21484
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)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
175 self.assertIn(3, p, "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
176 p[1] = 5
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
177 self.assertEqual(L[1], 5)
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
178 self.assertEqual(p[1], 5)
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
179 L2 = UserList.UserList(L)
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
180 p2 = weakref.proxy(L2)
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
181 self.assertEqual(p, p2)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31454
diff changeset
182 ## self.assertEqual(repr(L2), repr(p2))
22946
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
183 L3 = UserList.UserList(range(10))
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
184 p3 = weakref.proxy(L3)
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
185 self.assertEqual(L3[:], p3[:])
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
186 self.assertEqual(L3[5:], p3[5:])
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
187 self.assertEqual(L3[:5], p3[:5])
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
188 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
189
57346
8793ff8f4826 #5037 proxy __unicode__ correctly
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
190 def test_proxy_unicode(self):
8793ff8f4826 #5037 proxy __unicode__ correctly
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
191 # See bug 5037
8793ff8f4826 #5037 proxy __unicode__ correctly
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
192 class C(object):
8793ff8f4826 #5037 proxy __unicode__ correctly
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
193 def __str__(self):
8793ff8f4826 #5037 proxy __unicode__ correctly
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
194 return "string"
8793ff8f4826 #5037 proxy __unicode__ correctly
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
195 def __unicode__(self):
8793ff8f4826 #5037 proxy __unicode__ correctly
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
196 return u"unicode"
8793ff8f4826 #5037 proxy __unicode__ correctly
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
197 instance = C()
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
198 self.assertIn("__unicode__", dir(weakref.proxy(instance)))
57346
8793ff8f4826 #5037 proxy __unicode__ correctly
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
199 self.assertEqual(unicode(weakref.proxy(instance)), u"unicode")
8793ff8f4826 #5037 proxy __unicode__ correctly
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
200
47196
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
201 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
202 class C:
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
203 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
204 return 10
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
205 o = C()
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
206 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
207 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
208
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
209 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
210 class C:
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
211 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
212 return 42
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
213 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
214 return 21
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
215 o = C()
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
216 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
217 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
218 p //= 5
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
219 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
220
31448
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
221 # 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
222 # 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
223 # 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
224 # 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
225 # 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
226 # 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
227 # 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
228
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
229 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
230 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
231
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
232 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
233 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
234
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
235 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
236 o = Object(1)
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
237 p1 = makeref(o, None)
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
238 p2 = makeref(o, None)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
239 self.assertTrue(p1 is p2, "both callbacks were None in the C API")
31448
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
240 del p1, p2
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
241 p1 = makeref(o)
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
242 p2 = makeref(o, None)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
243 self.assertTrue(p1 is p2, "callbacks were NULL, None in the C API")
31448
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
244 del p1, p2
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
245 p1 = makeref(o)
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
246 p2 = makeref(o)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
247 self.assertTrue(p1 is p2, "both callbacks were NULL in the C API")
31448
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
248 del p1, p2
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
249 p1 = makeref(o, None)
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
250 p2 = makeref(o)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
251 self.assertTrue(p1 is p2, "callbacks were None, NULL in the C API")
31448
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
252
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
253 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
254 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
255 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
256
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
257 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
258
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
259 self.assertTrue(type(ref1) is weakref.CallableProxyType,
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
260 "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
261 ref1('twinkies!')
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
262 self.assertTrue(o.bar == 'twinkies!',
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
263 "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
264 ref1(x='Splat.')
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
265 self.assertTrue(o.bar == 'Splat.',
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
266 "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
267
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
268 # 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
269 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
270
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
271 # 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
272 self.assertRaises(TypeError, ref1, 1, 2, 3)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
273
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
274 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
275 o.foo = 1
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
276 self.assertTrue(proxy.foo == 1,
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
277 "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
278 o.foo = 2
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
279 self.assertTrue(proxy.foo == 2,
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
280 "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
281 del o.foo
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
282 self.assertTrue(not hasattr(proxy, 'foo'),
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
283 "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
284
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
285 proxy.foo = 1
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
286 self.assertTrue(o.foo == 1,
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
287 "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
288 proxy.foo = 2
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
289 self.assertTrue(
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
290 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
291 "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
292 del proxy.foo
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
293 self.assertTrue(not hasattr(o, 'foo'),
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
294 "object does not reflect attribute removal via proxy")
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
295
29697
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
296 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
297 # 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
298 class Foo:
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
299 result = None
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
300 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
301 self.result = accessor
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
302 g = Foo()
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
303 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
304 del f[0]
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
305 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
306
34847
51ba8ce8d92a SF bug #1770766: weakref proxy has incorrect __nonzero__ behavior.
Raymond Hettinger <python@rcn.com>
parents: 32530
diff changeset
307 def test_proxy_bool(self):
51ba8ce8d92a SF bug #1770766: weakref proxy has incorrect __nonzero__ behavior.
Raymond Hettinger <python@rcn.com>
parents: 32530
diff changeset
308 # 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
309 class List(list): pass
51ba8ce8d92a SF bug #1770766: weakref proxy has incorrect __nonzero__ behavior.
Raymond Hettinger <python@rcn.com>
parents: 32530
diff changeset
310 lyst = List()
51ba8ce8d92a SF bug #1770766: weakref proxy has incorrect __nonzero__ behavior.
Raymond Hettinger <python@rcn.com>
parents: 32530
diff changeset
311 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
312
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
313 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
314 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
315 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
316 ref2 = weakref.ref(o, self.callback)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
317 self.assertTrue(weakref.getweakrefcount(o) == 2,
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
318 "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
319
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
320 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
321 proxy2 = weakref.proxy(o, self.callback)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
322 self.assertTrue(weakref.getweakrefcount(o) == 4,
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
323 "got wrong number of weak reference objects")
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
324
31448
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
325 del ref1, ref2, proxy1, proxy2
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
326 self.assertTrue(weakref.getweakrefcount(o) == 0,
31448
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
327 "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
328 " referent when discarded.")
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
329
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
330 # assumes ints do not support weakrefs
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
331 self.assertTrue(weakref.getweakrefcount(1) == 0,
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
332 "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
333
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
334 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
335 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
336 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
337 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
338 del ref1
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
339 self.assertTrue(weakref.getweakrefs(o) == [ref2],
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
340 "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
341
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
342 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
343 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
344 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
345 del ref2
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
346 self.assertTrue(weakref.getweakrefs(o) == [ref1],
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
347 "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
348
31448
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
349 del ref1
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
350 self.assertTrue(weakref.getweakrefs(o) == [],
31448
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
351 "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
352
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
353 # assumes ints do not support weakrefs
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
354 self.assertTrue(weakref.getweakrefs(1) == [],
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
355 "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
356
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
357 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
358 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
359 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
360 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
361 p = weakref.proxy(f)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
362 self.assertTrue(p + 1.0 == 3.0)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
363 self.assertTrue(1.0 + p == 3.0) # this used to SEGV
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
364
21340
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
365 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
366 # 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
367 # 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
368 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
369 pass
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
370 data = {}
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
371 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
372 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
373 def encapsulate():
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
374 f = lambda : ()
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
375 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
376 raise BogusError
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
377 try:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
378 encapsulate()
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
379 except BogusError:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
380 pass
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
381 else:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
382 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
383 try:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
384 encapsulate()
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
385 except BogusError:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
386 pass
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
387 else:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
388 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
389
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
390 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
391 # "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
392 # 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
393 # 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
394 # 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
395 # 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
396 # 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
397 # 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
398 # 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
399 # 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
400
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 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
402
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 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
404 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
405
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 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
407 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
408 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
409
30916
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
410 # 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
411 del wr
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
412
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
413 c1 = C()
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
414 c1.i = C()
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
415 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
416
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
417 c2 = C()
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
418 c2.c1 = c1
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
419 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
420
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
421 # 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
422 # 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
423 # 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
424 # 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
425 # 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
426 # 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
427 # deallocation of c2.
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
428 del c2
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
429
30988
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
430 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
431 import gc
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
432
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
433 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
434 pass
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
435
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
436 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
437 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
438 self.J
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
439
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
440 I = II()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
441 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
442 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
443
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
444 # 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
445 # 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
446 # 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
447 # 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
448 # 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
449 # 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
450 # 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
451 # trash.
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
452 #
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
453 # 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
454 # 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
455 # 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
456 # 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
457 # 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
458 # 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
459 # 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
460 # 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
461 # 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
462 # 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
463 # 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
464 # 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
465 # 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
466 # 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
467 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
468 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
469
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
470 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
471 import gc
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
472
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
473 # 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
474 # 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
475 # 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
476 # 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
477 # __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
478 # 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
479 # __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
480 # 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
481 # 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
482 # "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
483 # 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
484
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
485 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
486 pass
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
487
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
488 class II:
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
489 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
490 self.J
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
491
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
492 I = II()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
493 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
494 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
495
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
496 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
497 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
498
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
499 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
500 import gc
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
501
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
502 # 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
503 # 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
504 # 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
505 # 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
506 # 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
507
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
508 class C:
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
509 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
510 self.me
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
511 self.c1
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
512 self.wr
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
513
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
514 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
515
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
516 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
517 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
518 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
519
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
520 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
521 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
522
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
523 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
524 import gc
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
525
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
526 # 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
527 # 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
528 # 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
529 # 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
530 # 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
531 # 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
532
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
533 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
534 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
535 self.me
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
536 self.c1
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
537 self.wr
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
538
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
539 class D:
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
540 pass
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
541
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
542 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
543
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
544 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
545 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
546 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
547
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
548 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
549 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
550
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
551 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
552 import gc
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
553
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
554 # 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
555 # 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
556 # 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
557 # 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
558 # 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
559 # 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
560 # 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
561 # 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
562
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
563 alist = []
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
564 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
565 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
566 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
567
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
568 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
569 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
570
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
571 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
572 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
573 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
574 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
575 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
576
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
577 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
578 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
579 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
580
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
581 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
582 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
583
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
584 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
585 # 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
586 # 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
587 # 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
588 # 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
589 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
590 # 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
591 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
592
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
593 del alist[:]
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
594 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
595 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
596
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
597 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
598 import gc
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
599
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
600 # 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
601 alist = []
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
602 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
603 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
604
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
605 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
606 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
607 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
608
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
609 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
610 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
611 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
612 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
613 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
614 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
615 external_wr = weakref.ref(callback, safe_callback) # but this will
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
616 self.assertTrue(external_wr() is callback)
30988
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
617
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
618 # 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
619 # 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
620 # 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
621 # 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
622 # -- 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
623 # 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
624
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
625 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
626 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
627 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
628 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
629 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
630
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
631 del alist[:]
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
632 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
633 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
634
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
635 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
636 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
637
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
638 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
639 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
640
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
641 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
642 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
643 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
644 gc.collect()
31522
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
645 class A:
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
646 pass
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
647
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
648 def callback(*args):
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
649 pass
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
650
31522
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
651 referenced = A()
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
652
31522
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
653 a = A()
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
654 a.a = a
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
655 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
656
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
657 try:
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
658 # 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
659 # cyclic trash:
31522
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
660 a = A()
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
661 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
662
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
663 finally:
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
664 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
665
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
666 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
667 # 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
668 # 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
669 # 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
670 # 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
671 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
672 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
673 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
674 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
675
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 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
677
49120
73cc7b6355b8 Fix #3634 invalid return value from _weakref.ref(Exception).__init__
Benjamin Peterson <benjamin@python.org>
parents: 47732
diff changeset
678 def test_init(self):
73cc7b6355b8 Fix #3634 invalid return value from _weakref.ref(Exception).__init__
Benjamin Peterson <benjamin@python.org>
parents: 47732
diff changeset
679 # Issue 3634
73cc7b6355b8 Fix #3634 invalid return value from _weakref.ref(Exception).__init__
Benjamin Peterson <benjamin@python.org>
parents: 47732
diff changeset
680 # <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
681 r = weakref.ref(Exception)
73cc7b6355b8 Fix #3634 invalid return value from _weakref.ref(Exception).__init__
Benjamin Peterson <benjamin@python.org>
parents: 47732
diff changeset
682 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
683 # 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
684 gc.collect()
73cc7b6355b8 Fix #3634 invalid return value from _weakref.ref(Exception).__init__
Benjamin Peterson <benjamin@python.org>
parents: 47732
diff changeset
685
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
686
47732
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
687 class SubclassableWeakrefTestCase(TestBase):
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
688
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
689 def test_subclass_refs(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
690 class MyRef(weakref.ref):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
691 def __init__(self, ob, callback=None, value=42):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
692 self.value = value
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
693 super(MyRef, self).__init__(ob, callback)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
694 def __call__(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
695 self.called = True
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
696 return super(MyRef, self).__call__()
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
697 o = Object("foo")
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
698 mr = MyRef(o, value=24)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
699 self.assertTrue(mr() is o)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
700 self.assertTrue(mr.called)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
701 self.assertEqual(mr.value, 24)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
702 del o
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
703 self.assertTrue(mr() is None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
704 self.assertTrue(mr.called)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
705
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
706 def test_subclass_refs_dont_replace_standard_refs(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
707 class MyRef(weakref.ref):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
708 pass
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
709 o = Object(42)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
710 r1 = MyRef(o)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
711 r2 = weakref.ref(o)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
712 self.assertTrue(r1 is not r2)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
713 self.assertEqual(weakref.getweakrefs(o), [r2, r1])
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
714 self.assertEqual(weakref.getweakrefcount(o), 2)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
715 r3 = MyRef(o)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
716 self.assertEqual(weakref.getweakrefcount(o), 3)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
717 refs = weakref.getweakrefs(o)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
718 self.assertEqual(len(refs), 3)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
719 self.assertTrue(r2 is refs[0])
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
720 self.assertIn(r1, refs[1:])
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
721 self.assertIn(r3, refs[1:])
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
722
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
723 def test_subclass_refs_dont_conflate_callbacks(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
724 class MyRef(weakref.ref):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
725 pass
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
726 o = Object(42)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
727 r1 = MyRef(o, id)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
728 r2 = MyRef(o, str)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
729 self.assertTrue(r1 is not r2)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
730 refs = weakref.getweakrefs(o)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
731 self.assertIn(r1, refs)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
732 self.assertIn(r2, refs)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
733
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
734 def test_subclass_refs_with_slots(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
735 class MyRef(weakref.ref):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
736 __slots__ = "slot1", "slot2"
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
737 def __new__(type, ob, callback, slot1, slot2):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
738 return weakref.ref.__new__(type, ob, callback)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
739 def __init__(self, ob, callback, slot1, slot2):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
740 self.slot1 = slot1
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
741 self.slot2 = slot2
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
742 def meth(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
743 return self.slot1 + self.slot2
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
744 o = Object(42)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
745 r = MyRef(o, None, "abc", "def")
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
746 self.assertEqual(r.slot1, "abc")
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
747 self.assertEqual(r.slot2, "def")
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
748 self.assertEqual(r.meth(), "abcdef")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
749 self.assertFalse(hasattr(r, "__dict__"))
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
750
47732
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
751 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
752 # Bug #3110
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
753 # 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
754 # 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
755 # 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
756 # 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
757 # being deleted.
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
758 class MyRef(weakref.ref):
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
759 pass
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
760
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
761 # 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
762 # to detect refcounting problems
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
763 def callback(w):
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
764 self.cbcalled += 1
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
765
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
766 o = C()
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
767 r1 = MyRef(o, callback)
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
768 r1.o = o
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
769 del o
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
770
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
771 del r1 # Used to crash here
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
772
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
773 self.assertEqual(self.cbcalled, 0)
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
774
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
775 # 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
776 # (since code paths are different)
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
777 o = C()
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
778 r1 = MyRef(o, callback)
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
779 r2 = MyRef(o, callback)
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
780 r1.r = r2
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
781 r2.o = o
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
782 del o
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
783 del r2
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
784
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
785 del r1 # Used to crash here
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
786
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
787 self.assertEqual(self.cbcalled, 0)
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
788
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
789
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
790 class Object:
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
791 def __init__(self, arg):
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
792 self.arg = arg
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
793 def __repr__(self):
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
794 return "<Object %r>" % self.arg
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
795
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
796
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
797 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
798
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
799 COUNT = 10
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
800
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
801 def test_weak_values(self):
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
802 #
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
803 # 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
804 #
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
805 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
806 for o in objects:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
807 self.assertTrue(weakref.getweakrefcount(o) == 1,
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
808 "wrong number of weak references to %r!" % o)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
809 self.assertTrue(o is dict[o.arg],
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
810 "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
811 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
812 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
813 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
814 items2.sort()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
815 self.assertTrue(items1 == 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
816 "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
817 del items1, items2
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
818 self.assertTrue(len(dict) == self.COUNT)
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
819 del objects[0]
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
820 self.assertTrue(len(dict) == (self.COUNT - 1),
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
821 "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
822 del objects, o
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
823 self.assertTrue(len(dict) == 0,
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
824 "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
825 # regression on SF bug #447152:
e90a658a58d4 Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
Fred Drake <fdrake@acm.org>
parents: 17767
diff changeset
826 dict = weakref.WeakValueDictionary()
e90a658a58d4 Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
Fred Drake <fdrake@acm.org>
parents: 17767
diff changeset
827 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
828 dict[2] = C()
e90a658a58d4 Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
Fred Drake <fdrake@acm.org>
parents: 17767
diff changeset
829 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
830
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
831 def test_weak_keys(self):
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
832 #
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
833 # This exercises d.copy(), d.items(), d[] = v, d[], del d[],
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
834 # len(d), d.has_key().
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
835 #
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
836 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
837 for o in objects:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
838 self.assertTrue(weakref.getweakrefcount(o) == 1,
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
839 "wrong number of weak references to %r!" % o)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
840 self.assertTrue(o.arg is dict[o],
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
841 "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
842 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
843 items2 = dict.copy().items()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
844 self.assertTrue(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
845 "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
846 del items1, items2
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
847 self.assertTrue(len(dict) == self.COUNT)
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
848 del objects[0]
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
849 self.assertTrue(len(dict) == (self.COUNT - 1),
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
850 "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
851 del objects, o
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
852 self.assertTrue(len(dict) == 0,
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
853 "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
854 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
855 dict[o] = "What is the meaning of the universe?"
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
856 self.assertTrue(dict.has_key(o))
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
857 self.assertTrue(not dict.has_key(34))
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
858
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
859 def test_weak_keyed_iters(self):
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
860 dict, objects = self.make_weak_keyed_dict()
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
861 self.check_iters(dict)
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
862
38079
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
863 # Test keyrefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
864 refs = dict.keyrefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
865 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
866 objects2 = list(objects)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
867 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
868 ob = wr()
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
869 self.assertTrue(dict.has_key(ob))
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
870 self.assertIn(ob, dict)
38079
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
871 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
872 objects2.remove(ob)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
873 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
874
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
875 # Test iterkeyrefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
876 objects2 = list(objects)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
877 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
878 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
879 ob = wr()
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
880 self.assertTrue(dict.has_key(ob))
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
881 self.assertIn(ob, dict)
38079
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
882 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
883 objects2.remove(ob)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
884 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
885
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
886 def test_weak_valued_iters(self):
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
887 dict, objects = self.make_weak_valued_dict()
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
888 self.check_iters(dict)
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
889
38079
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
890 # Test valuerefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
891 refs = dict.valuerefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
892 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
893 objects2 = list(objects)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
894 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
895 ob = wr()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
896 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
897 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
898 objects2.remove(ob)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
899 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
900
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
901 # Test itervaluerefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
902 objects2 = list(objects)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
903 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
904 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
905 ob = wr()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
906 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
907 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
908 objects2.remove(ob)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
909 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
910
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
911 def check_iters(self, dict):
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
912 # item iterator:
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
913 items = dict.items()
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
914 for item in dict.iteritems():
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
915 items.remove(item)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
916 self.assertTrue(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
917
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
918 # key iterator, via __iter__():
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
919 keys = dict.keys()
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
920 for k in dict:
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
921 keys.remove(k)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
922 self.assertTrue(len(keys) == 0, "__iter__() did not touch all keys")
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
923
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
924 # 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
925 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
926 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
927 keys.remove(k)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
928 self.assertTrue(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
929
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
930 # value iterator:
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
931 values = dict.values()
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
932 for v in dict.itervalues():
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
933 values.remove(v)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
934 self.assertTrue(len(values) == 0,
29862
f339f29b06cb stylistic nits:
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
935 "itervalues() did not touch all values")
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
936
23764
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
937 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
938 o = Object(3)
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
939 dict = weakref.WeakKeyDictionary({o:364})
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
940 self.assertTrue(dict[o] == 364)
23764
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
941
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
942 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
943 o = Object(3)
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
944 dict = weakref.WeakKeyDictionary({o:364})
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
945 dict2 = weakref.WeakKeyDictionary(dict)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
946 self.assertTrue(dict[o] == 364)
23764
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
947
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
948 def make_weak_keyed_dict(self):
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
949 dict = weakref.WeakKeyDictionary()
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
950 objects = map(Object, range(self.COUNT))
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
951 for o in objects:
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
952 dict[o] = o.arg
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
953 return dict, objects
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
954
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
955 def make_weak_valued_dict(self):
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
956 dict = weakref.WeakValueDictionary()
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
957 objects = map(Object, range(self.COUNT))
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
958 for o in objects:
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
959 dict[o.arg] = o
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
960 return dict, objects
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
961
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
962 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
963 weakdict = klass()
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
964 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
965 weakdict[key2] = value2
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
966 self.assertTrue(len(weakdict) == 2)
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
967 k, v = weakdict.popitem()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
968 self.assertTrue(len(weakdict) == 1)
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
969 if k is key1:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
970 self.assertTrue(v is value1)
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
971 else:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
972 self.assertTrue(v is value2)
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
973 k, v = weakdict.popitem()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
974 self.assertTrue(len(weakdict) == 0)
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
975 if k is key1:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
976 self.assertTrue(v is value1)
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
977 else:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
978 self.assertTrue(v is value2)
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
979
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
980 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
981 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
982 "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
983
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
984 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
985 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
986 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
987
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
988 def check_setdefault(self, klass, key, value1, value2):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
989 self.assertTrue(value1 is not value2,
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
990 "invalid test"
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
991 " -- 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
992 weakdict = klass()
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
993 o = weakdict.setdefault(key, value1)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
994 self.assertTrue(o is value1)
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
995 self.assertTrue(weakdict.has_key(key))
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
996 self.assertTrue(weakdict.get(key) is value1)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
997 self.assertTrue(weakdict[key] is value1)
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
998
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
999 o = weakdict.setdefault(key, value2)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1000 self.assertTrue(o is value1)
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
1001 self.assertTrue(weakdict.has_key(key))
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1002 self.assertTrue(weakdict.get(key) is value1)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1003 self.assertTrue(weakdict[key] is value1)
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1004
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1005 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
1006 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
1007 "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
1008
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1009 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
1010 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
1011 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
1012
17547
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1013 def check_update(self, klass, dict):
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1014 #
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
1015 # This exercises d.update(), len(d), d.keys(), d.has_key(),
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1016 # d.get(), d[].
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1017 #
17547
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1018 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
1019 weakdict.update(dict)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1020 self.assertTrue(len(weakdict) == len(dict))
17547
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1021 for k in weakdict.keys():
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
1022 self.assertTrue(dict.has_key(k),
17547
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1023 "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
1024 v = dict.get(k)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1025 self.assertTrue(v is weakdict[k])
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1026 self.assertTrue(v is weakdict.get(k))
17547
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1027 for k in dict.keys():
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
1028 self.assertTrue(weakdict.has_key(k),
17547
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1029 "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
1030 v = dict[k]
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1031 self.assertTrue(v is weakdict[k])
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1032 self.assertTrue(v is weakdict.get(k))
17547
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1033
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1034 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
1035 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
1036 {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
1037
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1038 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
1039 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
1040 {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
1041
19764
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1042 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
1043 d = weakref.WeakKeyDictionary()
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1044 o1 = Object('1')
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1045 o2 = Object('2')
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1046 d[o1] = 'something'
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1047 d[o2] = 'something'
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1048 self.assertTrue(len(d) == 2)
19764
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1049 del d[o1]
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1050 self.assertTrue(len(d) == 1)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1051 self.assertTrue(d.keys() == [o2])
19764
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1052
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1053 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
1054 d = weakref.WeakValueDictionary()
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1055 o1 = Object('1')
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1056 o2 = Object('2')
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1057 d['something'] = o1
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1058 d['something else'] = o2
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1059 self.assertTrue(len(d) == 2)
19764
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1060 del d['something']
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1061 self.assertTrue(len(d) == 1)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1062 self.assertTrue(d.items() == [('something else', o2)])
19764
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1063
29257
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1064 def test_weak_keyed_bad_delitem(self):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1065 d = weakref.WeakKeyDictionary()
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1066 o = Object('1')
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1067 # 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
1068 # 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
1069 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
1070 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
1071
18c020463965 Fleshed out WeakKeyDictionary.__delitem__ NEWS to cover issues raised on
Tim Peters <tim.peters@gmail.com>
parents: 29257
diff changeset
1072 # 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
1073 # __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
1074 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
1075 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
1076 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
1077
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1078 def test_weak_keyed_cascading_deletes(self):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1079 # 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
1080 # 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
1081 # 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
1082
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1083 d = weakref.WeakKeyDictionary()
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1084 mutate = False
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1085
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1086 class C(object):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1087 def __init__(self, i):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1088 self.value = i
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1089 def __hash__(self):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1090 return hash(self.value)
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1091 def __eq__(self, other):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1092 if mutate:
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1093 # 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
1094 # last strong reference to a key.
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1095 del objs[-1]
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1096 return self.value == other.value
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1097
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1098 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
1099 for o in objs:
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1100 d[o] = o.value
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1101 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
1102 # 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
1103 objs = d.keys()
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1104 # 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
1105 # 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
1106 objs.reverse()
29258
18c020463965 Fleshed out WeakKeyDictionary.__delitem__ NEWS to cover issues raised on
Tim Peters <tim.peters@gmail.com>
parents: 29257
diff changeset
1107
29257
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1108 # 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
1109 # 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
1110 # 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
1111 # RuntimeError: dictionary changed size during iteration
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1112 # 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
1113 # 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
1114 # "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
1115 mutate = True
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1116 count = 0
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1117 for o in objs:
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1118 count += 1
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1119 del d[o]
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1120 self.assertEqual(len(d), 0)
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1121 self.assertEqual(count, 2)
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1122
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
1123 from test import mapping_tests
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1124
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
1125 class WeakValueDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
29862
f339f29b06cb stylistic nits:
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
1126 """Check that WeakValueDictionary conforms to the mapping protocol"""
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1127 __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
1128 type2test = weakref.WeakValueDictionary
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1129 def _reference(self):
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1130 return self.__ref.copy()
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1131
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
1132 class WeakKeyDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
29862
f339f29b06cb stylistic nits:
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
1133 """Check that WeakKeyDictionary conforms to the mapping protocol"""
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1134 __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
1135 type2test = weakref.WeakKeyDictionary
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1136 def _reference(self):
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1137 return self.__ref.copy()
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1138
47196
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
1139 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
1140
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1141 >>> import weakref
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1142 >>> class Dict(dict):
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1143 ... pass
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1144 ...
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1145 >>> 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
1146 >>> r = weakref.ref(obj)
38464
e989ee5d404a ("Forward-port" of r46506)
Armin Rigo <arigo@tunes.org>
parents: 38079
diff changeset
1147 >>> print r() is obj
e989ee5d404a ("Forward-port" of r46506)
Armin Rigo <arigo@tunes.org>
parents: 38079
diff changeset
1148 True
35195
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1149
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1150 >>> import weakref
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1151 >>> class Object:
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1152 ... pass
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1153 ...
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1154 >>> o = Object()
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1155 >>> r = weakref.ref(o)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1156 >>> o2 = r()
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1157 >>> o is o2
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1158 True
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1159 >>> del o, o2
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1160 >>> print r()
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1161 None
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1162
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1163 >>> import weakref
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1164 >>> class ExtendedRef(weakref.ref):
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1165 ... def __init__(self, ob, callback=None, **annotations):
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1166 ... super(ExtendedRef, self).__init__(ob, callback)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1167 ... self.__counter = 0
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1168 ... for k, v in annotations.iteritems():
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1169 ... setattr(self, k, v)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1170 ... def __call__(self):
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1171 ... '''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
1172 ... times the reference has been called.
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1173 ... '''
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1174 ... ob = super(ExtendedRef, self).__call__()
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1175 ... if ob is not None:
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1176 ... self.__counter += 1
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1177 ... ob = (ob, self.__counter)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1178 ... return ob
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1179 ...
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1180 >>> 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
1181 ... pass
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1182 ...
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1183 >>> a = A()
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1184 >>> r = ExtendedRef(a, foo=1, bar="baz")
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1185 >>> r.foo
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1186 1
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1187 >>> r.bar
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1188 'baz'
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1189 >>> r()[1]
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1190 1
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1191 >>> r()[1]
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1192 2
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1193 >>> r()[0] is a
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1194 True
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1195
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 >>> import weakref
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1198 >>> _id2obj_dict = weakref.WeakValueDictionary()
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1199 >>> def remember(obj):
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1200 ... oid = id(obj)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1201 ... _id2obj_dict[oid] = obj
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1202 ... return oid
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1203 ...
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1204 >>> def id2obj(oid):
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1205 ... return _id2obj_dict[oid]
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1206 ...
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1207 >>> a = A() # from here, just testing
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1208 >>> a_id = remember(a)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1209 >>> id2obj(a_id) is a
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1210 True
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1211 >>> del a
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1212 >>> try:
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1213 ... id2obj(a_id)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1214 ... except KeyError:
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1215 ... print 'OK'
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1216 ... else:
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1217 ... print 'WeakValueDictionary error'
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1218 OK
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1219
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 __test__ = {'libreftest' : libreftest}
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1223
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19764
diff changeset
1224 def test_main():
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28240
diff changeset
1225 test_support.run_unittest(
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28240
diff changeset
1226 ReferencesTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28240
diff changeset
1227 MappingTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28240
diff changeset
1228 WeakValueDictionaryTestCase,
29862
f339f29b06cb stylistic nits:
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
1229 WeakKeyDictionaryTestCase,
47732
b8e9cc28022c Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47196
diff changeset
1230 SubclassableWeakrefTestCase,
29862
f339f29b06cb stylistic nits:
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
1231 )
35195
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1232 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
1233
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 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
1236 test_main()