summaryrefslogtreecommitdiffstats
path: root/Lib/weakref.py
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2006-05-02 06:53:59 (GMT)
committerFred Drake <fdrake@acm.org>2006-05-02 06:53:59 (GMT)
commit017e68c413be6262eb1e71b0f0c5676d6db33a43 (patch)
treed9d2649217fc7fc9a2eb2f98870f434a1f1d37ae /Lib/weakref.py
parenta6d01cec3ff3b945565653f96141c693af639924 (diff)
downloadcpython-017e68c413be6262eb1e71b0f0c5676d6db33a43.zip
cpython-017e68c413be6262eb1e71b0f0c5676d6db33a43.tar.gz
cpython-017e68c413be6262eb1e71b0f0c5676d6db33a43.tar.bz2
SF #1479988: add methods to allow access to weakrefs for the
weakref.WeakKeyDictionary and weakref.WeakValueDictionary
Diffstat (limited to 'Lib/weakref.py')
-rw-r--r--Lib/weakref.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/Lib/weakref.py b/Lib/weakref.py
index 09bd0be..4f6d757 100644
--- a/Lib/weakref.py
+++ b/Lib/weakref.py
@@ -118,6 +118,18 @@ class WeakValueDictionary(UserDict.UserDict):
def __iter__(self):
return self.data.iterkeys()
+ def itervaluerefs(self):
+ """Return an iterator that yields the weak references to the values.
+
+ The references are not guaranteed to be 'live' at the time
+ they are used, so the result of calling the references needs
+ to be checked before being used. This can be used to avoid
+ creating references that will cause the garbage collector to
+ keep the values around longer than needed.
+
+ """
+ return self.data.itervalues()
+
def itervalues(self):
for wr in self.data.itervalues():
obj = wr()
@@ -162,6 +174,18 @@ class WeakValueDictionary(UserDict.UserDict):
if len(kwargs):
self.update(kwargs)
+ def valuerefs(self):
+ """Return a list of weak references to the values.
+
+ The references are not guaranteed to be 'live' at the time
+ they are used, so the result of calling the references needs
+ to be checked before being used. This can be used to avoid
+ creating references that will cause the garbage collector to
+ keep the values around longer than needed.
+
+ """
+ return self.data.values()
+
def values(self):
L = []
for wr in self.data.values():
@@ -263,6 +287,18 @@ class WeakKeyDictionary(UserDict.UserDict):
if key is not None:
yield key, value
+ def iterkeyrefs(self):
+ """Return an iterator that yields the weak references to the keys.
+
+ The references are not guaranteed to be 'live' at the time
+ they are used, so the result of calling the references needs
+ to be checked before being used. This can be used to avoid
+ creating references that will cause the garbage collector to
+ keep the keys around longer than needed.
+
+ """
+ return self.data.iterkeys()
+
def iterkeys(self):
for wr in self.data.iterkeys():
obj = wr()
@@ -275,6 +311,18 @@ class WeakKeyDictionary(UserDict.UserDict):
def itervalues(self):
return self.data.itervalues()
+ def keyrefs(self):
+ """Return a list of weak references to the keys.
+
+ The references are not guaranteed to be 'live' at the time
+ they are used, so the result of calling the references needs
+ to be checked before being used. This can be used to avoid
+ creating references that will cause the garbage collector to
+ keep the keys around longer than needed.
+
+ """
+ return self.data.keys()
+
def keys(self):
L = []
for wr in self.data.keys():