summaryrefslogtreecommitdiff
path: root/Lib/weakref.py
diff options
context:
space:
mode:
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 09bd0bee24..4f6d757fe3 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():