annotate Lib/test/test_weakref.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 13b74c0b040c
children 7e771f0363e2
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
40705
293311e63a73 If you created a weakref in an object's __del__ method to itself it would
Brett Cannon <bcannon@gmail.com>
parents: 38464
diff changeset
10 # Used in ReferencesTestCase.test_ref_created_during_del() .
293311e63a73 If you created a weakref in an object's __del__ method to itself it would
Brett Cannon <bcannon@gmail.com>
parents: 38464
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
80400
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
36 class Object:
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
37 def __init__(self, arg):
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
38 self.arg = arg
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
39 def __repr__(self):
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
40 return "<Object %r>" % self.arg
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
41 def __eq__(self, other):
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
42 if isinstance(other, Object):
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
43 return self.arg == other.arg
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
44 return NotImplemented
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
45 def __ne__(self, other):
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
46 if isinstance(other, Object):
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
47 return self.arg != other.arg
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
48 return NotImplemented
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
49 def __hash__(self):
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
50 return hash(self.arg)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
51
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
52 class RefCycle:
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
53 def __init__(self):
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
54 self.cycle = self
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
55
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
56
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
57 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
58
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
59 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
60 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
61
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 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
63 self.cbcalled += 1
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
64
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
66 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
67
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_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
69 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
70 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
71 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
72 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
73
22946
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
74 # 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
75 # Live reference:
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
76 o = C()
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
77 wr = weakref.ref(o)
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57349
diff changeset
78 repr(wr)
22946
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
79 # Dead reference:
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
80 del o
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57349
diff changeset
81 repr(wr)
22946
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
82
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
83 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
84 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
85 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
86 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
87 self.check_basic_callback(create_unbound_method)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
88
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
89 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
90 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
91 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
92 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
93 del o
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
94 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
95 "expected reference to be invalidated")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
96 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
97 "expected reference to be invalidated")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
98 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
99 "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
100
17498
f72dcd67cccb Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents: 17385
diff changeset
101 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
102 # 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
103 #
f72dcd67cccb Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents: 17385
diff changeset
104 # 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
105 # 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
106 # (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
107 # 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
108 # 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
109 # 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
110 # 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
111 #
f72dcd67cccb Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents: 17385
diff changeset
112 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
113 self.ref()
f72dcd67cccb Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents: 17385
diff changeset
114 c = C()
f72dcd67cccb Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents: 17385
diff changeset
115 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
116 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
117 del c
f72dcd67cccb Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents: 17385
diff changeset
118
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
119 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
120 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
121 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
122 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
123 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
124 del o
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
125
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
126 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
127 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
128
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
129 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
130 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
131 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
132 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
133
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
134 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
135 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
136 ref = weakref.ref(o)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
137 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
138 "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
139 o2 = ref()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
140 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
141 "<ref>() should return original object if live")
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
142
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
143 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
144 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
145 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
146 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
147 del o
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
148 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
149 "callback did not properly set 'cbcalled'")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
150 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
151 "ref2 should be dead after deleting object reference")
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
152
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
153 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
154 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
155 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
156 # 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
157 # 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
158 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
159 ref2 = weakref.ref(o)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
160 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
161 "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
162
17172
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 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
165 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
166 ref2 = weakref.ref(o)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
167 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
168 "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
169 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
170 "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
171 del proxy
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
172 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
173 "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
174
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
175 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
176 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
177 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
178 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
179 proxy2 = weakref.proxy(o)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
180 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
181 "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
182
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
183 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
184 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
185 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
186
21484
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
187 L = UserList.UserList()
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
188 p = weakref.proxy(L)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
189 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
190 p.append(12)
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
191 self.assertEqual(len(L), 1)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
192 self.assertTrue(p, "proxy for non-empty UserList should be true")
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
193 with test_support.check_py3k_warnings():
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57349
diff changeset
194 p[:] = [2, 3]
21484
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
195 self.assertEqual(len(L), 2)
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
196 self.assertEqual(len(p), 2)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
197 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
198 p[1] = 5
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
199 self.assertEqual(L[1], 5)
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
200 self.assertEqual(p[1], 5)
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
201 L2 = UserList.UserList(L)
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
202 p2 = weakref.proxy(L2)
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
203 self.assertEqual(p, p2)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31454
diff changeset
204 ## self.assertEqual(repr(L2), repr(p2))
22946
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
205 L3 = UserList.UserList(range(10))
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
206 p3 = weakref.proxy(L3)
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
207 with test_support.check_py3k_warnings():
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57349
diff changeset
208 self.assertEqual(L3[:], p3[:])
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57349
diff changeset
209 self.assertEqual(L3[5:], p3[5:])
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57349
diff changeset
210 self.assertEqual(L3[:5], p3[:5])
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57349
diff changeset
211 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
212
57349
d5779e159ddb Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
213 def test_proxy_unicode(self):
d5779e159ddb Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
214 # See bug 5037
d5779e159ddb Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
215 class C(object):
d5779e159ddb Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
216 def __str__(self):
d5779e159ddb Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
217 return "string"
d5779e159ddb Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
218 def __unicode__(self):
d5779e159ddb Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
219 return u"unicode"
d5779e159ddb Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
220 instance = C()
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
221 self.assertIn("__unicode__", dir(weakref.proxy(instance)))
57349
d5779e159ddb Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
222 self.assertEqual(unicode(weakref.proxy(instance)), u"unicode")
d5779e159ddb Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
223
47196
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
224 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
225 class C:
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
226 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
227 return 10
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
228 o = C()
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
229 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
230 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
231
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
232 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
233 class C:
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
234 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
235 return 42
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
236 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
237 return 21
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
238 o = C()
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
239 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
240 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
241 p //= 5
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
242 self.assertEqual(p, 21)
21484
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
243
31448
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
244 # 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
245 # 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
246 # 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
247 # 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
248 # 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
249 # 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
250 # 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
251
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
252 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
253 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
254
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
255 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
256 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
257
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
258 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
259 o = Object(1)
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
260 p1 = makeref(o, None)
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
261 p2 = makeref(o, None)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
262 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
263 del p1, p2
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
264 p1 = makeref(o)
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
265 p2 = makeref(o, None)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
266 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
267 del p1, p2
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
268 p1 = makeref(o)
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
269 p2 = makeref(o)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
270 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
271 del p1, p2
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
272 p1 = makeref(o, None)
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
273 p2 = makeref(o)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
274 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
275
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
276 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
277 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
278 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
279
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
280 self.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
281
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
282 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
283 "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
284 ref1('twinkies!')
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
285 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
286 "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
287 ref1(x='Splat.')
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
288 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
289 "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
290
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
291 # 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
292 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
293
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
294 # 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
295 self.assertRaises(TypeError, ref1, 1, 2, 3)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
296
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
297 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
298 o.foo = 1
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
299 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
300 "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
301 o.foo = 2
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
302 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
303 "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
304 del o.foo
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
305 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
306 "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
307
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
308 proxy.foo = 1
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
309 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
310 "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
311 proxy.foo = 2
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
312 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
313 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
314 "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
315 del proxy.foo
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
316 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
317 "object does not reflect attribute removal via proxy")
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
318
29697
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
319 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
320 # 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
321 class Foo:
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
322 result = None
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
323 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
324 self.result = accessor
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
325 g = Foo()
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
326 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
327 del f[0]
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
328 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
329
34847
51ba8ce8d92a SF bug #1770766: weakref proxy has incorrect __nonzero__ behavior.
Raymond Hettinger <python@rcn.com>
parents: 32530
diff changeset
330 def test_proxy_bool(self):
51ba8ce8d92a SF bug #1770766: weakref proxy has incorrect __nonzero__ behavior.
Raymond Hettinger <python@rcn.com>
parents: 32530
diff changeset
331 # 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
332 class List(list): pass
51ba8ce8d92a SF bug #1770766: weakref proxy has incorrect __nonzero__ behavior.
Raymond Hettinger <python@rcn.com>
parents: 32530
diff changeset
333 lyst = List()
51ba8ce8d92a SF bug #1770766: weakref proxy has incorrect __nonzero__ behavior.
Raymond Hettinger <python@rcn.com>
parents: 32530
diff changeset
334 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
335
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
336 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
337 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
338 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
339 ref2 = weakref.ref(o, self.callback)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
340 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
341 "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
342
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
343 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
344 proxy2 = weakref.proxy(o, self.callback)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
345 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
346 "got wrong number of weak reference objects")
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
347
31448
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
348 del ref1, ref2, proxy1, proxy2
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
349 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
350 "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
351 " referent when discarded.")
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.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
355 "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
356
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
357 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
358 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
359 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
360 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
361 del ref1
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
362 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
363 "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
364
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
365 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
366 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
367 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
368 del ref2
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
369 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
370 "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
371
31448
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
372 del ref1
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
373 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
374 "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
375
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
376 # assumes ints do not support weakrefs
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
377 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
378 "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
379
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
380 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
381 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
382 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
383 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
384 p = weakref.proxy(f)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
385 self.assertTrue(p + 1.0 == 3.0)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
386 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
387
21340
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
388 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
389 # 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
390 # 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
391 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
392 pass
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
393 data = {}
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
394 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
395 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
396 def encapsulate():
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
397 f = lambda : ()
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
398 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
399 raise BogusError
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
400 try:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
401 encapsulate()
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
402 except BogusError:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
403 pass
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
404 else:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
405 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
406 try:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
407 encapsulate()
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
408 except BogusError:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
409 pass
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
410 else:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
411 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
412
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
413 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
414 # "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
415 # 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
416 # 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
417 # 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
418 # 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
419 # 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
420 # 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
421 # 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
422 # 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
423
cd7624b025a0 Backport of fix for SF bug 840829, memory corruption in some cases of
Tim Peters <tim.peters@gmail.com>
parents: 29862
diff changeset
424 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
425
cd7624b025a0 Backport of fix for SF bug 840829, memory corruption in some cases of
Tim Peters <tim.peters@gmail.com>
parents: 29862
diff changeset
426 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
427 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
428
cd7624b025a0 Backport of fix for SF bug 840829, memory corruption in some cases of
Tim Peters <tim.peters@gmail.com>
parents: 29862
diff changeset
429 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
430 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
431 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
432
30916
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
433 # 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
434 del wr
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
435
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
436 c1 = C()
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
437 c1.i = C()
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
438 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
439
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
440 c2 = C()
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
441 c2.c1 = c1
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
442 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
443
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
444 # 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
445 # 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
446 # 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
447 # 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
448 # 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
449 # 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
450 # deallocation of c2.
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
451 del c2
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
452
30988
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
453 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
454 import gc
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
455
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
456 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
457 pass
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
458
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
459 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
460 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
461 self.J
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
462
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
463 I = II()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
464 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
465 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
466
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
467 # 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
468 # 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
469 # 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
470 # 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
471 # 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
472 # 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
473 # 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
474 # trash.
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
475 #
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
476 # 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
477 # 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
478 # 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
479 # 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
480 # 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
481 # 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
482 # 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
483 # 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
484 # 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
485 # 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
486 # 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
487 # 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
488 # 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
489 # 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
490 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
491 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
492
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
493 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
494 import gc
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 # 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
497 # 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
498 # 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
499 # 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
500 # __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
501 # 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
502 # __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
503 # 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
504 # 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
505 # "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
506 # 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
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 J(object):
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
509 pass
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
510
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
511 class II:
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
512 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
513 self.J
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
514
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
515 I = II()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
516 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
517 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
518
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
519 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
520 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
521
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
522 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
523 import gc
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
524
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
525 # 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
526 # 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
527 # 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
528 # 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
529 # 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
530
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
531 class C:
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
532 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
533 self.me
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
534 self.c1
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
535 self.wr
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
536
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
537 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
538
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
539 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
540 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
541 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
542
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
543 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
544 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
545
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
546 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
547 import gc
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
548
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
549 # 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
550 # 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
551 # 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
552 # 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
553 # 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
554 # 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
555
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
556 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
557 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
558 self.me
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
559 self.c1
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
560 self.wr
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
561
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
562 class D:
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
563 pass
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
564
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
565 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
566
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
567 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
568 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
569 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
570
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
571 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
572 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
573
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
574 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
575 import gc
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 # 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
578 # 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
579 # 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
580 # 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
581 # 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
582 # 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
583 # 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
584 # 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
585
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
586 alist = []
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
587 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
588 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
589 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
590
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
591 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
592 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
593
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
594 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
595 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
596 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
597 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
598 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
599
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
600 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
601 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
602 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
603
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
604 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
605 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
606
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
607 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
608 # 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
609 # 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
610 # 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
611 # 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
612 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
613 # 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
614 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
615
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
616 del alist[:]
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
617 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
618 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
619
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
620 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
621 import gc
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
622
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
623 # 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
624 alist = []
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
625 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
626 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
627
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
628 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
629 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
630 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
631
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
632 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
633 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
634 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
635 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
636 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
637 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
638 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
639 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
640
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
641 # 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
642 # 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
643 # 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
644 # 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
645 # -- 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
646 # 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
647
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
648 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
649 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
650 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
651 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
652 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
653
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
654 del alist[:]
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
655 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
656 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
657
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
658 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
659 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
660
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
661 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
662 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
663
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
664 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
665 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
666 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
667 gc.collect()
31522
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
668 class A:
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
669 pass
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
670
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
671 def callback(*args):
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
672 pass
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
673
31522
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
674 referenced = A()
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
675
31522
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
676 a = A()
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
677 a.a = a
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
678 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
679
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
680 try:
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
681 # 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
682 # cyclic trash:
31522
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
683 a = A()
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
684 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
685
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
686 finally:
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
687 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
688
40705
293311e63a73 If you created a weakref in an object's __del__ method to itself it would
Brett Cannon <bcannon@gmail.com>
parents: 38464
diff changeset
689 def test_ref_created_during_del(self):
293311e63a73 If you created a weakref in an object's __del__ method to itself it would
Brett Cannon <bcannon@gmail.com>
parents: 38464
diff changeset
690 # Bug #1377858
293311e63a73 If you created a weakref in an object's __del__ method to itself it would
Brett Cannon <bcannon@gmail.com>
parents: 38464
diff changeset
691 # A weakref created in an object's __del__() would crash the
293311e63a73 If you created a weakref in an object's __del__ method to itself it would
Brett Cannon <bcannon@gmail.com>
parents: 38464
diff changeset
692 # interpreter when the weakref was cleaned up since it would refer to
293311e63a73 If you created a weakref in an object's __del__ method to itself it would
Brett Cannon <bcannon@gmail.com>
parents: 38464
diff changeset
693 # non-existent memory. This test should not segfault the interpreter.
293311e63a73 If you created a weakref in an object's __del__ method to itself it would
Brett Cannon <bcannon@gmail.com>
parents: 38464
diff changeset
694 class Target(object):
293311e63a73 If you created a weakref in an object's __del__ method to itself it would
Brett Cannon <bcannon@gmail.com>
parents: 38464
diff changeset
695 def __del__(self):
293311e63a73 If you created a weakref in an object's __del__ method to itself it would
Brett Cannon <bcannon@gmail.com>
parents: 38464
diff changeset
696 global ref_from_del
293311e63a73 If you created a weakref in an object's __del__ method to itself it would
Brett Cannon <bcannon@gmail.com>
parents: 38464
diff changeset
697 ref_from_del = weakref.ref(self)
293311e63a73 If you created a weakref in an object's __del__ method to itself it would
Brett Cannon <bcannon@gmail.com>
parents: 38464
diff changeset
698
293311e63a73 If you created a weakref in an object's __del__ method to itself it would
Brett Cannon <bcannon@gmail.com>
parents: 38464
diff changeset
699 w = Target()
293311e63a73 If you created a weakref in an object's __del__ method to itself it would
Brett Cannon <bcannon@gmail.com>
parents: 38464
diff changeset
700
49120
73cc7b6355b8 Fix #3634 invalid return value from _weakref.ref(Exception).__init__
Benjamin Peterson <benjamin@python.org>
parents: 47732
diff changeset
701 def test_init(self):
73cc7b6355b8 Fix #3634 invalid return value from _weakref.ref(Exception).__init__
Benjamin Peterson <benjamin@python.org>
parents: 47732
diff changeset
702 # Issue 3634
73cc7b6355b8 Fix #3634 invalid return value from _weakref.ref(Exception).__init__
Benjamin Peterson <benjamin@python.org>
parents: 47732
diff changeset
703 # <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
704 r = weakref.ref(Exception)
73cc7b6355b8 Fix #3634 invalid return value from _weakref.ref(Exception).__init__
Benjamin Peterson <benjamin@python.org>
parents: 47732
diff changeset
705 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
706 # 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
707 gc.collect()
73cc7b6355b8 Fix #3634 invalid return value from _weakref.ref(Exception).__init__
Benjamin Peterson <benjamin@python.org>
parents: 47732
diff changeset
708
60112
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
709 def test_classes(self):
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
710 # Check that both old-style classes and new-style classes
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
711 # are weakrefable.
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
712 class A(object):
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
713 pass
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
714 class B:
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
715 pass
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
716 l = []
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
717 weakref.ref(int)
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
718 a = weakref.ref(A, l.append)
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
719 A = None
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
720 gc.collect()
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
721 self.assertEqual(a(), None)
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
722 self.assertEqual(l, [a])
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
723 b = weakref.ref(B, l.append)
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
724 B = None
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
725 gc.collect()
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
726 self.assertEqual(b(), None)
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
727 self.assertEqual(l, [a, b])
7b8ba019cd15 Issue #8268: Old-style classes (not just instances) now support weak
Antoine Pitrou <solipsis@pitrou.net>
parents: 59783
diff changeset
728
80400
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
729 def test_equality(self):
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
730 # Alive weakrefs defer equality testing to their underlying object.
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
731 x = Object(1)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
732 y = Object(1)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
733 z = Object(2)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
734 a = weakref.ref(x)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
735 b = weakref.ref(y)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
736 c = weakref.ref(z)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
737 d = weakref.ref(x)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
738 # Note how we directly test the operators here, to stress both
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
739 # __eq__ and __ne__.
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
740 self.assertTrue(a == b)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
741 self.assertFalse(a != b)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
742 self.assertFalse(a == c)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
743 self.assertTrue(a != c)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
744 self.assertTrue(a == d)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
745 self.assertFalse(a != d)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
746 del x, y, z
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
747 gc.collect()
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
748 for r in a, b, c:
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
749 # Sanity check
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
750 self.assertIs(r(), None)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
751 # Dead weakrefs compare by identity: whether `a` and `d` are the
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
752 # same weakref object is an implementation detail, since they pointed
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
753 # to the same original object and didn't have a callback.
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
754 # (see issue #16453).
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
755 self.assertFalse(a == b)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
756 self.assertTrue(a != b)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
757 self.assertFalse(a == c)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
758 self.assertTrue(a != c)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
759 self.assertEqual(a == d, a is d)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
760 self.assertEqual(a != d, a is not d)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
761
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
762 def test_hashing(self):
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
763 # Alive weakrefs hash the same as the underlying object
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
764 x = Object(42)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
765 y = Object(42)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
766 a = weakref.ref(x)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
767 b = weakref.ref(y)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
768 self.assertEqual(hash(a), hash(42))
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
769 del x, y
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
770 gc.collect()
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
771 # Dead weakrefs:
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
772 # - retain their hash is they were hashed when alive;
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
773 # - otherwise, cannot be hashed.
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
774 self.assertEqual(hash(a), hash(42))
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
775 self.assertRaises(TypeError, hash, b)
13b74c0b040c Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
776
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
777
47733
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
778 class SubclassableWeakrefTestCase(TestBase):
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
779
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
780 def test_subclass_refs(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
781 class MyRef(weakref.ref):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
782 def __init__(self, ob, callback=None, value=42):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
783 self.value = value
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
784 super(MyRef, self).__init__(ob, callback)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
785 def __call__(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
786 self.called = True
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
787 return super(MyRef, self).__call__()
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
788 o = Object("foo")
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
789 mr = MyRef(o, value=24)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
790 self.assertTrue(mr() is o)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
791 self.assertTrue(mr.called)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
792 self.assertEqual(mr.value, 24)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
793 del o
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
794 self.assertTrue(mr() is None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
795 self.assertTrue(mr.called)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
796
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
797 def test_subclass_refs_dont_replace_standard_refs(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
798 class MyRef(weakref.ref):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
799 pass
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
800 o = Object(42)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
801 r1 = MyRef(o)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
802 r2 = weakref.ref(o)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
803 self.assertTrue(r1 is not r2)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
804 self.assertEqual(weakref.getweakrefs(o), [r2, r1])
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
805 self.assertEqual(weakref.getweakrefcount(o), 2)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
806 r3 = MyRef(o)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
807 self.assertEqual(weakref.getweakrefcount(o), 3)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
808 refs = weakref.getweakrefs(o)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
809 self.assertEqual(len(refs), 3)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
810 self.assertTrue(r2 is refs[0])
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
811 self.assertIn(r1, refs[1:])
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
812 self.assertIn(r3, refs[1:])
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
813
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
814 def test_subclass_refs_dont_conflate_callbacks(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
815 class MyRef(weakref.ref):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
816 pass
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
817 o = Object(42)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
818 r1 = MyRef(o, id)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
819 r2 = MyRef(o, str)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
820 self.assertTrue(r1 is not r2)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
821 refs = weakref.getweakrefs(o)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
822 self.assertIn(r1, refs)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
823 self.assertIn(r2, refs)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
824
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
825 def test_subclass_refs_with_slots(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
826 class MyRef(weakref.ref):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
827 __slots__ = "slot1", "slot2"
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
828 def __new__(type, ob, callback, slot1, slot2):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
829 return weakref.ref.__new__(type, ob, callback)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
830 def __init__(self, ob, callback, slot1, slot2):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
831 self.slot1 = slot1
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
832 self.slot2 = slot2
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
833 def meth(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
834 return self.slot1 + self.slot2
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
835 o = Object(42)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
836 r = MyRef(o, None, "abc", "def")
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
837 self.assertEqual(r.slot1, "abc")
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
838 self.assertEqual(r.slot2, "def")
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
839 self.assertEqual(r.meth(), "abcdef")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
840 self.assertFalse(hasattr(r, "__dict__"))
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
841
47733
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
842 def test_subclass_refs_with_cycle(self):
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
843 # Bug #3110
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
844 # An instance of a weakref subclass can have attributes.
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
845 # If such a weakref holds the only strong reference to the object,
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
846 # deleting the weakref will delete the object. In this case,
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
847 # the callback must not be called, because the ref object is
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
848 # being deleted.
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
849 class MyRef(weakref.ref):
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
850 pass
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
851
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
852 # Use a local callback, for "regrtest -R::"
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
853 # to detect refcounting problems
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
854 def callback(w):
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
855 self.cbcalled += 1
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
856
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
857 o = C()
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
858 r1 = MyRef(o, callback)
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
859 r1.o = o
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
860 del o
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
861
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
862 del r1 # Used to crash here
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
863
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
864 self.assertEqual(self.cbcalled, 0)
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
865
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
866 # Same test, with two weakrefs to the same object
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
867 # (since code paths are different)
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
868 o = C()
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
869 r1 = MyRef(o, callback)
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
870 r2 = MyRef(o, callback)
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
871 r1.r = r2
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
872 r2.o = o
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
873 del o
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
874 del r2
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
875
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
876 del r1 # Used to crash here
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
877
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
878 self.assertEqual(self.cbcalled, 0)
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
879
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
880
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
881 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
882
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
883 COUNT = 10
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
884
75358
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
885 def check_len_cycles(self, dict_type, cons):
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
886 N = 20
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
887 items = [RefCycle() for i in range(N)]
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
888 dct = dict_type(cons(o) for o in items)
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
889 # Keep an iterator alive
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
890 it = dct.iteritems()
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
891 try:
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
892 next(it)
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
893 except StopIteration:
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
894 pass
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
895 del items
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
896 gc.collect()
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
897 n1 = len(dct)
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
898 del it
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
899 gc.collect()
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
900 n2 = len(dct)
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
901 # one item may be kept alive inside the iterator
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
902 self.assertIn(n1, (0, 1))
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
903 self.assertEqual(n2, 0)
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
904
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
905 def test_weak_keyed_len_cycles(self):
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
906 self.check_len_cycles(weakref.WeakKeyDictionary, lambda k: (k, 1))
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
907
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
908 def test_weak_valued_len_cycles(self):
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
909 self.check_len_cycles(weakref.WeakValueDictionary, lambda k: (1, k))
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
910
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
911 def check_len_race(self, dict_type, cons):
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
912 # Extended sanity checks for len() in the face of cyclic collection
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
913 self.addCleanup(gc.set_threshold, *gc.get_threshold())
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
914 for th in range(1, 100):
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
915 N = 20
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
916 gc.collect(0)
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
917 gc.set_threshold(th, th, th)
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
918 items = [RefCycle() for i in range(N)]
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
919 dct = dict_type(cons(o) for o in items)
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
920 del items
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
921 # All items will be collected at next garbage collection pass
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
922 it = dct.iteritems()
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
923 try:
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
924 next(it)
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
925 except StopIteration:
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
926 pass
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
927 n1 = len(dct)
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
928 del it
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
929 n2 = len(dct)
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
930 self.assertGreaterEqual(n1, 0)
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
931 self.assertLessEqual(n1, N)
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
932 self.assertGreaterEqual(n2, 0)
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
933 self.assertLessEqual(n2, n1)
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
934
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
935 def test_weak_keyed_len_race(self):
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
936 self.check_len_race(weakref.WeakKeyDictionary, lambda k: (k, 1))
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
937
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
938 def test_weak_valued_len_race(self):
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
939 self.check_len_race(weakref.WeakValueDictionary, lambda k: (1, k))
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
940
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
941 def test_weak_values(self):
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
942 #
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
943 # 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
944 #
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
945 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
946 for o in objects:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
947 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
948 "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
949 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
950 "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
951 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
952 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
953 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
954 items2.sort()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
955 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
956 "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
957 del items1, items2
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
958 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
959 del objects[0]
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
960 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
961 "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
962 del objects, o
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
963 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
964 "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
965 # regression on SF bug #447152:
e90a658a58d4 Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
Fred Drake <fdrake@acm.org>
parents: 17767
diff changeset
966 dict = weakref.WeakValueDictionary()
e90a658a58d4 Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
Fred Drake <fdrake@acm.org>
parents: 17767
diff changeset
967 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
968 dict[2] = C()
e90a658a58d4 Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
Fred Drake <fdrake@acm.org>
parents: 17767
diff changeset
969 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
970
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
971 def test_weak_keys(self):
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
972 #
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
973 # This exercises d.copy(), d.items(), d[] = v, d[], del d[],
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57349
diff changeset
974 # len(d), in d.
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
975 #
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
976 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
977 for o in objects:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
978 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
979 "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
980 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
981 "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
982 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
983 items2 = dict.copy().items()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
984 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
985 "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
986 del items1, items2
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
987 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
988 del objects[0]
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
989 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
990 "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
991 del objects, o
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
992 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
993 "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
994 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
995 dict[o] = "What is the meaning of the universe?"
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
996 self.assertIn(o, dict)
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
997 self.assertNotIn(34, dict)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
998
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
999 def test_weak_keyed_iters(self):
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1000 dict, objects = self.make_weak_keyed_dict()
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1001 self.check_iters(dict)
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1002
38079
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1003 # Test keyrefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1004 refs = dict.keyrefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1005 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
1006 objects2 = list(objects)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1007 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
1008 ob = wr()
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
1009 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
1010 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
1011 objects2.remove(ob)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1012 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
1013
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1014 # Test iterkeyrefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1015 objects2 = list(objects)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1016 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
1017 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
1018 ob = wr()
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
1019 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
1020 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
1021 objects2.remove(ob)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1022 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
1023
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1024 def test_weak_valued_iters(self):
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1025 dict, objects = self.make_weak_valued_dict()
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1026 self.check_iters(dict)
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1027
38079
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1028 # Test valuerefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1029 refs = dict.valuerefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1030 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
1031 objects2 = list(objects)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1032 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
1033 ob = wr()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1034 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
1035 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
1036 objects2.remove(ob)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1037 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
1038
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1039 # Test itervaluerefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1040 objects2 = list(objects)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1041 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
1042 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
1043 ob = wr()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1044 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
1045 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
1046 objects2.remove(ob)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1047 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
1048
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1049 def check_iters(self, dict):
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1050 # item iterator:
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1051 items = dict.items()
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1052 for item in dict.iteritems():
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1053 items.remove(item)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1054 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
1055
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1056 # key iterator, via __iter__():
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1057 keys = dict.keys()
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1058 for k in dict:
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1059 keys.remove(k)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1060 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
1061
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1062 # 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
1063 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
1064 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
1065 keys.remove(k)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1066 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
1067
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1068 # value iterator:
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1069 values = dict.values()
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1070 for v in dict.itervalues():
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1071 values.remove(v)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1072 self.assertTrue(len(values) == 0,
29862
f339f29b06cb stylistic nits:
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
1073 "itervalues() did not touch all values")
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1074
23764
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
1075 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
1076 o = Object(3)
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
1077 dict = weakref.WeakKeyDictionary({o:364})
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1078 self.assertTrue(dict[o] == 364)
23764
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
1079
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
1080 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
1081 o = Object(3)
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
1082 dict = weakref.WeakKeyDictionary({o:364})
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
1083 dict2 = weakref.WeakKeyDictionary(dict)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1084 self.assertTrue(dict[o] == 364)
23764
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
1085
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1086 def make_weak_keyed_dict(self):
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1087 dict = weakref.WeakKeyDictionary()
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1088 objects = map(Object, range(self.COUNT))
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1089 for o in objects:
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1090 dict[o] = o.arg
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1091 return dict, objects
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1092
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1093 def make_weak_valued_dict(self):
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1094 dict = weakref.WeakValueDictionary()
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1095 objects = map(Object, range(self.COUNT))
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1096 for o in objects:
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1097 dict[o.arg] = o
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1098 return dict, objects
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1099
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1100 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
1101 weakdict = klass()
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1102 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
1103 weakdict[key2] = value2
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1104 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
1105 k, v = weakdict.popitem()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1106 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
1107 if k is key1:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1108 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
1109 else:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1110 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
1111 k, v = weakdict.popitem()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1112 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
1113 if k is key1:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1114 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
1115 else:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1116 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
1117
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1118 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
1119 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
1120 "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
1121
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1122 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
1123 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
1124 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
1125
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1126 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
1127 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
1128 "invalid test"
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1129 " -- 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
1130 weakdict = klass()
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1131 o = weakdict.setdefault(key, value1)
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
1132 self.assertIs(o, value1)
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
1133 self.assertIn(key, weakdict)
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
1134 self.assertIs(weakdict.get(key), value1)
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
1135 self.assertIs(weakdict[key], value1)
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1136
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1137 o = weakdict.setdefault(key, value2)
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
1138 self.assertIs(o, value1)
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
1139 self.assertIn(key, weakdict)
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
1140 self.assertIs(weakdict.get(key), value1)
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
1141 self.assertIs(weakdict[key], value1)
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1142
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1143 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
1144 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
1145 "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
1146
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1147 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
1148 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
1149 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
1150
17547
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1151 def check_update(self, klass, dict):
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1152 #
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57349
diff changeset
1153 # This exercises d.update(), len(d), d.keys(), in d,
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1154 # d.get(), d[].
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1155 #
17547
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1156 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
1157 weakdict.update(dict)
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57349
diff changeset
1158 self.assertEqual(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
1159 for k in weakdict.keys():
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
1160 self.assertIn(k, 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
1161 "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
1162 v = dict.get(k)
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
1163 self.assertIs(v, weakdict[k])
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
1164 self.assertIs(v, 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
1165 for k in dict.keys():
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
1166 self.assertIn(k, weakdict,
17547
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1167 "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
1168 v = dict[k]
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
1169 self.assertIs(v, weakdict[k])
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
1170 self.assertIs(v, 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
1171
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1172 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
1173 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
1174 {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
1175
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1176 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
1177 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
1178 {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
1179
19764
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1180 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
1181 d = weakref.WeakKeyDictionary()
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1182 o1 = Object('1')
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1183 o2 = Object('2')
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1184 d[o1] = 'something'
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1185 d[o2] = 'something'
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1186 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
1187 del d[o1]
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1188 self.assertTrue(len(d) == 1)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1189 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
1190
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1191 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
1192 d = weakref.WeakValueDictionary()
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1193 o1 = Object('1')
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1194 o2 = Object('2')
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1195 d['something'] = o1
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1196 d['something else'] = o2
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1197 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
1198 del d['something']
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1199 self.assertTrue(len(d) == 1)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49120
diff changeset
1200 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
1201
29257
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1202 def test_weak_keyed_bad_delitem(self):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1203 d = weakref.WeakKeyDictionary()
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1204 o = Object('1')
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1205 # 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
1206 # 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
1207 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
1208 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
1209
18c020463965 Fleshed out WeakKeyDictionary.__delitem__ NEWS to cover issues raised on
Tim Peters <tim.peters@gmail.com>
parents: 29257
diff changeset
1210 # 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
1211 # __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
1212 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
1213 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
1214 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
1215
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1216 def test_weak_keyed_cascading_deletes(self):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1217 # 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
1218 # 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
1219 # 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
1220
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1221 d = weakref.WeakKeyDictionary()
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1222 mutate = False
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1223
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1224 class C(object):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1225 def __init__(self, i):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1226 self.value = i
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1227 def __hash__(self):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1228 return hash(self.value)
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1229 def __eq__(self, other):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1230 if mutate:
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1231 # 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
1232 # last strong reference to a key.
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1233 del objs[-1]
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1234 return self.value == other.value
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1235
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1236 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
1237 for o in objs:
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1238 d[o] = o.value
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1239 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
1240 # 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
1241 objs = d.keys()
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1242 # 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
1243 # 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
1244 objs.reverse()
29258
18c020463965 Fleshed out WeakKeyDictionary.__delitem__ NEWS to cover issues raised on
Tim Peters <tim.peters@gmail.com>
parents: 29257
diff changeset
1245
29257
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1246 # 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
1247 # 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
1248 # 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
1249 # RuntimeError: dictionary changed size during iteration
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1250 # 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
1251 # 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
1252 # "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
1253 mutate = True
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1254 count = 0
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1255 for o in objs:
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1256 count += 1
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1257 del d[o]
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1258 self.assertEqual(len(d), 0)
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1259 self.assertEqual(count, 2)
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1260
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
1261 from test import mapping_tests
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1262
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
1263 class WeakValueDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
29862
f339f29b06cb stylistic nits:
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
1264 """Check that WeakValueDictionary conforms to the mapping protocol"""
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1265 __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
1266 type2test = weakref.WeakValueDictionary
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1267 def _reference(self):
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1268 return self.__ref.copy()
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1269
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
1270 class WeakKeyDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
29862
f339f29b06cb stylistic nits:
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
1271 """Check that WeakKeyDictionary conforms to the mapping protocol"""
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1272 __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
1273 type2test = weakref.WeakKeyDictionary
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1274 def _reference(self):
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1275 return self.__ref.copy()
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1276
47196
12c56d2eb4d9 #2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents: 40707
diff changeset
1277 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
1278
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1279 >>> import weakref
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1280 >>> class Dict(dict):
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1281 ... pass
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1282 ...
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1283 >>> 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
1284 >>> r = weakref.ref(obj)
38464
e989ee5d404a ("Forward-port" of r46506)
Armin Rigo <arigo@tunes.org>
parents: 38079
diff changeset
1285 >>> print r() is obj
e989ee5d404a ("Forward-port" of r46506)
Armin Rigo <arigo@tunes.org>
parents: 38079
diff changeset
1286 True
35195
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1287
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1288 >>> import weakref
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1289 >>> class Object:
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1290 ... pass
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1291 ...
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1292 >>> o = Object()
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1293 >>> r = weakref.ref(o)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1294 >>> o2 = r()
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1295 >>> o is o2
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1296 True
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1297 >>> del o, o2
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1298 >>> print r()
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1299 None
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1300
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1301 >>> import weakref
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1302 >>> class ExtendedRef(weakref.ref):
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1303 ... def __init__(self, ob, callback=None, **annotations):
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1304 ... super(ExtendedRef, self).__init__(ob, callback)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1305 ... self.__counter = 0
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1306 ... for k, v in annotations.iteritems():
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1307 ... setattr(self, k, v)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1308 ... def __call__(self):
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1309 ... '''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
1310 ... times the reference has been called.
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1311 ... '''
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1312 ... ob = super(ExtendedRef, self).__call__()
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1313 ... if ob is not None:
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1314 ... self.__counter += 1
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1315 ... ob = (ob, self.__counter)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1316 ... return ob
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1317 ...
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1318 >>> 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
1319 ... pass
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1320 ...
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1321 >>> a = A()
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1322 >>> r = ExtendedRef(a, foo=1, bar="baz")
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1323 >>> r.foo
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1324 1
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1325 >>> r.bar
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1326 'baz'
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1327 >>> r()[1]
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1328 1
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1329 >>> r()[1]
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1330 2
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1331 >>> r()[0] is a
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1332 True
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1333
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1334
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1335 >>> import weakref
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1336 >>> _id2obj_dict = weakref.WeakValueDictionary()
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1337 >>> def remember(obj):
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1338 ... oid = id(obj)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1339 ... _id2obj_dict[oid] = obj
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1340 ... return oid
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1341 ...
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1342 >>> def id2obj(oid):
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1343 ... return _id2obj_dict[oid]
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1344 ...
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1345 >>> a = A() # from here, just testing
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1346 >>> a_id = remember(a)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1347 >>> id2obj(a_id) is a
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1348 True
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1349 >>> del a
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1350 >>> try:
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1351 ... id2obj(a_id)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1352 ... except KeyError:
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1353 ... print 'OK'
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1354 ... else:
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1355 ... print 'WeakValueDictionary error'
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1356 OK
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1357
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1358 """
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1359
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1360 __test__ = {'libreftest' : libreftest}
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1361
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19764
diff changeset
1362 def test_main():
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28240
diff changeset
1363 test_support.run_unittest(
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28240
diff changeset
1364 ReferencesTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28240
diff changeset
1365 MappingTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28240
diff changeset
1366 WeakValueDictionaryTestCase,
29862
f339f29b06cb stylistic nits:
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
1367 WeakKeyDictionaryTestCase,
47733
4ed165940bdd Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40705
diff changeset
1368 SubclassableWeakrefTestCase,
29862
f339f29b06cb stylistic nits:
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
1369 )
35195
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1370 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
1371
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19764
diff changeset
1372
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19764
diff changeset
1373 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
1374 test_main()