[Python-checkins] r79637 - python/trunk/Lib/collections.py

raymond.hettinger python-checkins at python.org
Sat Apr 3 02:39:26 CEST 2010


Author: raymond.hettinger
Date: Sat Apr  3 02:39:26 2010
New Revision: 79637

Log:
Clear cyclical references in list based OrderedDict.

Modified:
   python/trunk/Lib/collections.py

Modified: python/trunk/Lib/collections.py
==============================================================================
--- python/trunk/Lib/collections.py	(original)
+++ python/trunk/Lib/collections.py	Sat Apr  3 02:39:26 2010
@@ -67,8 +67,10 @@
         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 @@
             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 @@
                    all(_imap(_eq, self.iteritems(), other.iteritems()))
         return dict.__eq__(self, other)
 
+    def __del__(self):
+        self.clear()                # eliminate cyclical references
 
 
 ################################################################################


More information about the Python-checkins mailing list