summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-04-03 00:39:26 +0000
committerRaymond Hettinger <python@rcn.com>2010-04-03 00:39:26 +0000
commit3928276e6442f52b0a2717df8d8441dbe03ccd53 (patch)
tree6051db2d23a0958ef3636e3d6dd88c96242624e2
parent6caf7ff505ca8d3369043e7909cb24a61908bed9 (diff)
downloadcpython-git-3928276e6442f52b0a2717df8d8441dbe03ccd53.tar.gz
Clear cyclical references in list based OrderedDict.
-rw-r--r--Lib/collections.py14
1 files changed, 11 insertions, 3 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index 64c40ca349..1f1f51072b 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -67,8 +67,10 @@ class OrderedDict(dict, MutableMapping):
PREV = 0
NEXT = 1
link = self.__map.pop(key)
- link[PREV][NEXT] = link[NEXT]
- link[NEXT][PREV] = link[PREV]
+ link_prev = link[PREV]
+ link_next = link[NEXT]
+ link_prev[NEXT] = link_next
+ link_next[PREV] = link_prev
def __iter__(self):
'od.__iter__() <==> iter(od)'
@@ -103,7 +105,11 @@ class OrderedDict(dict, MutableMapping):
return (self.__class__, (items,), inst_dict)
return self.__class__, (items,)
- clear = MutableMapping.clear
+ def clear(self):
+ 'od.clear() -> None. Remove all items from od.'
+ for k in dict.keys(self):
+ del self[k]
+
setdefault = MutableMapping.setdefault
update = MutableMapping.update
pop = MutableMapping.pop
@@ -157,6 +163,8 @@ class OrderedDict(dict, MutableMapping):
all(_imap(_eq, self.iteritems(), other.iteritems()))
return dict.__eq__(self, other)
+ def __del__(self):
+ self.clear() # eliminate cyclical references
################################################################################