Clear cyclical references in list based OrderedDict.

This commit is contained in:
Raymond Hettinger 2010-04-03 00:39:26 +00:00
parent 6caf7ff505
commit 3928276e64
1 changed files with 11 additions and 3 deletions

View File

@ -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
################################################################################