annotate Lib/test/test_weakref.py @ 78069:a8aa77458e12 3.2

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