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

raymond.hettinger python-checkins at python.org
Sat Apr 3 05:14:29 CEST 2010


Author: raymond.hettinger
Date: Sat Apr  3 05:14:28 2010
New Revision: 79650

Log:
Improve clear() method.  Keeps key/value refcnts >= 1 until final dict.clear() so that decrefs to zero won't trigger arbitrary code .  Also runs a bit faster.

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 05:14:28 2010
@@ -107,8 +107,14 @@
 
     def clear(self):
         'od.clear() -> None.  Remove all items from od.'
-        for k in dict.keys(self):
-            del self[k]
+        try:
+            for node in self.__map.itervalues():
+                del node[:]
+            self.__root[:] = [self.__root, self.__root, None]
+            self.__map.clear()
+        except AttributeError:
+            pass
+        dict.clear(self)
 
     setdefault = MutableMapping.setdefault
     update = MutableMapping.update


More information about the Python-checkins mailing list