summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_weakref.py
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2004-02-03 19:56:46 (GMT)
committerFred Drake <fdrake@acm.org>2004-02-03 19:56:46 (GMT)
commitea2adc9c8007c55420c0567ad3e0dd2a445893e6 (patch)
treec45d6b505003b1d4e758826950d0e15206a07c6a /Lib/test/test_weakref.py
parent6a2852cd48fbe4164c6a6eec95a086e8630dde45 (diff)
downloadcpython-ea2adc9c8007c55420c0567ad3e0dd2a445893e6.zip
cpython-ea2adc9c8007c55420c0567ad3e0dd2a445893e6.tar.gz
cpython-ea2adc9c8007c55420c0567ad3e0dd2a445893e6.tar.bz2
- add tests that exercise fixes for the PyWeakref_NewRef() and
PyWeakref_NewProxy() constructors from the C API - elaborate the getweakrefcount() and getweakrefs() tests slightly
Diffstat (limited to 'Lib/test/test_weakref.py')
-rw-r--r--Lib/test/test_weakref.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py
index d42922a..f732d7b 100644
--- a/Lib/test/test_weakref.py
+++ b/Lib/test/test_weakref.py
@@ -183,6 +183,38 @@ class ReferencesTestCase(TestBase):
self.assertEqual(L3[:5], p3[:5])
self.assertEqual(L3[2:5], p3[2:5])
+ # The PyWeakref_* C API is documented as allowing either NULL or
+ # None as the value for the callback, where either means "no
+ # callback". The "no callback" ref and proxy objects are supposed
+ # to be shared so long as they exist by all callers so long as
+ # they are active. In Python 2.3.3 and earlier, this guaranttee
+ # was not honored, and was broken in different ways for
+ # PyWeakref_NewRef() and PyWeakref_NewProxy(). (Two tests.)
+
+ def test_shared_ref_without_callback(self):
+ self.check_shared_without_callback(weakref.ref)
+
+ def test_shared_proxy_without_callback(self):
+ self.check_shared_without_callback(weakref.proxy)
+
+ def check_shared_without_callback(self, makeref):
+ o = Object(1)
+ p1 = makeref(o, None)
+ p2 = makeref(o, None)
+ self.assert_(p1 is p2, "both callbacks were None in the C API")
+ del p1, p2
+ p1 = makeref(o)
+ p2 = makeref(o, None)
+ self.assert_(p1 is p2, "callbacks were NULL, None in the C API")
+ del p1, p2
+ p1 = makeref(o)
+ p2 = makeref(o)
+ self.assert_(p1 is p2, "both callbacks were NULL in the C API")
+ del p1, p2
+ p1 = makeref(o, None)
+ p2 = makeref(o)
+ self.assert_(p1 is p2, "callbacks were None, NULL in the C API")
+
def test_callable_proxy(self):
o = Callable()
ref1 = weakref.proxy(o)
@@ -249,6 +281,11 @@ class ReferencesTestCase(TestBase):
self.assert_(weakref.getweakrefcount(o) == 4,
"got wrong number of weak reference objects")
+ del ref1, ref2, proxy1, proxy2
+ self.assert_(weakref.getweakrefcount(o) == 0,
+ "weak reference objects not unlinked from"
+ " referent when discarded.")
+
# assumes ints do not support weakrefs
self.assert_(weakref.getweakrefcount(1) == 0,
"got wrong number of weak reference objects for int")
@@ -268,6 +305,10 @@ class ReferencesTestCase(TestBase):
self.assert_(weakref.getweakrefs(o) == [ref1],
"list of refs does not match")
+ del ref1
+ self.assert_(weakref.getweakrefs(o) == [],
+ "list of refs not cleared")
+
# assumes ints do not support weakrefs
self.assert_(weakref.getweakrefs(1) == [],
"list of refs does not match for int")