annotate Lib/test/test_weakref.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 8deab371850b
children
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
83631
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
10 from test import support, script_helper
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
83631
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
15 # Used by FinalizeTestCase as a global that may be replaced by None
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
16 # when the interpreter shuts down.
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
17 _global_var = 'foobar'
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
18
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19 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
20 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
21 pass
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
22
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
23
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
24 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
25 bar = None
16385
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 __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
28 self.bar = x
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
29
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
30
17172
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_function():
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
32 def 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
33 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
34
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
35 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
36 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
37
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
38
80394
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
39 class Object:
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
40 def __init__(self, arg):
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
41 self.arg = arg
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
42 def __repr__(self):
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
43 return "<Object %r>" % self.arg
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
44 def __eq__(self, other):
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
45 if isinstance(other, Object):
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
46 return self.arg == other.arg
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
47 return NotImplemented
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
48 def __lt__(self, other):
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
49 if isinstance(other, Object):
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
50 return self.arg < other.arg
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
51 return NotImplemented
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
52 def __hash__(self):
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
53 return hash(self.arg)
80476
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
54 def some_method(self):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
55 return 4
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
56 def other_method(self):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
57 return 5
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
58
80394
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
59
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
60 class RefCycle:
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
61 def __init__(self):
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
62 self.cycle = self
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
63
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
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 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
66
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
67 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
68 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
69
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
70 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
71 self.cbcalled += 1
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
72
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
73
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
74 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
75
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
76 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
77 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
78 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
79 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
80
22946
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
81 # 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
82 # Live reference:
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
83 o = C()
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
84 wr = weakref.ref(o)
39669
e076cca4d9d7 Remove usage of backticks.
Brett Cannon <bcannon@gmail.com>
parents: 39644
diff changeset
85 repr(wr)
22946
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
86 # Dead reference:
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
87 del o
39669
e076cca4d9d7 Remove usage of backticks.
Brett Cannon <bcannon@gmail.com>
parents: 39644
diff changeset
88 repr(wr)
22946
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
89
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
90 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
91 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
92 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
93 self.check_basic_callback(create_bound_method)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
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_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
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 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
98 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
99 del o
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
100 self.assertIsNone(ref1(), "expected reference to be invalidated")
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
101 self.assertIsNone(ref2(), "expected reference to be invalidated")
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
102 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
103 "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
104
17498
f72dcd67cccb Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents: 17385
diff changeset
105 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
106 # 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
107 #
f72dcd67cccb Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents: 17385
diff changeset
108 # 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
109 # 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
110 # (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
111 # 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
112 # 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
113 # 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
114 # 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
115 #
f72dcd67cccb Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents: 17385
diff changeset
116 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
117 self.ref()
f72dcd67cccb Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents: 17385
diff changeset
118 c = C()
f72dcd67cccb Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents: 17385
diff changeset
119 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
120 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
121 del c
f72dcd67cccb Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents: 17385
diff changeset
122
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
123 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
124 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
125 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
126 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
127 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
128 del o
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
129
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
130 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
131 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
132
40949
e759fd8d95ae Remove the exceptions builtin module, all the exceptions are already builtin.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40818
diff changeset
133 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
134 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
135 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
136 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
137
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
138 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
139 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
140 ref = weakref.ref(o)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
141 self.assertIsNotNone(ref(),
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
142 "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
143 o2 = ref()
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
144 self.assertIs(o, 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
145 "<ref>() should return original object if live")
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
146
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
147 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
148 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
149 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
150 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
151 del o
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
152 self.assertEqual(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
153 "callback did not properly set 'cbcalled'")
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
154 self.assertIsNone(ref(),
17498
f72dcd67cccb Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents: 17385
diff changeset
155 "ref2 should be dead after deleting object reference")
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
156
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 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
158 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
159 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
160 # 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
161 # 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
162 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
163 ref2 = weakref.ref(o)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
164 self.assertIs(ref1, 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
165 "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
166
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
167 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
168 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
169 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
170 ref2 = weakref.ref(o)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
171 self.assertIs(ref1, 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
172 "reference object w/out callback should be re-used")
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
173 self.assertEqual(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
174 "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
175 del proxy
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
176 self.assertEqual(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
177 "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
178
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
179 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
180 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
181 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
182 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
183 proxy2 = weakref.proxy(o)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
184 self.assertIs(proxy1, 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
185 "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
186
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
187 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
188 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
189 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
190
45302
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 44021
diff changeset
191 L = collections.UserList()
21484
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
192 p = weakref.proxy(L)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54456
diff changeset
193 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
194 p.append(12)
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
195 self.assertEqual(len(L), 1)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54456
diff changeset
196 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
197 p[:] = [2, 3]
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
198 self.assertEqual(len(L), 2)
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
199 self.assertEqual(len(p), 2)
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
200 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
201 p[1] = 5
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
202 self.assertEqual(L[1], 5)
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
203 self.assertEqual(p[1], 5)
45302
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 44021
diff changeset
204 L2 = collections.UserList(L)
21484
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
205 p2 = weakref.proxy(L2)
2d376d926da8 Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents: 21340
diff changeset
206 self.assertEqual(p, p2)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31454
diff changeset
207 ## self.assertEqual(repr(L2), repr(p2))
45302
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 44021
diff changeset
208 L3 = collections.UserList(range(10))
22946
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
209 p3 = weakref.proxy(L3)
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
210 self.assertEqual(L3[:], p3[:])
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
211 self.assertEqual(L3[5:], p3[5:])
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
212 self.assertEqual(L3[:5], p3[:5])
cc50a091f573 Improve coverage of Objects/weakrefobject.c.
Fred Drake <fdrake@acm.org>
parents: 21484
diff changeset
213 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
214
57348
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
215 def test_proxy_unicode(self):
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
216 # See bug 5037
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
217 class C(object):
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
218 def __str__(self):
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
219 return "string"
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
220 def __bytes__(self):
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
221 return b"bytes"
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
222 instance = C()
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58194
diff changeset
223 self.assertIn("__bytes__", dir(weakref.proxy(instance)))
57348
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
224 self.assertEqual(bytes(weakref.proxy(instance)), b"bytes")
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
225
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
226 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
227 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
228 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
229 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
230 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
231 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
232 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
233
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
234 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
235 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
236 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
237 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
238 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
239 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
240 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
241 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
242 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
243 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
244 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
245
31448
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
246 # 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
247 # 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
248 # 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
249 # 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
250 # 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
251 # 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
252 # 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
253
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
254 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
255 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
256
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
257 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
258 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
259
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
260 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
261 o = Object(1)
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
262 p1 = makeref(o, None)
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
263 p2 = makeref(o, None)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
264 self.assertIs(p1, 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
265 del p1, p2
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
266 p1 = makeref(o)
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
267 p2 = makeref(o, None)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
268 self.assertIs(p1, 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
269 del p1, p2
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
270 p1 = makeref(o)
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
271 p2 = makeref(o)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
272 self.assertIs(p1, 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
273 del p1, p2
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
274 p1 = makeref(o, None)
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
275 p2 = makeref(o)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
276 self.assertIs(p1, 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
277
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
278 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
279 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
280 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
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 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
283
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
284 self.assertIs(type(ref1), 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
285 "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
286 ref1('twinkies!')
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
287 self.assertEqual(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
288 "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
289 ref1(x='Splat.')
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
290 self.assertEqual(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
291 "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
292
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
293 # 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
294 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
295
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
296 # 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
297 self.assertRaises(TypeError, ref1, 1, 2, 3)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
298
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
299 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
300 o.foo = 1
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
301 self.assertEqual(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
302 "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
303 o.foo = 2
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
304 self.assertEqual(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
305 "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
306 del o.foo
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
307 self.assertFalse(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
308 "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
309
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
310 proxy.foo = 1
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
311 self.assertEqual(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
312 "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
313 proxy.foo = 2
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
314 self.assertEqual(o.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
315 "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
316 del proxy.foo
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
317 self.assertFalse(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
318 "object does not reflect attribute removal via proxy")
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
319
29697
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
320 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
321 # 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
322 class Foo:
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
323 result = None
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
324 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
325 self.result = accessor
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
326 g = Foo()
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
327 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
328 del f[0]
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 29258
diff changeset
329 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
330
34847
51ba8ce8d92a SF bug #1770766: weakref proxy has incorrect __nonzero__ behavior.
Raymond Hettinger <python@rcn.com>
parents: 32530
diff changeset
331 def test_proxy_bool(self):
51ba8ce8d92a SF bug #1770766: weakref proxy has incorrect __nonzero__ behavior.
Raymond Hettinger <python@rcn.com>
parents: 32530
diff changeset
332 # 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
333 class List(list): pass
51ba8ce8d92a SF bug #1770766: weakref proxy has incorrect __nonzero__ behavior.
Raymond Hettinger <python@rcn.com>
parents: 32530
diff changeset
334 lyst = List()
51ba8ce8d92a SF bug #1770766: weakref proxy has incorrect __nonzero__ behavior.
Raymond Hettinger <python@rcn.com>
parents: 32530
diff changeset
335 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
336
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 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
338 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
339 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
340 ref2 = weakref.ref(o, self.callback)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
341 self.assertEqual(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
342 "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
343
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
344 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
345 proxy2 = weakref.proxy(o, self.callback)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
346 self.assertEqual(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
347 "got wrong number of weak reference objects")
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
348
31448
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
349 del ref1, ref2, proxy1, proxy2
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
350 self.assertEqual(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
351 "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
352 " referent when discarded.")
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
353
31168
48702f19ccfb Add tests to test_weakref.py to bring code coverage in _weakref.c up to 100%.
Walter Dörwald <walter@livinglogic.de>
parents: 30988
diff changeset
354 # assumes ints do not support weakrefs
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
355 self.assertEqual(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
356 "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
357
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
358 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
359 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
360 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
361 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
362 del ref1
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
363 self.assertEqual(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
364 "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
365
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
366 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
367 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
368 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
369 del ref2
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
370 self.assertEqual(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
371 "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
372
31448
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
373 del ref1
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
374 self.assertEqual(weakref.getweakrefs(o), [],
31448
dfc1819b4eb4 - add tests that exercise fixes for the PyWeakref_NewRef() and
Fred Drake <fdrake@acm.org>
parents: 31168
diff changeset
375 "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
376
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
377 # assumes ints do not support weakrefs
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
378 self.assertEqual(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
379 "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
380
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
381 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
382 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
383 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
384 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
385 p = weakref.proxy(f)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
386 self.assertEqual(p + 1.0, 3.0)
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
387 self.assertEqual(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
388
21340
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
389 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
390 # 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
391 # 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
392 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
393 pass
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
394 data = {}
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
395 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
396 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
397 def encapsulate():
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
398 f = lambda : ()
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
399 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
400 raise BogusError
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
401 try:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
402 encapsulate()
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
403 except BogusError:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
404 pass
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
405 else:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
406 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
407 try:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
408 encapsulate()
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
409 except BogusError:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
410 pass
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
411 else:
3d68e3110e1f Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents: 20903
diff changeset
412 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
413
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
414 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
415 # "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
416 # 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
417 # 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
418 # 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
419 # 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
420 # 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
421 # 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
422 # 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
423 # 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
424
cd7624b025a0 Backport of fix for SF bug 840829, memory corruption in some cases of
Tim Peters <tim.peters@gmail.com>
parents: 29862
diff changeset
425 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
426
cd7624b025a0 Backport of fix for SF bug 840829, memory corruption in some cases of
Tim Peters <tim.peters@gmail.com>
parents: 29862
diff changeset
427 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
428 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
429
cd7624b025a0 Backport of fix for SF bug 840829, memory corruption in some cases of
Tim Peters <tim.peters@gmail.com>
parents: 29862
diff changeset
430 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
431 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
432 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
433
30916
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
434 # 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
435 del wr
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
436
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
437 c1 = C()
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
438 c1.i = C()
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
439 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
440
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
441 c2 = C()
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
442 c2.c1 = c1
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
443 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
444
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
445 # 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
446 # 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
447 # 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
448 # 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
449 # 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
450 # 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
451 # deallocation of c2.
81733c7a95c8 subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30908
diff changeset
452 del c2
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
453
30988
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
454 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
455 import gc
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
456
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
457 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
458 pass
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
459
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
460 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
461 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
462 self.J
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
463
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
464 I = II()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
465 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
466 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
467
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
468 # 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
469 # 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
470 # 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
471 # 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
472 # 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
473 # 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
474 # 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
475 # trash.
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
476 #
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
477 # 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
478 # 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
479 # 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
480 # 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
481 # 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
482 # 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
483 # 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
484 # 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
485 # 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
486 # 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
487 # 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
488 # 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
489 # 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
490 # 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
491 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
492 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
493
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
494 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
495 import gc
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
496
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
497 # 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
498 # 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
499 # 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
500 # 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
501 # __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
502 # 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
503 # __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
504 # 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
505 # 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
506 # "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
507 # 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
508
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
509 class J(object):
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
510 pass
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
511
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
512 class II:
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
513 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
514 self.J
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
515
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
516 I = II()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
517 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
518 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
519
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
520 del 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
521 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
522
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
523 def test_callback_in_cycle_3(self):
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
524 import gc
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
525
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
526 # 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
527 # 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
528 # 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
529 # 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
530 # 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
531
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
532 class C:
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
533 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
534 self.me
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
535 self.c1
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
536 self.wr
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
537
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
538 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
539
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
540 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
541 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
542 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
543
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
544 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
545 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
546
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
547 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
548 import gc
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
549
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
550 # 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
551 # 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
552 # 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
553 # 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
554 # 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
555 # 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
556
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
557 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
558 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
559 self.me
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
560 self.c1
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
561 self.wr
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
562
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
563 class D:
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
564 pass
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
565
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
566 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
567
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
568 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
569 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
570 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
571
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
572 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
573 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
574
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
575 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
576 import gc
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 # 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
579 # 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
580 # 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
581 # 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
582 # 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
583 # 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
584 # 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
585 # 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
586
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
587 alist = []
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
588 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
589 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
590 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
591
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
592 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
593 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
594
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
595 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
596 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
597 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
598 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
599 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
600
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
601 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
602 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
603 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
604
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
605 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
606 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
607
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
608 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
609 # 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
610 # 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
611 # 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
612 # 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
613 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
614 # 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
615 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
616
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
617 del alist[:]
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
618 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
619 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
620
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
621 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
622 import gc
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
623
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
624 # 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
625 alist = []
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
626 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
627 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
628
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
629 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
630 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
631 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
632
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
633 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
634 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
635 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
636 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
637 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
638 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
639 external_wr = weakref.ref(callback, safe_callback) # but this will
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
640 self.assertIs(external_wr(), callback)
30988
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
641
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
642 # 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
643 # 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
644 # 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
645 # 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
646 # -- 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
647 # 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
648
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
649 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
650 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
651 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
652 self.assertEqual(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
653 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
654
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
655 del alist[:]
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
656 gc.collect()
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
657 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
658
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
659 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
660 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
661
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
662 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
663 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
664
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
665 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
666 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
667 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
668 gc.collect()
31522
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
669 class A:
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
670 pass
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
671
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
672 def callback(*args):
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
673 pass
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
674
31522
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
675 referenced = A()
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
676
31522
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
677 a = A()
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
678 a.a = a
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
679 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
680
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
681 try:
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
682 # 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
683 # cyclic trash:
31522
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
684 a = A()
239443933324 further testing indicates that the simplified version of the test
Fred Drake <fdrake@acm.org>
parents: 31517
diff changeset
685 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
686
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
687 finally:
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31448
diff changeset
688 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
689
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40582
diff changeset
690 def test_ref_created_during_del(self):
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40582
diff changeset
691 # Bug #1377858
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40582
diff changeset
692 # 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
693 # 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
694 # 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
695 class Target(object):
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40582
diff changeset
696 def __del__(self):
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40582
diff changeset
697 global ref_from_del
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40582
diff changeset
698 ref_from_del = weakref.ref(self)
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40582
diff changeset
699
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40582
diff changeset
700 w = Target()
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40582
diff changeset
701
49137
17475eb55067 Merged revisions 66337,66347,66350,66352,66358 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49001
diff changeset
702 def test_init(self):
17475eb55067 Merged revisions 66337,66347,66350,66352,66358 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49001
diff changeset
703 # Issue 3634
17475eb55067 Merged revisions 66337,66347,66350,66352,66358 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49001
diff changeset
704 # <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
705 r = weakref.ref(Exception)
17475eb55067 Merged revisions 66337,66347,66350,66352,66358 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49001
diff changeset
706 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
707 # 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
708 gc.collect()
17475eb55067 Merged revisions 66337,66347,66350,66352,66358 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49001
diff changeset
709
60115
c36f23da88c1 NOTE: only ported the test for new-style classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58456
diff changeset
710 def test_classes(self):
c36f23da88c1 NOTE: only ported the test for new-style classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58456
diff changeset
711 # Check that classes are weakrefable.
c36f23da88c1 NOTE: only ported the test for new-style classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58456
diff changeset
712 class A(object):
c36f23da88c1 NOTE: only ported the test for new-style classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58456
diff changeset
713 pass
c36f23da88c1 NOTE: only ported the test for new-style classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58456
diff changeset
714 l = []
c36f23da88c1 NOTE: only ported the test for new-style classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58456
diff changeset
715 weakref.ref(int)
c36f23da88c1 NOTE: only ported the test for new-style classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58456
diff changeset
716 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
717 A = None
c36f23da88c1 NOTE: only ported the test for new-style classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58456
diff changeset
718 gc.collect()
c36f23da88c1 NOTE: only ported the test for new-style classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58456
diff changeset
719 self.assertEqual(a(), None)
c36f23da88c1 NOTE: only ported the test for new-style classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58456
diff changeset
720 self.assertEqual(l, [a])
c36f23da88c1 NOTE: only ported the test for new-style classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58456
diff changeset
721
80394
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
722 def test_equality(self):
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
723 # Alive weakrefs defer equality testing to their underlying object.
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
724 x = Object(1)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
725 y = Object(1)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
726 z = Object(2)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
727 a = weakref.ref(x)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
728 b = weakref.ref(y)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
729 c = weakref.ref(z)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
730 d = weakref.ref(x)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
731 # Note how we directly test the operators here, to stress both
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
732 # __eq__ and __ne__.
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
733 self.assertTrue(a == b)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
734 self.assertFalse(a != b)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
735 self.assertFalse(a == c)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
736 self.assertTrue(a != c)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
737 self.assertTrue(a == d)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
738 self.assertFalse(a != d)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
739 del x, y, z
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
740 gc.collect()
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
741 for r in a, b, c:
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
742 # Sanity check
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
743 self.assertIs(r(), None)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
744 # Dead weakrefs compare by identity: whether `a` and `d` are the
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
745 # same weakref object is an implementation detail, since they pointed
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
746 # to the same original object and didn't have a callback.
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
747 # (see issue #16453).
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
748 self.assertFalse(a == b)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
749 self.assertTrue(a != b)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
750 self.assertFalse(a == c)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
751 self.assertTrue(a != c)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
752 self.assertEqual(a == d, a is d)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
753 self.assertEqual(a != d, a is not d)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
754
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
755 def test_ordering(self):
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
756 # weakrefs cannot be ordered, even if the underlying objects can.
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
757 ops = [operator.lt, operator.gt, operator.le, operator.ge]
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
758 x = Object(1)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
759 y = Object(1)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
760 a = weakref.ref(x)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
761 b = weakref.ref(y)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
762 for op in ops:
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
763 self.assertRaises(TypeError, op, a, b)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
764 # Same when dead.
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
765 del x, y
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
766 gc.collect()
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
767 for op in ops:
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
768 self.assertRaises(TypeError, op, a, b)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
769
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
770 def test_hashing(self):
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
771 # Alive weakrefs hash the same as the underlying object
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
772 x = Object(42)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
773 y = Object(42)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
774 a = weakref.ref(x)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
775 b = weakref.ref(y)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
776 self.assertEqual(hash(a), hash(42))
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
777 del x, y
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
778 gc.collect()
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
779 # Dead weakrefs:
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
780 # - retain their hash is they were hashed when alive;
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
781 # - otherwise, cannot be hashed.
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
782 self.assertEqual(hash(a), hash(42))
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
783 self.assertRaises(TypeError, hash, b)
590f1b55abea Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75356
diff changeset
784
80759
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
785 def test_trashcan_16602(self):
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
786 # Issue #16602: when a weakref's target was part of a long
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
787 # deallocation chain, the trashcan mechanism could delay clearing
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
788 # of the weakref and make the target object visible from outside
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
789 # code even though its refcount had dropped to 0. A crash ensued.
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
790 class C:
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
791 def __init__(self, parent):
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
792 if not parent:
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
793 return
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
794 wself = weakref.ref(self)
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
795 def cb(wparent):
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
796 o = wself()
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
797 self.wparent = weakref.ref(parent, cb)
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
798
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
799 d = weakref.WeakKeyDictionary()
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
800 root = c = C(None)
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
801 for n in range(100):
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
802 d[c] = c = C(c)
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
803 del root
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
804 gc.collect()
0748c22b37e5 Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
805
83286
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
806 def test_callback_attribute(self):
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
807 x = Object(1)
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
808 callback = lambda ref: None
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
809 ref1 = weakref.ref(x, callback)
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
810 self.assertIs(ref1.__callback__, callback)
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
811
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
812 ref2 = weakref.ref(x)
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
813 self.assertIsNone(ref2.__callback__)
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
814
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
815 def test_callback_attribute_after_deletion(self):
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
816 x = Object(1)
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
817 ref = weakref.ref(x, self.callback)
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
818 self.assertIsNotNone(ref.__callback__)
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
819 del x
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
820 support.gc_collect()
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
821 self.assertIsNone(ref.__callback__)
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
822
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
823 def test_set_callback_attribute(self):
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
824 x = Object(1)
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
825 callback = lambda ref: None
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
826 ref1 = weakref.ref(x, callback)
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
827 with self.assertRaises(AttributeError):
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
828 ref1.__callback__ = lambda ref: None
9e7d31b04d78 Issue #17643: Add __callback__ attribute to weakref.ref.
Mark Dickinson <dickinsm@gmail.com>
parents: 80761
diff changeset
829
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
830
47734
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
831 class SubclassableWeakrefTestCase(TestBase):
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
832
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
833 def test_subclass_refs(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
834 class MyRef(weakref.ref):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
835 def __init__(self, ob, callback=None, value=42):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
836 self.value = value
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
837 super().__init__(ob, callback)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
838 def __call__(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
839 self.called = True
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
840 return super().__call__()
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
841 o = Object("foo")
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
842 mr = MyRef(o, value=24)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
843 self.assertIs(mr(), o)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54456
diff changeset
844 self.assertTrue(mr.called)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
845 self.assertEqual(mr.value, 24)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
846 del o
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
847 self.assertIsNone(mr())
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54456
diff changeset
848 self.assertTrue(mr.called)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
849
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
850 def test_subclass_refs_dont_replace_standard_refs(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
851 class MyRef(weakref.ref):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
852 pass
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
853 o = Object(42)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
854 r1 = MyRef(o)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
855 r2 = weakref.ref(o)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
856 self.assertIsNot(r1, r2)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
857 self.assertEqual(weakref.getweakrefs(o), [r2, r1])
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
858 self.assertEqual(weakref.getweakrefcount(o), 2)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
859 r3 = MyRef(o)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
860 self.assertEqual(weakref.getweakrefcount(o), 3)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
861 refs = weakref.getweakrefs(o)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
862 self.assertEqual(len(refs), 3)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
863 self.assertIs(r2, refs[0])
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58194
diff changeset
864 self.assertIn(r1, refs[1:])
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58194
diff changeset
865 self.assertIn(r3, refs[1:])
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
866
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
867 def test_subclass_refs_dont_conflate_callbacks(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
868 class MyRef(weakref.ref):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
869 pass
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
870 o = Object(42)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
871 r1 = MyRef(o, id)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
872 r2 = MyRef(o, str)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
873 self.assertIsNot(r1, r2)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
874 refs = weakref.getweakrefs(o)
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58194
diff changeset
875 self.assertIn(r1, refs)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58194
diff changeset
876 self.assertIn(r2, refs)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
877
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
878 def test_subclass_refs_with_slots(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
879 class MyRef(weakref.ref):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
880 __slots__ = "slot1", "slot2"
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
881 def __new__(type, ob, callback, slot1, slot2):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
882 return weakref.ref.__new__(type, ob, callback)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
883 def __init__(self, ob, callback, slot1, slot2):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
884 self.slot1 = slot1
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
885 self.slot2 = slot2
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
886 def meth(self):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
887 return self.slot1 + self.slot2
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
888 o = Object(42)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
889 r = MyRef(o, None, "abc", "def")
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
890 self.assertEqual(r.slot1, "abc")
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
891 self.assertEqual(r.slot2, "def")
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
892 self.assertEqual(r.meth(), "abcdef")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54456
diff changeset
893 self.assertFalse(hasattr(r, "__dict__"))
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
894
47734
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
895 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
896 # Bug #3110
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
897 # 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
898 # 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
899 # 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
900 # 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
901 # being deleted.
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
902 class MyRef(weakref.ref):
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
903 pass
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
904
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
905 # 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
906 # to detect refcounting problems
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
907 def callback(w):
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
908 self.cbcalled += 1
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
909
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
910 o = C()
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
911 r1 = MyRef(o, callback)
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
912 r1.o = o
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
913 del o
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
914
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
915 del r1 # Used to crash here
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
916
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
917 self.assertEqual(self.cbcalled, 0)
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
918
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
919 # 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
920 # (since code paths are different)
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
921 o = C()
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
922 r1 = MyRef(o, callback)
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
923 r2 = MyRef(o, callback)
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
924 r1.r = r2
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
925 r2.o = o
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
926 del o
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
927 del r2
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
928
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
929 del r1 # Used to crash here
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
930
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
931 self.assertEqual(self.cbcalled, 0)
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
932
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32204
diff changeset
933
80476
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
934 class WeakMethodTestCase(unittest.TestCase):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
935
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
936 def _subclass(self):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
937 """Return a Object subclass overriding `some_method`."""
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
938 class C(Object):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
939 def some_method(self):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
940 return 6
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
941 return C
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
942
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
943 def test_alive(self):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
944 o = Object(1)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
945 r = weakref.WeakMethod(o.some_method)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
946 self.assertIsInstance(r, weakref.ReferenceType)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
947 self.assertIsInstance(r(), type(o.some_method))
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
948 self.assertIs(r().__self__, o)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
949 self.assertIs(r().__func__, o.some_method.__func__)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
950 self.assertEqual(r()(), 4)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
951
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
952 def test_object_dead(self):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
953 o = Object(1)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
954 r = weakref.WeakMethod(o.some_method)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
955 del o
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
956 gc.collect()
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
957 self.assertIs(r(), None)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
958
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
959 def test_method_dead(self):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
960 C = self._subclass()
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
961 o = C(1)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
962 r = weakref.WeakMethod(o.some_method)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
963 del C.some_method
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
964 gc.collect()
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
965 self.assertIs(r(), None)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
966
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
967 def test_callback_when_object_dead(self):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
968 # Test callback behaviour when object dies first.
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
969 C = self._subclass()
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
970 calls = []
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
971 def cb(arg):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
972 calls.append(arg)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
973 o = C(1)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
974 r = weakref.WeakMethod(o.some_method, cb)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
975 del o
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
976 gc.collect()
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
977 self.assertEqual(calls, [r])
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
978 # Callback is only called once.
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
979 C.some_method = Object.some_method
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
980 gc.collect()
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
981 self.assertEqual(calls, [r])
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
982
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
983 def test_callback_when_method_dead(self):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
984 # Test callback behaviour when method dies first.
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
985 C = self._subclass()
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
986 calls = []
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
987 def cb(arg):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
988 calls.append(arg)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
989 o = C(1)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
990 r = weakref.WeakMethod(o.some_method, cb)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
991 del C.some_method
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
992 gc.collect()
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
993 self.assertEqual(calls, [r])
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
994 # Callback is only called once.
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
995 del o
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
996 gc.collect()
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
997 self.assertEqual(calls, [r])
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
998
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
999 @support.cpython_only
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1000 def test_no_cycles(self):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1001 # A WeakMethod doesn't create any reference cycle to itself.
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1002 o = Object(1)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1003 def cb(_):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1004 pass
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1005 r = weakref.WeakMethod(o.some_method, cb)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1006 wr = weakref.ref(r)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1007 del r
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1008 self.assertIs(wr(), None)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1009
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1010 def test_equality(self):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1011 def _eq(a, b):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1012 self.assertTrue(a == b)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1013 self.assertFalse(a != b)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1014 def _ne(a, b):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1015 self.assertTrue(a != b)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1016 self.assertFalse(a == b)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1017 x = Object(1)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1018 y = Object(1)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1019 a = weakref.WeakMethod(x.some_method)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1020 b = weakref.WeakMethod(y.some_method)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1021 c = weakref.WeakMethod(x.other_method)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1022 d = weakref.WeakMethod(y.other_method)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1023 # Objects equal, same method
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1024 _eq(a, b)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1025 _eq(c, d)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1026 # Objects equal, different method
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1027 _ne(a, c)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1028 _ne(a, d)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1029 _ne(b, c)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1030 _ne(b, d)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1031 # Objects unequal, same or different method
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1032 z = Object(2)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1033 e = weakref.WeakMethod(z.some_method)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1034 f = weakref.WeakMethod(z.other_method)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1035 _ne(a, e)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1036 _ne(a, f)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1037 _ne(b, e)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1038 _ne(b, f)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1039 del x, y, z
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1040 gc.collect()
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1041 # Dead WeakMethods compare by identity
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1042 refs = a, b, c, d, e, f
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1043 for q in refs:
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1044 for r in refs:
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1045 self.assertEqual(q == r, q is r)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1046 self.assertEqual(q != r, q is not r)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1047
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1048 def test_hashing(self):
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1049 # Alive WeakMethods are hashable if the underlying object is
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1050 # hashable.
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1051 x = Object(1)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1052 y = Object(1)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1053 a = weakref.WeakMethod(x.some_method)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1054 b = weakref.WeakMethod(y.some_method)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1055 c = weakref.WeakMethod(y.other_method)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1056 # Since WeakMethod objects are equal, the hashes should be equal.
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1057 self.assertEqual(hash(a), hash(b))
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1058 ha = hash(a)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1059 # Dead WeakMethods retain their old hash value
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1060 del x, y
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1061 gc.collect()
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1062 self.assertEqual(hash(a), ha)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1063 self.assertEqual(hash(b), ha)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1064 # If it wasn't hashed when alive, a dead WeakMethod cannot be hashed.
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1065 self.assertRaises(TypeError, hash, c)
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1066
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1067
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
1068 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
1069
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
1070 COUNT = 10
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1071
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
1072 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
1073 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
1074 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
1075 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
1076 # 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
1077 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
1078 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
1079 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
1080 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
1081 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
1082 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
1083 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
1084 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
1085 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
1086 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
1087 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
1088 # 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
1089 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
1090 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
1091
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
1092 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
1093 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
1094
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
1095 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
1096 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
1097
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
1098 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
1099 # 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
1100 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
1101 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
1102 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
1103 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
1104 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
1105 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
1106 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
1107 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
1108 # 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
1109 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
1110 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
1111 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
1112 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
1113 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
1114 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
1115 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
1116 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
1117 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
1118 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
1119 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
1120 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
1121
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
1122 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
1123 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
1124
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
1125 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
1126 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
1127
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
1128 def test_weak_values(self):
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1129 #
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1130 # 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
1131 #
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1132 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
1133 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: 38464
diff changeset
1134 self.assertEqual(weakref.getweakrefcount(o), 1)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1135 self.assertIs(o, 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
1136 "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
1137 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
1138 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
1139 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
1140 items2.sort()
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1141 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
1142 "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
1143 del items1, items2
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1144 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
1145 del objects[0]
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1146 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
1147 "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
1148 del objects, o
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1149 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
1150 "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
1151 # regression on SF bug #447152:
e90a658a58d4 Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
Fred Drake <fdrake@acm.org>
parents: 17767
diff changeset
1152 dict = weakref.WeakValueDictionary()
e90a658a58d4 Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
Fred Drake <fdrake@acm.org>
parents: 17767
diff changeset
1153 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
1154 dict[2] = C()
e90a658a58d4 Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
Fred Drake <fdrake@acm.org>
parents: 17767
diff changeset
1155 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
1156
17172
10902ecabca9 Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents: 16885
diff changeset
1157 def test_weak_keys(self):
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1158 #
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1159 # 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
1160 # len(d), k in d.
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1161 #
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1162 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
1163 for o in objects:
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1164 self.assertEqual(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
1165 "wrong number of weak references to %r!" % o)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1166 self.assertIs(o.arg, 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
1167 "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
1168 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
1169 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
1170 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
1171 "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
1172 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
1173 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
1174 del objects[0]
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1175 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
1176 "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
1177 del objects, o
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1178 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
1179 "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
1180 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
1181 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
1182 self.assertIn(o, dict)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58194
diff changeset
1183 self.assertNotIn(34, dict)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1184
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1185 def test_weak_keyed_iters(self):
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1186 dict, objects = self.make_weak_keyed_dict()
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1187 self.check_iters(dict)
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1188
38079
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1189 # Test keyrefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1190 refs = dict.keyrefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1191 self.assertEqual(len(refs), len(objects))
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1192 objects2 = list(objects)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1193 for wr in refs:
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1194 ob = wr()
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58194
diff changeset
1195 self.assertIn(ob, dict)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58194
diff changeset
1196 self.assertIn(ob, dict)
38079
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1197 self.assertEqual(ob.arg, dict[ob])
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1198 objects2.remove(ob)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1199 self.assertEqual(len(objects2), 0)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1200
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1201 # Test iterkeyrefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1202 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
1203 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
1204 for wr in dict.keyrefs():
38079
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1205 ob = wr()
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58194
diff changeset
1206 self.assertIn(ob, dict)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58194
diff changeset
1207 self.assertIn(ob, dict)
38079
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1208 self.assertEqual(ob.arg, dict[ob])
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1209 objects2.remove(ob)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1210 self.assertEqual(len(objects2), 0)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1211
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1212 def test_weak_valued_iters(self):
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1213 dict, objects = self.make_weak_valued_dict()
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1214 self.check_iters(dict)
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1215
38079
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1216 # Test valuerefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1217 refs = dict.valuerefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1218 self.assertEqual(len(refs), len(objects))
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1219 objects2 = list(objects)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1220 for wr in refs:
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1221 ob = wr()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1222 self.assertEqual(ob, dict[ob.arg])
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1223 self.assertEqual(ob.arg, dict[ob.arg].arg)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1224 objects2.remove(ob)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1225 self.assertEqual(len(objects2), 0)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1226
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1227 # Test itervaluerefs()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1228 objects2 = list(objects)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1229 self.assertEqual(len(list(dict.itervaluerefs())), len(objects))
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1230 for wr in dict.itervaluerefs():
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1231 ob = wr()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1232 self.assertEqual(ob, dict[ob.arg])
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1233 self.assertEqual(ob.arg, dict[ob.arg].arg)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1234 objects2.remove(ob)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1235 self.assertEqual(len(objects2), 0)
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35195
diff changeset
1236
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1237 def check_iters(self, dict):
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1238 # 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
1239 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
1240 for item in dict.items():
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1241 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
1242 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
1243
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1244 # key iterator, via __iter__():
40818
a84460e53160 Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
1245 keys = list(dict.keys())
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1246 for k in dict:
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1247 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
1248 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
1249
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1250 # key iterator, via iterkeys():
40818
a84460e53160 Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
1251 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
1252 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
1253 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
1254 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
1255
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1256 # value iterator:
40818
a84460e53160 Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
1257 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
1258 for v in dict.values():
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1259 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
1260 self.assertFalse(values,
29862
f339f29b06cb stylistic nits:
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
1261 "itervalues() did not touch all values")
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1262
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1263 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
1264 n = len(dict)
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1265 it = iter(getattr(dict, iter_name)())
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1266 next(it) # Trigger internal iteration
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1267 # Destroy an object
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1268 del objects[-1]
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1269 gc.collect() # just in case
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1270 # 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
1271 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
1272 del it
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1273 # The removal has been committed
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1274 self.assertEqual(len(dict), n - 1)
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1275
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1276 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
1277 # 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
1278 # interfering with delayed removal.
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1279 # `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
1280 # 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
1281 # to the destroyed object.
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1282 with testcontext() as (k, v):
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
1283 self.assertNotIn(k, dict)
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1284 with testcontext() as (k, v):
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1285 self.assertRaises(KeyError, dict.__delitem__, k)
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
1286 self.assertNotIn(k, dict)
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1287 with testcontext() as (k, v):
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1288 self.assertRaises(KeyError, dict.pop, k)
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
1289 self.assertNotIn(k, dict)
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1290 with testcontext() as (k, v):
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1291 dict[k] = v
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1292 self.assertEqual(dict[k], v)
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1293 ddict = copy.copy(dict)
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1294 with testcontext() as (k, v):
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1295 dict.update(ddict)
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1296 self.assertEqual(dict, ddict)
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1297 with testcontext() as (k, v):
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1298 dict.clear()
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1299 self.assertEqual(len(dict), 0)
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1300
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1301 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
1302 # 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
1303 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
1304 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
1305 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
1306 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
1307 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
1308 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
1309 @contextlib.contextmanager
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1310 def testcontext():
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1311 try:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1312 it = iter(dict.items())
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1313 next(it)
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1314 # 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
1315 v = objects.pop().arg
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1316 gc.collect() # just in case
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1317 yield Object(v), v
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1318 finally:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1319 it = None # should commit all removals
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1320 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
1321
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1322 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
1323 # 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
1324 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
1325 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
1326 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
1327 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
1328 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
1329 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
1330 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
1331 @contextlib.contextmanager
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1332 def testcontext():
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1333 try:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1334 it = iter(dict.items())
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1335 next(it)
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1336 # 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
1337 k = objects.pop().arg
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1338 gc.collect() # just in case
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1339 yield k, Object(k)
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1340 finally:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1341 it = None # should commit all removals
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
1342 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
1343
23764
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
1344 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
1345 o = Object(3)
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
1346 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
1347 self.assertEqual(dict[o], 364)
23764
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
1348
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
1349 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
1350 o = Object(3)
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
1351 dict = weakref.WeakKeyDictionary({o:364})
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
1352 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
1353 self.assertEqual(dict[o], 364)
23764
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 22946
diff changeset
1354
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1355 def make_weak_keyed_dict(self):
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1356 dict = weakref.WeakKeyDictionary()
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1357 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
1358 for o in objects:
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1359 dict[o] = o.arg
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1360 return dict, objects
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1361
54456
49485f98a308 Issue #6149: Fix initialization of WeakValueDictionary objects from non-empty parameters.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49137
diff changeset
1362 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
1363 o = Object(3)
49485f98a308 Issue #6149: Fix initialization of WeakValueDictionary objects from non-empty parameters.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49137
diff changeset
1364 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
1365 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
1366
49485f98a308 Issue #6149: Fix initialization of WeakValueDictionary objects from non-empty parameters.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49137
diff changeset
1367 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
1368 o = Object(3)
49485f98a308 Issue #6149: Fix initialization of WeakValueDictionary objects from non-empty parameters.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49137
diff changeset
1369 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
1370 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
1371 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
1372
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1373 def make_weak_valued_dict(self):
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1374 dict = weakref.WeakValueDictionary()
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1375 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
1376 for o in objects:
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1377 dict[o.arg] = o
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1378 return dict, objects
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1379
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1380 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
1381 weakdict = klass()
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1382 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
1383 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
1384 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
1385 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
1386 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
1387 if k is key1:
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1388 self.assertIs(v, value1)
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1389 else:
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1390 self.assertIs(v, value2)
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1391 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
1392 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
1393 if k is key1:
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1394 self.assertIs(v, value1)
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1395 else:
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1396 self.assertIs(v, value2)
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1397
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1398 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
1399 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
1400 "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
1401
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1402 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
1403 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
1404 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
1405
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1406 def check_setdefault(self, klass, key, value1, value2):
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1407 self.assertIsNot(value1, value2,
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1408 "invalid test"
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1409 " -- 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
1410 weakdict = klass()
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1411 o = weakdict.setdefault(key, value1)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1412 self.assertIs(o, value1)
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58194
diff changeset
1413 self.assertIn(key, weakdict)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1414 self.assertIs(weakdict.get(key), value1)
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1415 self.assertIs(weakdict[key], value1)
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1416
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1417 o = weakdict.setdefault(key, value2)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1418 self.assertIs(o, value1)
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58194
diff changeset
1419 self.assertIn(key, weakdict)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1420 self.assertIs(weakdict.get(key), value1)
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1421 self.assertIs(weakdict[key], value1)
17767
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1422
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1423 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
1424 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
1425 "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
1426
47f4d0568c7c Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents: 17651
diff changeset
1427 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
1428 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
1429 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
1430
17547
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1431 def check_update(self, klass, dict):
17651
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1432 #
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
1433 # 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
1434 # d.get(), d[].
fa8326cc761c Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents: 17547
diff changeset
1435 #
17547
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1436 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
1437 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
1438 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
1439 for k in weakdict.keys():
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
1440 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
1441 v = dict.get(k)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1442 self.assertIs(v, weakdict[k])
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1443 self.assertIs(v, weakdict.get(k))
17547
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1444 for k in dict.keys():
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
1445 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
1446 v = dict[k]
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1447 self.assertIs(v, weakdict[k])
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1448 self.assertIs(v, weakdict.get(k))
17547
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1449
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1450 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
1451 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
1452 {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
1453
6252693afd9a Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents: 17498
diff changeset
1454 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
1455 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
1456 {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
1457
19764
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1458 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
1459 d = weakref.WeakKeyDictionary()
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1460 o1 = Object('1')
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1461 o2 = Object('2')
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1462 d[o1] = 'something'
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1463 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
1464 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
1465 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
1466 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
1467 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
1468
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1469 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
1470 d = weakref.WeakValueDictionary()
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1471 o1 = Object('1')
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1472 o2 = Object('2')
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1473 d['something'] = o1
578ef36f8fde Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
1474 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
1475 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
1476 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
1477 self.assertEqual(len(d), 1)
87195
d187eae08b8a Issue #19607: Use specific asserts in weakref tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80759
diff changeset
1478 self.assertEqual(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
1479
29257
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1480 def test_weak_keyed_bad_delitem(self):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1481 d = weakref.WeakKeyDictionary()
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1482 o = Object('1')
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1483 # 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
1484 # 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
1485 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
1486 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
1487
18c020463965 Fleshed out WeakKeyDictionary.__delitem__ NEWS to cover issues raised on
Tim Peters <tim.peters@gmail.com>
parents: 29257
diff changeset
1488 # 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
1489 # __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
1490 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
1491 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
1492 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
1493
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1494 def test_weak_keyed_cascading_deletes(self):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1495 # 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
1496 # 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
1497 # 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
1498
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1499 d = weakref.WeakKeyDictionary()
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1500 mutate = False
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1501
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1502 class C(object):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1503 def __init__(self, i):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1504 self.value = i
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1505 def __hash__(self):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1506 return hash(self.value)
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1507 def __eq__(self, other):
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1508 if mutate:
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1509 # 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
1510 # last strong reference to a key.
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1511 del objs[-1]
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1512 return self.value == other.value
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1513
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1514 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
1515 for o in objs:
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1516 d[o] = o.value
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1517 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
1518 # 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
1519 objs = list(d.keys())
29257
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1520 # 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
1521 # 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
1522 objs.reverse()
29258
18c020463965 Fleshed out WeakKeyDictionary.__delitem__ NEWS to cover issues raised on
Tim Peters <tim.peters@gmail.com>
parents: 29257
diff changeset
1523
29257
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1524 # 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
1525 # 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
1526 # 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
1527 # RuntimeError: dictionary changed size during iteration
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1528 # 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
1529 # 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
1530 # "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
1531 mutate = True
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1532 count = 0
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1533 for o in objs:
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1534 count += 1
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1535 del d[o]
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1536 self.assertEqual(len(d), 0)
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1537 self.assertEqual(count, 2)
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28922
diff changeset
1538
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
1539 from test import mapping_tests
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1540
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
1541 class WeakValueDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
29862
f339f29b06cb stylistic nits:
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
1542 """Check that WeakValueDictionary conforms to the mapping protocol"""
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1543 __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
1544 type2test = weakref.WeakValueDictionary
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1545 def _reference(self):
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1546 return self.__ref.copy()
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1547
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
1548 class WeakKeyDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
29862
f339f29b06cb stylistic nits:
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
1549 """Check that WeakKeyDictionary conforms to the mapping protocol"""
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1550 __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
1551 type2test = weakref.WeakKeyDictionary
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1552 def _reference(self):
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24963
diff changeset
1553 return self.__ref.copy()
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1554
83631
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1555
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1556 class FinalizeTestCase(unittest.TestCase):
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1557
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1558 class A:
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1559 pass
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1560
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1561 def _collect_if_necessary(self):
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1562 # we create no ref-cycles so in CPython no gc should be needed
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1563 if sys.implementation.name != 'cpython':
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1564 support.gc_collect()
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1565
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1566 def test_finalize(self):
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1567 def add(x,y,z):
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1568 res.append(x + y + z)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1569 return x + y + z
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1570
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1571 a = self.A()
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1572
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1573 res = []
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1574 f = weakref.finalize(a, add, 67, 43, z=89)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1575 self.assertEqual(f.alive, True)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1576 self.assertEqual(f.peek(), (a, add, (67,43), {'z':89}))
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1577 self.assertEqual(f(), 199)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1578 self.assertEqual(f(), None)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1579 self.assertEqual(f(), None)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1580 self.assertEqual(f.peek(), None)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1581 self.assertEqual(f.detach(), None)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1582 self.assertEqual(f.alive, False)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1583 self.assertEqual(res, [199])
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1584
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1585 res = []
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1586 f = weakref.finalize(a, add, 67, 43, 89)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1587 self.assertEqual(f.peek(), (a, add, (67,43,89), {}))
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1588 self.assertEqual(f.detach(), (a, add, (67,43,89), {}))
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1589 self.assertEqual(f(), None)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1590 self.assertEqual(f(), None)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1591 self.assertEqual(f.peek(), None)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1592 self.assertEqual(f.detach(), None)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1593 self.assertEqual(f.alive, False)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1594 self.assertEqual(res, [])
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1595
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1596 res = []
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1597 f = weakref.finalize(a, add, x=67, y=43, z=89)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1598 del a
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1599 self._collect_if_necessary()
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1600 self.assertEqual(f(), None)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1601 self.assertEqual(f(), None)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1602 self.assertEqual(f.peek(), None)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1603 self.assertEqual(f.detach(), None)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1604 self.assertEqual(f.alive, False)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1605 self.assertEqual(res, [199])
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1606
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1607 def test_order(self):
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1608 a = self.A()
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1609 res = []
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1610
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1611 f1 = weakref.finalize(a, res.append, 'f1')
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1612 f2 = weakref.finalize(a, res.append, 'f2')
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1613 f3 = weakref.finalize(a, res.append, 'f3')
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1614 f4 = weakref.finalize(a, res.append, 'f4')
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1615 f5 = weakref.finalize(a, res.append, 'f5')
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1616
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1617 # make sure finalizers can keep themselves alive
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1618 del f1, f4
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1619
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1620 self.assertTrue(f2.alive)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1621 self.assertTrue(f3.alive)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1622 self.assertTrue(f5.alive)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1623
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1624 self.assertTrue(f5.detach())
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1625 self.assertFalse(f5.alive)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1626
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1627 f5() # nothing because previously unregistered
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1628 res.append('A')
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1629 f3() # => res.append('f3')
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1630 self.assertFalse(f3.alive)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1631 res.append('B')
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1632 f3() # nothing because previously called
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1633 res.append('C')
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1634 del a
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1635 self._collect_if_necessary()
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1636 # => res.append('f4')
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1637 # => res.append('f2')
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1638 # => res.append('f1')
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1639 self.assertFalse(f2.alive)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1640 res.append('D')
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1641 f2() # nothing because previously called by gc
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1642
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1643 expected = ['A', 'f3', 'B', 'C', 'f4', 'f2', 'f1', 'D']
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1644 self.assertEqual(res, expected)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1645
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1646 def test_all_freed(self):
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1647 # we want a weakrefable subclass of weakref.finalize
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1648 class MyFinalizer(weakref.finalize):
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1649 pass
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1650
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1651 a = self.A()
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1652 res = []
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1653 def callback():
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1654 res.append(123)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1655 f = MyFinalizer(a, callback)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1656
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1657 wr_callback = weakref.ref(callback)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1658 wr_f = weakref.ref(f)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1659 del callback, f
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1660
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1661 self.assertIsNotNone(wr_callback())
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1662 self.assertIsNotNone(wr_f())
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1663
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1664 del a
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1665 self._collect_if_necessary()
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1666
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1667 self.assertIsNone(wr_callback())
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1668 self.assertIsNone(wr_f())
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1669 self.assertEqual(res, [123])
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1670
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1671 @classmethod
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1672 def run_in_child(cls):
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1673 def error():
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1674 # Create an atexit finalizer from inside a finalizer called
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1675 # at exit. This should be the next to be run.
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1676 g1 = weakref.finalize(cls, print, 'g1')
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1677 print('f3 error')
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1678 1/0
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1679
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1680 # cls should stay alive till atexit callbacks run
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1681 f1 = weakref.finalize(cls, print, 'f1', _global_var)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1682 f2 = weakref.finalize(cls, print, 'f2', _global_var)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1683 f3 = weakref.finalize(cls, error)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1684 f4 = weakref.finalize(cls, print, 'f4', _global_var)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1685
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1686 assert f1.atexit == True
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1687 f2.atexit = False
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1688 assert f3.atexit == True
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1689 assert f4.atexit == True
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1690
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1691 def test_atexit(self):
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1692 prog = ('from test.test_weakref import FinalizeTestCase;'+
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1693 'FinalizeTestCase.run_in_child()')
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1694 rc, out, err = script_helper.assert_python_ok('-c', prog)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1695 out = out.decode('ascii').splitlines()
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1696 self.assertEqual(out, ['f4 foobar', 'f3 error', 'g1', 'f1 foobar'])
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1697 self.assertTrue(b'ZeroDivisionError' in err)
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1698
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1699
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
1700 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
1701
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1702 >>> import weakref
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1703 >>> class Dict(dict):
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1704 ... pass
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1705 ...
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1706 >>> 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
1707 >>> 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
1708 >>> print(r() is obj)
38464
e989ee5d404a ("Forward-port" of r46506)
Armin Rigo <arigo@tunes.org>
parents: 38079
diff changeset
1709 True
35195
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1710
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1711 >>> import weakref
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1712 >>> class Object:
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1713 ... pass
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1714 ...
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1715 >>> o = Object()
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1716 >>> r = weakref.ref(o)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1717 >>> o2 = r()
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1718 >>> o is o2
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1719 True
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1720 >>> 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
1721 >>> print(r())
35195
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1722 None
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1723
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1724 >>> import weakref
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1725 >>> class ExtendedRef(weakref.ref):
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1726 ... 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
1727 ... super().__init__(ob, callback)
35195
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1728 ... self.__counter = 0
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40785
diff changeset
1729 ... for k, v in annotations.items():
35195
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1730 ... setattr(self, k, v)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1731 ... def __call__(self):
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1732 ... '''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
1733 ... times the reference has been called.
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1734 ... '''
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1735 ... ob = super().__call__()
35195
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1736 ... if ob is not None:
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1737 ... self.__counter += 1
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1738 ... ob = (ob, self.__counter)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1739 ... return ob
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40949
diff changeset
1740 ...
35195
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1741 >>> 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
1742 ... pass
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1743 ...
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1744 >>> a = A()
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1745 >>> r = ExtendedRef(a, foo=1, bar="baz")
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1746 >>> r.foo
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1747 1
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1748 >>> r.bar
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1749 'baz'
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1750 >>> r()[1]
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1751 1
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1752 >>> r()[1]
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1753 2
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1754 >>> r()[0] is a
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1755 True
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1756
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1757
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1758 >>> import weakref
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1759 >>> _id2obj_dict = weakref.WeakValueDictionary()
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1760 >>> def remember(obj):
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1761 ... oid = id(obj)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1762 ... _id2obj_dict[oid] = obj
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1763 ... return oid
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1764 ...
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1765 >>> def id2obj(oid):
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1766 ... return _id2obj_dict[oid]
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1767 ...
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1768 >>> a = A() # from here, just testing
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1769 >>> a_id = remember(a)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1770 >>> id2obj(a_id) is a
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1771 True
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1772 >>> del a
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1773 >>> try:
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1774 ... id2obj(a_id)
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1775 ... 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
1776 ... print('OK')
35195
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1777 ... 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
1778 ... print('WeakValueDictionary error')
35195
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1779 OK
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1780
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1781 """
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1782
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1783 __test__ = {'libreftest' : libreftest}
7804bbb26492 backport doctest for weakref examples
Georg Brandl <georg@python.org>
parents: 34847
diff changeset
1784
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19764
diff changeset
1785 def test_main():
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45302
diff changeset
1786 support.run_unittest(
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28240
diff changeset
1787 ReferencesTestCase,
80476
27c20650aeab Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80394
diff changeset
1788 WeakMethodTestCase,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28240
diff changeset
1789 MappingTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28240
diff changeset
1790 WeakValueDictionaryTestCase,
29862
f339f29b06cb stylistic nits:
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
1791 WeakKeyDictionaryTestCase,
47734
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47278
diff changeset
1792 SubclassableWeakrefTestCase,
83631
186cf551dae5 Issue #15528: Add weakref.finalize to support finalization using
Richard Oudkerk <shibturn@gmail.com>
parents: 83629
diff changeset
1793 FinalizeTestCase,
29862
f339f29b06cb stylistic nits:
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
1794 )
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45302
diff changeset
1795 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
1796
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19764
diff changeset
1797
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19764
diff changeset
1798 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
1799 test_main()